4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5 * Copyright (C) 2003-2016 Cinelerra CV contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "interlacemodes.h"
30 #include "mainerror.h"
36 FileTIFF::FileTIFF(Asset *asset, File *file)
37 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
39 asset->video_data = 1;
49 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
50 Asset *asset, BC_WindowBase* &format_window,
51 int audio_options, int video_options, EDL *edl)
55 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
56 format_window = window;
57 window->create_objects();
64 int FileTIFF::check_sig(Asset *asset)
66 FILE *stream = fopen(asset->path, "rb");
71 (void)fread(test, 10, 1, stream);
74 if(test[0] == 'I' && test[1] == 'I')
76 // Reject cr2, libtiff fails with it
77 if( test[4] == 0x10 && !test[5] && !test[6] && !test[7] &&
78 test[8] == 'C' && test[9] == 'R' )
83 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
84 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
89 if(strlen(asset->path) > 4 &&
90 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
95 if(strlen(asset->path) > 5 &&
96 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
104 const char* FileTIFF::compression_to_str(int value)
107 case FileTIFF::NONE: break;
108 case FileTIFF::LZW: return "LZW";
109 case FileTIFF::PACK_BITS: return "Pack Bits";
110 case FileTIFF::DEFLATE: return "Deflate";
111 case FileTIFF::JPEG: return "JPEG";
112 case FileTIFF::PIXARFILM: return "PIXARFILM";
113 case FileTIFF::PIXARLOG: return "PIXARLOG";
114 case FileTIFF::JP2000: return "JP2000";
115 case FileTIFF::SGILOG: return "SGILOG";
116 case FileTIFF::LZMA: return "LZMA";
117 case FileTIFF::ADOBE_DEFLATE: return "Adobe Deflate";
122 const char* FileTIFF::cmodel_to_str(int value)
125 case FileTIFF::RGB_888: return "RGB-8 Bit";
126 case FileTIFF::RGB_161616: return "RGB-16 Bit";
127 case FileTIFF::RGBA_8888: return "RGBA-8 Bit";
128 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit";
129 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT";
130 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT";
131 case FileTIFF::GREYSCALE: return "Greyscale";
137 int FileTIFF::can_copy_from(Asset *asset, int64_t position)
139 if(asset->format == FILE_TIFF_LIST ||
140 asset->format == FILE_TIFF)
148 int FileTIFF::read_frame_header(char *path)
153 if(!(stream = TIFFOpen(path, "rb")))
155 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
160 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
161 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
162 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
164 printf("FileTIFF::read_frame_header: got a %s.\n",
168 // The raw format for certain cameras deviates from TIFF here.
170 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
171 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
174 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
175 int bitspersample = 0;
176 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
177 int sampleformat = 0;
178 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
180 if(bitspersample == 8 && components == 3)
181 asset->tiff_cmodel = FileTIFF::RGB_888;
183 if(bitspersample == 16 && components == 3)
184 asset->tiff_cmodel = FileTIFF::RGB_161616;
186 if(bitspersample == 8 && components == 4)
187 asset->tiff_cmodel = FileTIFF::RGBA_8888;
189 if(bitspersample == 16 && components == 4)
190 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
192 if(bitspersample == 32 && components == 3)
193 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
195 if(bitspersample == 32 && components == 4)
196 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
198 if(bitspersample == 8 && (components == 1 || components == 0))
199 asset->tiff_cmodel = FileTIFF::GREYSCALE;
201 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
204 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
208 int FileTIFF::colormodel_supported(int colormodel)
210 switch(asset->tiff_cmodel)
212 case FileTIFF::RGB_888: return BC_RGB888; break;
213 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
214 case FileTIFF::GREYSCALE: return BC_RGB888; break;
215 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
216 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
217 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
218 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
219 default: return BC_RGB888; break;
223 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
225 switch(asset->tiff_cmodel)
227 case FileTIFF::GREYSCALE: return BC_RGB888; break;
228 case FileTIFF::RGB_888: return BC_RGB888; break;
229 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
230 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
231 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
232 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
233 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
234 default: return BC_RGB888; break;
239 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
241 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
242 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
244 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
245 tiff_unit->offset += size;
249 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
251 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
252 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
254 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
258 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
259 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
260 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
263 tiff_unit->offset += size;
267 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
269 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
273 tiff_unit->offset = off;
276 tiff_unit->offset += off;
279 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
282 return tiff_unit->offset;
285 static int tiff_close(thandle_t ptr)
290 static toff_t tiff_size(thandle_t ptr)
292 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
293 return tiff_unit->data->get_compressed_size();
296 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
298 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
299 *pbase = tiff_unit->data->get_data();
300 *psize = tiff_unit->data->get_compressed_size();
304 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
308 int FileTIFF::read_frame(VFrame *output, VFrame *input)
310 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
315 stream = TIFFClientOpen("FileTIFF",
329 // This loads the original TIFF data into each scanline of the output frame,
330 // assuming the output scanlines are bigger than the input scanlines.
331 // Then it expands the input data in reverse to fill the row.
332 for(int i = 0; i < asset->height; i++)
334 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
336 // For the greyscale model, the output is RGB888 but the input must be expanded
337 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
339 unsigned char *row = output->get_rows()[i];
340 for(int j = output->get_w() - 1; j >= 0; j--)
342 unsigned char value = row[j];
344 row[j * 3 + 1] = value;
345 row[j * 3 + 2] = value;
348 // For the 16 bit models, the output is floating point.
350 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
352 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
353 float *output_row = (float*)output->get_rows()[i];
354 for(int j = output->get_w() - 1; j >= 0; j--)
356 uint16_t r = input_row[j * 3];
357 uint16_t g = input_row[j * 3 + 1];
358 uint16_t b = input_row[j * 3 + 2];
359 output_row[j * 3] = (float)r / 65535;
360 output_row[j * 3 + 1] = (float)g / 65535;
361 output_row[j * 3 + 2] = (float)b / 65535;
365 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
367 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
368 float *output_row = (float*)output->get_rows()[i];
369 for(int j = output->get_w() - 1; j >= 0; j--)
371 uint16_t r = input_row[j * 4];
372 uint16_t g = input_row[j * 4 + 1];
373 uint16_t b = input_row[j * 4 + 2];
374 output_row[j * 4] = (float)r / 65535;
375 output_row[j * 4 + 1] = (float)g / 65535;
376 output_row[j * 4 + 2] = (float)b / 65535;
387 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
389 //printf("FileTIFF::write_frame 1\n");
390 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
393 tiff_unit->offset = 0;
394 tiff_unit->data = data;
395 tiff_unit->data->set_compressed_size(0);
397 stream = TIFFClientOpen("FileTIFF",
408 int components, color_model, bits, compression;
409 int sampleformat = SAMPLEFORMAT_UINT;
410 //int bytesperrow, type;
411 switch( asset->tiff_cmodel ) {
412 case FileTIFF::RGB_888:
413 components = 3; color_model = BC_RGB888; bits = 8;
414 //type = TIFF_BYTE; bytesperrow = 3 * asset->width;
416 case FileTIFF::RGB_161616:
417 components = 3; color_model = BC_RGB_FLOAT; bits = 16;
418 //type = TIFF_SHORT; bytesperrow = 6 * asset->width;
420 case FileTIFF::RGBA_8888:
421 components = 4; color_model = BC_RGBA8888; bits = 8;
422 //type = TIFF_BYTE; bytesperrow = 4 * asset->width;
424 case FileTIFF::RGBA_16161616:
425 components = 4; color_model = BC_RGBA_FLOAT; bits = 16;
426 //type = TIFF_SHORT; bytesperrow = 8 * asset->width;
428 case FileTIFF::RGB_FLOAT:
429 components = 3; color_model = BC_RGB_FLOAT; bits = 32;
430 //type = TIFF_FLOAT; bytesperrow = 12 * asset->width;
431 sampleformat = SAMPLEFORMAT_IEEEFP;
433 case FileTIFF::RGBA_FLOAT:
434 components = 4; color_model = BC_RGBA_FLOAT; bits = 32;
435 //type = TIFF_FLOAT; bytesperrow = 16 * asset->width;
436 sampleformat = SAMPLEFORMAT_IEEEFP;
439 components = 3; color_model = BC_RGB888; bits = 8;
440 //type = TIFF_BYTE; bytesperrow = 3 * asset->width;
444 switch(asset->tiff_compression) {
445 case FileTIFF::LZW: compression = COMPRESSION_LZW; break;
446 case FileTIFF::PACK_BITS: compression = COMPRESSION_PACKBITS; break;
447 case FileTIFF::DEFLATE: compression = COMPRESSION_DEFLATE; break;
448 case FileTIFF::JPEG: compression = COMPRESSION_JPEG; break;
449 case FileTIFF::PIXARFILM: compression = COMPRESSION_PIXARFILM; break;
450 case FileTIFF::PIXARLOG: compression = COMPRESSION_PIXARLOG; break;
451 case FileTIFF::JP2000: compression = COMPRESSION_JP2000; break;
452 case FileTIFF::LZMA: compression = COMPRESSION_LZMA; break;
453 case FileTIFF::SGILOG: compression = COMPRESSION_SGILOG; break;
454 case FileTIFF::ADOBE_DEFLATE: compression = ADOBE_DEFLATE; break;
455 default: compression = COMPRESSION_NONE; break;
458 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
459 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
460 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
461 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
462 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
463 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
464 if( components == 4 ) {
465 uint16 out[1]; out[0] = EXTRASAMPLE_UNASSALPHA;
466 TIFFSetField(stream, TIFFTAG_EXTRASAMPLES, 1, &out);
468 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
469 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
470 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
471 TIFFDefaultStripSize(stream, (uint32_t)-1));
472 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
473 // (8 * 1024) / bytesperrow);
474 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
476 if( frame->get_color_model() == color_model ) {
477 for( int y=0; y<asset->height; ++y )
478 TIFFWriteScanline(stream, frame->get_rows()[y], y, 0);
481 if( tiff_unit->temp &&
482 tiff_unit->temp->get_color_model() != color_model ) {
483 delete tiff_unit->temp; tiff_unit->temp = 0;
485 if( !tiff_unit->temp )
486 tiff_unit->temp = new VFrame(asset->width, asset->height, color_model, 0);
488 BC_CModels::transfer(tiff_unit->temp->get_rows(), frame->get_rows(),
489 tiff_unit->temp->get_y(), tiff_unit->temp->get_u(), tiff_unit->temp->get_v(),
490 frame->get_y(), frame->get_u(), frame->get_v(),
491 0, 0, frame->get_w(), frame->get_h(),
492 0, 0, frame->get_w(), frame->get_h(),
493 frame->get_color_model(), color_model,
494 0, frame->get_w(), frame->get_w());
495 for( int y=0; y<asset->height; ++y )
496 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[y], y, 0);
501 //printf("FileTIFF::write_frame 10\n");
505 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
507 return new FileTIFFUnit(this, writer);
511 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
512 : FrameWriterUnit(writer)
518 FileTIFFUnit::~FileTIFFUnit()
520 if(temp) delete temp;
534 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
535 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
536 parent_window->get_abs_cursor_x(1),
537 parent_window->get_abs_cursor_y(1),
540 this->parent_window = parent_window;
542 // *** CONTEXT_HELP ***
543 context_help_set_keyword("Single File Rendering");
546 TIFFConfigVideo::~TIFFConfigVideo()
550 void TIFFConfigVideo::create_objects()
552 lock_window("TIFFConfigVideo::create_objects");
553 int x = xS(10), y = yS(10);
555 add_subwindow(new BC_Title(x, y, _("Colorspace:")));
556 TIFFColorspace *menu1;
557 add_subwindow(menu1 = new TIFFColorspace(this, x + xS(150), y, xS(200)));
558 menu1->create_objects();
560 add_subwindow(new BC_Title(x, y, _("Compression:")));
561 TIFFCompression *menu2;
562 add_subwindow(menu2 = new TIFFCompression(this, x + xS(150), y, xS(200)));
563 menu2->create_objects();
565 add_subwindow(new BC_OKButton(this));
570 int TIFFConfigVideo::close_event()
577 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
578 : BC_PopupMenu(x, y, w, FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
582 int TIFFColorspace::handle_event()
586 void TIFFColorspace::create_objects()
588 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
589 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
590 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
591 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
592 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
593 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
597 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
598 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
603 int TIFFColorspaceItem::handle_event()
605 gui->asset->tiff_cmodel = value;
615 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
616 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
620 int TIFFCompression::handle_event()
624 void TIFFCompression::create_objects()
626 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
627 add_item(new TIFFCompressionItem(gui, FileTIFF::LZW)); // patent expired in 2004 ?
628 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
629 add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE)); // works!
630 // add_item(new TIFFCompressionItem(gui, FileTIFF::SGILOG)); scanline encoding not implemented
631 add_item(new TIFFCompressionItem(gui, FileTIFF::LZMA)); // works, a bit new for data exchange?
632 // add_item(new TIFFCompressionItem(gui, FileTIFF::ADOBE_DEFLATE)); scanline encoding not implemented
633 // add_item(new TIFFCompressionItem(gui, FileTIFF::PIXARFILM)); not supported for scanline encoding
634 // add_item(new TIFFCompressionItem(gui, FileTIFF::PIXARLOG)); only 8 bit ?
635 // add_item(new TIFFCompressionItem(gui, FileTIFF::JP2000)); doesn't support scanline encoding
636 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
640 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
641 : BC_MenuItem(FileTIFF::compression_to_str(value))
646 int TIFFCompressionItem::handle_event()
648 gui->asset->tiff_compression = value;