Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / filetiff.C
blob2206c096f8d2802e0522a6135d48d227f2051ce8
1 #include "asset.h"
2 #include "bcwidgetgrid.h"
3 #include "edit.h"
4 #include "file.h"
5 #include "filetiff.h"
6 #include "interlacemodes.h"
7 #include "language.h"
8 #include "vframe.h"
9 #include "mainerror.h"
11 #include <stdint.h>
12 #include <string.h>
13 #include <unistd.h>
15 FileTIFF::FileTIFF(Asset_GC asset, File *file)
16  : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
18         asset->video_data = 1;
19         temp = 0;
22 FileTIFF::~FileTIFF()
24         if(temp) delete temp;
28 void FileTIFF::get_parameters(BC_WindowBase *parent_window, 
29         Asset_GC asset, 
30         BC_WindowBase* &format_window,
31         int audio_options,
32         int video_options)
34         if(video_options)
35         {
36                 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
37                 format_window = window;
38                 window->create_objects();
39                 window->run_window();
40                 delete window;
41         }
45 int FileTIFF::check_sig(Asset_GC asset)
47         FILE *stream = fopen(asset->path, "rb");
49         if(stream)
50         {
51                 char test[10];
52                 fread(test, 10, 1, stream);
53                 fclose(stream);
55                 if(test[0] == 'I' && test[1] == 'I')
56                 {
57                         return 1;
58                 }
59                 else
60                 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' && 
61                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
62                 {
63                         return 1;
64                 }
65                 else
66                 if(strlen(asset->path) > 4 && 
67                         !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
68                 {
69                         return 1;
70                 }
71                 else
72                 if(strlen(asset->path) > 5 && 
73                         !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
74                 {
75                         return 1;
76                 }
77         }
78         return 0;
81 char* FileTIFF::compression_to_str(int value)
83         switch(value)
84         {
85                 case FileTIFF::NONE: return "None"; break;
86                 case FileTIFF::LZW: return "LZW"; break;
87                 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
88                 case FileTIFF::DEFLATE: return "Deflate"; break;
89                 case FileTIFF::JPEG: return "JPEG"; break;
90                 default: 
91                         return "None"; 
92                         break;
93         }
96 char* FileTIFF::cmodel_to_str(int value)
98         switch(value)
99         {
100                 case FileTIFF::GREYSCALE: return "Greyscale"; break;
101                 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
102                 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
103                 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
104                 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
105                 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
106                 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
107                 default: 
108                         return "RGB-8 Bit"; 
109                         break;
110         }
114 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
116         if(edit->asset->format == FILE_TIFF_LIST ||
117                 edit->asset->format == FILE_TIFF)
118                 return 1;
119         
120         return 0;
125 int FileTIFF::read_frame_header(char *path)
127         TIFF *stream;
128         int result = 0;
130         if(!(stream = TIFFOpen(path, "rb")))
131         {
132                 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
133                 return 1;
134         }
136         char *ptr = 0;
137         TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
138 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
139         if(ptr && !strcmp(ptr, "Canon EOS-1DS"))       // FIXME: Does this have a purpose?
140         {
141                 printf("FileTIFF::read_frame_header: got a %s.\n",
142                         ptr);
143         }
145 // The raw format for certain cameras deviates from TIFF here.
147         TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
148         TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
150         int components = 0;
151         TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
152         int bitspersample = 0;
153         TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
154         int sampleformat = 0;
155         TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
157         if(bitspersample == 8 && components == 3)
158                 asset->tiff_cmodel = FileTIFF::RGB_888;
159         else
160         if(bitspersample == 16 && components == 3)
161                 asset->tiff_cmodel = FileTIFF::RGB_161616;
162         else
163         if(bitspersample == 8 && components == 4)
164                 asset->tiff_cmodel = FileTIFF::RGBA_8888;
165         else
166         if(bitspersample == 16 && components == 4)
167                 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
168         else
169         if(bitspersample == 32 && components == 3)
170                 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
171         else
172         if(bitspersample == 32 && components == 4)
173                 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
174         else
175         if(bitspersample == 8 && (components == 1 || components == 0))
176                 asset->tiff_cmodel = FileTIFF::GREYSCALE;
178 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
179         TIFFClose(stream);
181         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
183         return result;
186 int FileTIFF::colormodel_supported(int colormodel)
188         switch(asset->tiff_cmodel)
189         {
190                 case FileTIFF::RGB_888: return BC_RGB888; break;
191                 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
192                 case FileTIFF::GREYSCALE: return BC_RGB888; break;
193                 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
194                 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
195                 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
196                 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
197                 default: return BC_RGB888; break;
198         }
201 int FileTIFF::get_best_colormodel(Asset_GC asset, int driver)
203         switch(asset->tiff_cmodel)
204         {
205                 case FileTIFF::GREYSCALE: return BC_RGB888; break;
206                 case FileTIFF::RGB_888: return BC_RGB888; break;
207                 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
208                 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
209                 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
210                 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
211                 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
212                 default: return BC_RGB888; break;
213         }
217 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
219         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
220         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
221                 return 0;
222         memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
223         tiff_unit->offset += size;
224         return size;
227 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
229         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
230         if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
231         {
232                 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
233         }
236         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
237                 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
238         memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
239                 buf,
240                 size);
241         tiff_unit->offset += size;
242         return size;
245 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
247         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
248         switch(whence)
249         {
250                 case SEEK_SET:
251                         tiff_unit->offset = off;
252                         break;
253                 case SEEK_CUR:
254                         tiff_unit->offset += off;
255                         break;
256                 case SEEK_END:
257                         tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
258                         break;
259         }
260         return tiff_unit->offset;
263 static int tiff_close(thandle_t ptr)
265         return 0;
268 static toff_t tiff_size(thandle_t ptr)
270         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
271         return tiff_unit->data->get_compressed_size();
274 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
276         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
277         *pbase = tiff_unit->data->get_data();
278         *psize = tiff_unit->data->get_compressed_size();
279         return 0;
282 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
286 int FileTIFF::read_frame(VFrame *output, VFrame *input)
288         FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
289         TIFF *stream;
290         unit->offset = 0;
291         unit->data = input;
293         stream = TIFFClientOpen("FileTIFF", 
294                 "r",
295             (void*)unit,
296             tiff_read, 
297                 tiff_write,
298             tiff_seek, 
299                 tiff_close,
300             tiff_size,
301             tiff_mmap, 
302                 tiff_unmap);
304 // This loads the original TIFF data into each scanline of the output frame, 
305 // assuming the output scanlines are bigger than the input scanlines.
306 // Then it expands the input data in reverse to fill the row.
307         for(int i = 0; i < asset->height; i++)
308         {
309                 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
311 // For the greyscale model, the output is RGB888 but the input must be expanded
312                 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
313                 {
314                         unsigned char *row = output->get_rows()[i];
315                         for(int j = output->get_w() - 1; j >= 0; j--)
316                         {
317                                 unsigned char value = row[j];
318                                 row[j * 3] = value;
319                                 row[j * 3 + 1] = value;
320                                 row[j * 3 + 2] = value;
321                         }
322                 }
323 // For the 16 bit models, the output is floating point.
324                 else
325                 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
326                 {
327                         uint16_t *input_row = (uint16_t*)output->get_rows()[i];
328                         float *output_row = (float*)output->get_rows()[i];
329                         for(int j = output->get_w() - 1; j >= 0; j--)
330                         {
331                                 uint16_t r = input_row[j * 3];
332                                 uint16_t g = input_row[j * 3 + 1];
333                                 uint16_t b = input_row[j * 3 + 2];
334                                 output_row[j * 3] = (float)r / 65535;
335                                 output_row[j * 3 + 1] = (float)g / 65535;
336                                 output_row[j * 3 + 2] = (float)b / 65535;
337                         }
338                 }
339                 else
340                 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
341                 {
342                         uint16_t *input_row = (uint16_t*)output->get_rows()[i];
343                         float *output_row = (float*)output->get_rows()[i];
344                         for(int j = output->get_w() - 1; j >= 0; j--)
345                         {
346                                 uint16_t r = input_row[j * 4];
347                                 uint16_t g = input_row[j * 4 + 1];
348                                 uint16_t b = input_row[j * 4 + 2];
349                                 output_row[j * 4] = (float)r / 65535;
350                                 output_row[j * 4 + 1] = (float)g / 65535;
351                                 output_row[j * 4 + 2] = (float)b / 65535;
352                         }
353                 }
354         }
356         TIFFClose(stream);
357         delete unit;
359         return 0;
362 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
364 //printf("FileTIFF::write_frame 1\n");
365         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
366         int result = 0;
367         TIFF *stream;
368         tiff_unit->offset = 0;
369         tiff_unit->data = data;
370         tiff_unit->data->set_compressed_size(0);
372         stream = TIFFClientOpen("FileTIFF", 
373                 "w",
374             (void*)tiff_unit,
375             tiff_read, 
376                 tiff_write,
377             tiff_seek, 
378                 tiff_close,
379             tiff_size,
380             tiff_mmap, 
381                 tiff_unmap);
383         int components, color_model, bits, type, compression;
384         int sampleformat = SAMPLEFORMAT_UINT;
385         int bytesperrow;
386         switch(asset->tiff_cmodel)
387         {
388                 case FileTIFF::RGB_888: 
389                         components = 3;
390                         color_model = BC_RGB888;
391                         bits = 8;
392                         type = TIFF_BYTE;
393                         bytesperrow = 3 * asset->width;
394                         break;
395                 case FileTIFF::RGB_161616: 
396                         components = 3;
397                         color_model = BC_RGB_FLOAT;
398                         bits = 16;
399                         type = TIFF_SHORT;
400                         bytesperrow = 6 * asset->width;
401                         break;
402                 case FileTIFF::RGBA_8888: 
403                         components = 4;
404                         color_model = BC_RGBA8888;
405                         bits = 8;
406                         type = TIFF_BYTE;
407                         bytesperrow = 4 * asset->width;
408                         break;
409                 case FileTIFF::RGBA_16161616: 
410                         components = 4;
411                         color_model = BC_RGBA_FLOAT;
412                         bits = 16;
413                         type = TIFF_SHORT;
414                         bytesperrow = 8 * asset->width;
415                         break;
416                 case FileTIFF::RGB_FLOAT: 
417                         components = 3;
418                         color_model = BC_RGB_FLOAT;
419                         bits = 32;
420                         type = TIFF_FLOAT;
421                         sampleformat = SAMPLEFORMAT_IEEEFP;
422                         bytesperrow = 12 * asset->width;
423                         break;
424                 case FileTIFF::RGBA_FLOAT: 
425                         components = 4;
426                         color_model = BC_RGBA_FLOAT;
427                         bits = 32;
428                         type = TIFF_FLOAT;
429                         sampleformat = SAMPLEFORMAT_IEEEFP;
430                         bytesperrow = 16 * asset->width;
431                         break;
432                 default: 
433                         components = 3;
434                         color_model = BC_RGB888;
435                         bits = 8;
436                         type = TIFF_BYTE;
437                         bytesperrow = 3 * asset->width;
438                         break;
439         }
442         switch(asset->tiff_compression)
443         {
444                 case FileTIFF::LZW:
445                         compression = COMPRESSION_LZW;
446                         break;
447                 case FileTIFF::PACK_BITS:
448                         compression = COMPRESSION_PACKBITS;
449                         break;
450                 case FileTIFF::DEFLATE:
451                         compression = COMPRESSION_DEFLATE;
452                         break;
453                 case FileTIFF::JPEG:
454                         compression = COMPRESSION_JPEG;
455                         break;
456                 default:
457                         compression = COMPRESSION_NONE;
458                         break;
459         }
461         TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
462         TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
463         TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
464         TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
465         TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
466     TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
467         TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
468         TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
469         TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
470                 TIFFDefaultStripSize(stream, (uint32_t)-1));
471 //      TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
472 //              (8 * 1024) / bytesperrow);
473         TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
475         if(frame->get_color_model() == color_model)
476         {
477                 for(int i = 0; i < asset->height; i++)
478                 {
479                         TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
480                 }
481         }
482         else
483         {
484                 if(tiff_unit->temp &&
485                         tiff_unit->temp->get_color_model() != color_model)
486                 {
487                         delete tiff_unit->temp;
488                         tiff_unit->temp = 0;
489                 }
490                 if(!tiff_unit->temp)
491                 {
492                         tiff_unit->temp = new VFrame(0,
493                                 asset->width,
494                                 asset->height,
495                                 color_model);
496                 }
498                 cmodel_transfer(tiff_unit->temp->get_rows(), 
499                         frame->get_rows(),
500                         tiff_unit->temp->get_y(),
501                         tiff_unit->temp->get_u(),
502                         tiff_unit->temp->get_v(),
503                         frame->get_y(),
504                         frame->get_u(),
505                         frame->get_v(),
506                         0, 
507                         0, 
508                         frame->get_w(), 
509                         frame->get_h(),
510                         0, 
511                         0, 
512                         frame->get_w(), 
513                         frame->get_h(),
514                         frame->get_color_model(), 
515                         color_model,
516                         0,
517                         frame->get_w(),
518                         frame->get_w());
519                 for(int i = 0; i < asset->height; i++)
520                 {
521                         TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
522                 }
523         }
525         TIFFClose(stream);
527 //printf("FileTIFF::write_frame 10\n");
528         return result;
531 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
533         return new FileTIFFUnit(this, writer);
543 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
544  : FrameWriterUnit(writer)
546         this->file = file;
547         temp = 0;
550 FileTIFFUnit::~FileTIFFUnit()
552         if(temp) delete temp;
566 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset_GC asset)
567  : BC_Window(PROGRAM_NAME ": Video Compression",
568         parent_window->get_abs_cursor_x(1),
569         parent_window->get_abs_cursor_y(1),
570         400,
571         200,
572         400,
573         200,
574         0,
575         1)
577         this->parent_window = parent_window;
578         this->asset = asset;
581 TIFFConfigVideo::~TIFFConfigVideo()
585 int TIFFConfigVideo::create_objects()
587         int x = 10, y = 10;
589         BC_WidgetGrid *wg;
590         BC_RelocatableWidget *rw;
592         wg = add_widgetgrid(new BC_WidgetGrid(10, 10, 10, 10, 3, 3));
594         rw =
595         add_subwindow(new BC_Title(x, y, "Colorspace:"));
596         TIFFColorspace *menu1;
597         add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
598         menu1->create_objects();
599         wg->add(rw, 1, 0);
600         wg->add(menu1, 1, 1);
601         y += 40;
602         rw =
603         add_subwindow(new BC_Title(x, y, "Compression:"));
604         TIFFCompression *menu2;
605         add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
606         menu2->create_objects();
607         wg->add(rw, 0, 0);
608         wg->add(menu2, 0, 1);
610         rw =
611         add_subwindow(new BC_OKButton(this));
613         wg->move_widgets();
614         resize_window(wg->get_w_wm(), wg->get_h_wm() + rw->get_h());
615         return 0;
618 int TIFFConfigVideo::close_event()
620         set_done(0);
621         return 1;
629 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
630  : BC_PopupMenu(x,
631         y,
632         w,
633         FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
635         this->gui = gui;
637 int TIFFColorspace::handle_event()
639         return 1;
641 void TIFFColorspace::create_objects()
643         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
644 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
645         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
646 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
647         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
648         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
652 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
653  : BC_MenuItem(FileTIFF::cmodel_to_str(value))
655         this->gui = gui;
656         this->value = value;
658 int TIFFColorspaceItem::handle_event()
660         gui->asset->tiff_cmodel = value;
661         return 0;
670 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
671  : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
673         this->gui = gui;
675 int TIFFCompression::handle_event()
677         return 1;
679 void TIFFCompression::create_objects()
681         add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
682 //      add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
683         add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
684 //      add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
685 //      add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
692 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
693  : BC_MenuItem(FileTIFF::compression_to_str(value))
695         this->gui = gui;
696         this->value = value;
698 int TIFFCompressionItem::handle_event()
700         gui->asset->tiff_compression = value;
701         return 0;
707 //      Local Variables:
708 //      mode: C++
709 //      c-file-style: "linux"
710 //      End: