r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / filetiff.C
blobdaf34b5f28872154a57ad261a8394a39449d50c9
1 #include "asset.h"
2 #include "edit.h"
3 #include "file.h"
4 #include "filetiff.h"
5 #include "interlacemodes.h"
6 #include "language.h"
7 #include "vframe.h"
9 #include <stdint.h>
10 #include <string.h>
11 #include <unistd.h>
13 FileTIFF::FileTIFF(Asset *asset, File *file)
14  : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
16         asset->video_data = 1;
17         temp = 0;
20 FileTIFF::~FileTIFF()
22         if(temp) delete temp;
26 void FileTIFF::get_parameters(BC_WindowBase *parent_window, 
27         Asset *asset, 
28         BC_WindowBase* &format_window,
29         int audio_options,
30         int video_options)
32         if(video_options)
33         {
34                 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
35                 format_window = window;
36                 window->create_objects();
37                 window->run_window();
38                 delete window;
39         }
43 int FileTIFF::check_sig(Asset *asset)
45         FILE *stream = fopen(asset->path, "rb");
47         if(stream)
48         {
49                 char test[10];
50                 fread(test, 10, 1, stream);
51                 fclose(stream);
53                 if(test[0] == 'I' && test[1] == 'I')
54                 {
55                         return 1;
56                 }
57                 else
58                 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' && 
59                         test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
60                 {
61                         return 1;
62                 }
63                 else
64                 if(strlen(asset->path) > 4 && 
65                         !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
66                 {
67                         return 1;
68                 }
69                 else
70                 if(strlen(asset->path) > 5 && 
71                         !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
72                 {
73                         return 1;
74                 }
75         }
76         return 0;
79 char* FileTIFF::compression_to_str(int value)
81         switch(value)
82         {
83                 case FileTIFF::NONE: return "None"; break;
84                 case FileTIFF::LZW: return "LZW"; break;
85                 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
86                 case FileTIFF::DEFLATE: return "Deflate"; break;
87                 case FileTIFF::JPEG: return "JPEG"; break;
88                 default: 
89                         return "None"; 
90                         break;
91         }
94 char* FileTIFF::cmodel_to_str(int value)
96         switch(value)
97         {
98                 case FileTIFF::GREYSCALE: return "Greyscale"; break;
99                 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
100                 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
101                 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
102                 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
103                 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
104                 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
105                 default: 
106                         return "RGB-8 Bit"; 
107                         break;
108         }
112 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
114         if(edit->asset->format == FILE_TIFF_LIST ||
115                 edit->asset->format == FILE_TIFF)
116                 return 1;
117         
118         return 0;
123 int FileTIFF::read_frame_header(char *path)
125         TIFF *stream;
126         int result = 0;
128         if(!(stream = TIFFOpen(path, "rb")))
129         {
130                 fprintf(stderr, "FileTIFF::read_frame_header failed from %s\n", asset->path);
131                 return 1;
132         }
134         char *ptr = 0;
135         TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
136 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
137         if(ptr && !strcmp(ptr, "Canon EOS-1DS"))
138         {
139                 printf("FileTIFF::read_frame_header: got a %s.\n",
140                         ptr);
141         }
143 // The raw format for certain cameras deviates from TIFF here.
145         TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
146         TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
148         int components = 0;
149         TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
150         int bitspersample = 0;
151         TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
152         int sampleformat = 0;
153         TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
155         if(bitspersample == 8 && components == 3)
156                 asset->tiff_cmodel = FileTIFF::RGB_888;
157         else
158         if(bitspersample == 16 && components == 3)
159                 asset->tiff_cmodel = FileTIFF::RGB_161616;
160         else
161         if(bitspersample == 8 && components == 4)
162                 asset->tiff_cmodel = FileTIFF::RGBA_8888;
163         else
164         if(bitspersample == 16 && components == 4)
165                 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
166         else
167         if(bitspersample == 32 && components == 3)
168                 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
169         else
170         if(bitspersample == 32 && components == 4)
171                 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
172         else
173         if(bitspersample == 8 && (components == 1 || components == 0))
174                 asset->tiff_cmodel = FileTIFF::GREYSCALE;
176 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
177         TIFFClose(stream);
179         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
181         return result;
184 int FileTIFF::colormodel_supported(int colormodel)
186         switch(asset->tiff_cmodel)
187         {
188                 case FileTIFF::RGB_888: return BC_RGB888; break;
189                 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
190                 case FileTIFF::GREYSCALE: return BC_RGB888; break;
191                 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
192                 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
193                 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
194                 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
195                 default: return BC_RGB888; break;
196         }
199 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
201         switch(asset->tiff_cmodel)
202         {
203                 case FileTIFF::GREYSCALE: return BC_RGB888; break;
204                 case FileTIFF::RGB_888: return BC_RGB888; break;
205                 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
206                 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
207                 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
208                 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
209                 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
210                 default: return BC_RGB888; break;
211         }
215 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
217         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
218         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
219                 return 0;
220         memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
221         tiff_unit->offset += size;
222         return size;
225 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
227         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
228         if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
229         {
230                 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
231         }
234         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
235                 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
236         memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
237                 buf,
238                 size);
239         tiff_unit->offset += size;
240         return size;
243 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
245         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
246         switch(whence)
247         {
248                 case SEEK_SET:
249                         tiff_unit->offset = off;
250                         break;
251                 case SEEK_CUR:
252                         tiff_unit->offset += off;
253                         break;
254                 case SEEK_END:
255                         tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
256                         break;
257         }
258         return tiff_unit->offset;
261 static int tiff_close(thandle_t ptr)
263         return 0;
266 static toff_t tiff_size(thandle_t ptr)
268         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
269         return tiff_unit->data->get_compressed_size();
272 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
274         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
275         *pbase = tiff_unit->data->get_data();
276         *psize = tiff_unit->data->get_compressed_size();
277         return 0;
280 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
284 int FileTIFF::read_frame(VFrame *output, VFrame *input)
286         FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
287         TIFF *stream;
288         unit->offset = 0;
289         unit->data = input;
291         stream = TIFFClientOpen("FileTIFF", 
292                 "r",
293             (void*)unit,
294             tiff_read, 
295                 tiff_write,
296             tiff_seek, 
297                 tiff_close,
298             tiff_size,
299             tiff_mmap, 
300                 tiff_unmap);
302 // This loads the original TIFF data into each scanline of the output frame, 
303 // assuming the output scanlines are bigger than the input scanlines.
304 // Then it expands the input data in reverse to fill the row.
305         for(int i = 0; i < asset->height; i++)
306         {
307                 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
309 // For the greyscale model, the output is RGB888 but the input must be expanded
310                 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
311                 {
312                         unsigned char *row = output->get_rows()[i];
313                         for(int j = output->get_w() - 1; j >= 0; j--)
314                         {
315                                 unsigned char value = row[j];
316                                 row[j * 3] = value;
317                                 row[j * 3 + 1] = value;
318                                 row[j * 3 + 2] = value;
319                         }
320                 }
321 // For the 16 bit models, the output is floating point.
322                 else
323                 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
324                 {
325                         uint16_t *input_row = (uint16_t*)output->get_rows()[i];
326                         float *output_row = (float*)output->get_rows()[i];
327                         for(int j = output->get_w() - 1; j >= 0; j--)
328                         {
329                                 uint16_t r = input_row[j * 3];
330                                 uint16_t g = input_row[j * 3 + 1];
331                                 uint16_t b = input_row[j * 3 + 2];
332                                 output_row[j * 3] = (float)r / 65535;
333                                 output_row[j * 3 + 1] = (float)g / 65535;
334                                 output_row[j * 3 + 2] = (float)b / 65535;
335                         }
336                 }
337                 else
338                 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
339                 {
340                         uint16_t *input_row = (uint16_t*)output->get_rows()[i];
341                         float *output_row = (float*)output->get_rows()[i];
342                         for(int j = output->get_w() - 1; j >= 0; j--)
343                         {
344                                 uint16_t r = input_row[j * 4];
345                                 uint16_t g = input_row[j * 4 + 1];
346                                 uint16_t b = input_row[j * 4 + 2];
347                                 output_row[j * 4] = (float)r / 65535;
348                                 output_row[j * 4 + 1] = (float)g / 65535;
349                                 output_row[j * 4 + 2] = (float)b / 65535;
350                         }
351                 }
352         }
354         TIFFClose(stream);
355         delete unit;
357         return 0;
360 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
362 //printf("FileTIFF::write_frame 1\n");
363         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
364         int result = 0;
365         TIFF *stream;
366         tiff_unit->offset = 0;
367         tiff_unit->data = data;
368         tiff_unit->data->set_compressed_size(0);
370         stream = TIFFClientOpen("FileTIFF", 
371                 "w",
372             (void*)tiff_unit,
373             tiff_read, 
374                 tiff_write,
375             tiff_seek, 
376                 tiff_close,
377             tiff_size,
378             tiff_mmap, 
379                 tiff_unmap);
381         int components, color_model, bits, type, compression;
382         int sampleformat = SAMPLEFORMAT_UINT;
383         int bytesperrow;
384         switch(asset->tiff_cmodel)
385         {
386                 case FileTIFF::RGB_888: 
387                         components = 3;
388                         color_model = BC_RGB888;
389                         bits = 8;
390                         type = TIFF_BYTE;
391                         bytesperrow = 3 * asset->width;
392                         break;
393                 case FileTIFF::RGB_161616: 
394                         components = 3;
395                         color_model = BC_RGB_FLOAT;
396                         bits = 16;
397                         type = TIFF_SHORT;
398                         bytesperrow = 6 * asset->width;
399                         break;
400                 case FileTIFF::RGBA_8888: 
401                         components = 4;
402                         color_model = BC_RGBA8888;
403                         bits = 8;
404                         type = TIFF_BYTE;
405                         bytesperrow = 4 * asset->width;
406                         break;
407                 case FileTIFF::RGBA_16161616: 
408                         components = 4;
409                         color_model = BC_RGBA_FLOAT;
410                         bits = 16;
411                         type = TIFF_SHORT;
412                         bytesperrow = 8 * asset->width;
413                         break;
414                 case FileTIFF::RGB_FLOAT: 
415                         components = 3;
416                         color_model = BC_RGB_FLOAT;
417                         bits = 32;
418                         type = TIFF_FLOAT;
419                         sampleformat = SAMPLEFORMAT_IEEEFP;
420                         bytesperrow = 12 * asset->width;
421                         break;
422                 case FileTIFF::RGBA_FLOAT: 
423                         components = 4;
424                         color_model = BC_RGBA_FLOAT;
425                         bits = 32;
426                         type = TIFF_FLOAT;
427                         sampleformat = SAMPLEFORMAT_IEEEFP;
428                         bytesperrow = 16 * asset->width;
429                         break;
430                 default: 
431                         components = 3;
432                         color_model = BC_RGB888;
433                         bits = 8;
434                         type = TIFF_BYTE;
435                         bytesperrow = 3 * asset->width;
436                         break;
437         }
440         switch(asset->tiff_compression)
441         {
442                 case FileTIFF::LZW:
443                         compression = COMPRESSION_LZW;
444                         break;
445                 case FileTIFF::PACK_BITS:
446                         compression = COMPRESSION_PACKBITS;
447                         break;
448                 case FileTIFF::DEFLATE:
449                         compression = COMPRESSION_DEFLATE;
450                         break;
451                 case FileTIFF::JPEG:
452                         compression = COMPRESSION_JPEG;
453                         break;
454                 default:
455                         compression = COMPRESSION_NONE;
456                         break;
457         }
459         TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
460         TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
461         TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
462         TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
463         TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
464     TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
465         TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
466         TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
467         TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
468                 TIFFDefaultStripSize(stream, (uint32_t)-1));
469 //      TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
470 //              (8 * 1024) / bytesperrow);
471         TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
473         if(frame->get_color_model() == color_model)
474         {
475                 for(int i = 0; i < asset->height; i++)
476                 {
477                         TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
478                 }
479         }
480         else
481         {
482                 if(tiff_unit->temp &&
483                         tiff_unit->temp->get_color_model() != color_model)
484                 {
485                         delete tiff_unit->temp;
486                         tiff_unit->temp = 0;
487                 }
488                 if(!tiff_unit->temp)
489                 {
490                         tiff_unit->temp = new VFrame(0,
491                                 asset->width,
492                                 asset->height,
493                                 color_model);
494                 }
496                 cmodel_transfer(tiff_unit->temp->get_rows(), 
497                         frame->get_rows(),
498                         tiff_unit->temp->get_y(),
499                         tiff_unit->temp->get_u(),
500                         tiff_unit->temp->get_v(),
501                         frame->get_y(),
502                         frame->get_u(),
503                         frame->get_v(),
504                         0, 
505                         0, 
506                         frame->get_w(), 
507                         frame->get_h(),
508                         0, 
509                         0, 
510                         frame->get_w(), 
511                         frame->get_h(),
512                         frame->get_color_model(), 
513                         color_model,
514                         0,
515                         frame->get_w(),
516                         frame->get_w());
517                 for(int i = 0; i < asset->height; i++)
518                 {
519                         TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
520                 }
521         }
523         TIFFClose(stream);
525 //printf("FileTIFF::write_frame 10\n");
526         return result;
529 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
531         return new FileTIFFUnit(this, writer);
541 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
542  : FrameWriterUnit(writer)
544         this->file = file;
545         temp = 0;
548 FileTIFFUnit::~FileTIFFUnit()
550         if(temp) delete temp;
564 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
565  : BC_Window(PROGRAM_NAME ": Video Compression",
566         parent_window->get_abs_cursor_x(1),
567         parent_window->get_abs_cursor_y(1),
568         400,
569         200)
571         this->parent_window = parent_window;
572         this->asset = asset;
575 TIFFConfigVideo::~TIFFConfigVideo()
579 int TIFFConfigVideo::create_objects()
581         int x = 10, y = 10;
583         add_subwindow(new BC_Title(x, y, "Colorspace:"));
584         TIFFColorspace *menu1;
585         add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
586         menu1->create_objects();
587         y += 40;
588         add_subwindow(new BC_Title(x, y, "Compression:"));
589         TIFFCompression *menu2;
590         add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
591         menu2->create_objects();
593         add_subwindow(new BC_OKButton(this));
594         return 0;
597 int TIFFConfigVideo::close_event()
599         set_done(0);
600         return 1;
608 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
609  : BC_PopupMenu(x,
610         y,
611         w,
612         FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
614         this->gui = gui;
616 int TIFFColorspace::handle_event()
618         return 1;
620 void TIFFColorspace::create_objects()
622         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
623 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
624         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
625 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
626         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
627         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
631 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
632  : BC_MenuItem(FileTIFF::cmodel_to_str(value))
634         this->gui = gui;
635         this->value = value;
637 int TIFFColorspaceItem::handle_event()
639         gui->asset->tiff_cmodel = value;
640         return 0;
649 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
650  : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
652         this->gui = gui;
654 int TIFFCompression::handle_event()
656         return 1;
658 void TIFFCompression::create_objects()
660         add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
661 //      add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
662         add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
663 //      add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
664 //      add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
671 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
672  : BC_MenuItem(FileTIFF::compression_to_str(value))
674         this->gui = gui;
675         this->value = value;
677 int TIFFCompressionItem::handle_event()
679         gui->asset->tiff_compression = value;
680         return 0;