4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "interlacemodes.h"
29 #include "mainerror.h"
35 FileTIFF::FileTIFF(Asset *asset, File *file)
36 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
38 asset->video_data = 1;
48 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
49 Asset *asset, BC_WindowBase* &format_window,
50 int audio_options, int video_options, EDL *edl)
54 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
55 format_window = window;
56 window->create_objects();
63 int FileTIFF::check_sig(Asset *asset)
65 FILE *stream = fopen(asset->path, "rb");
70 (void)fread(test, 10, 1, stream);
73 if(test[0] == 'I' && test[1] == 'I')
75 // Reject cr2, libtiff fails with it
76 if( test[4] == 0x10 && !test[5] && !test[6] && !test[7] &&
77 test[8] == 'C' && test[9] == 'R' )
82 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
83 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
88 if(strlen(asset->path) > 4 &&
89 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
94 if(strlen(asset->path) > 5 &&
95 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
103 const char* FileTIFF::compression_to_str(int value)
106 case FileTIFF::NONE: break;
107 case FileTIFF::LZW: return "LZW";
108 case FileTIFF::PACK_BITS: return "Pack Bits";
109 case FileTIFF::DEFLATE: return "Deflate";
110 case FileTIFF::JPEG: return "JPEG";
111 case FileTIFF::PIXARFILM: return "PIXARFILM";
112 case FileTIFF::PIXARLOG: return "PIXARLOG";
113 case FileTIFF::JP2000: return "JP2000";
114 case FileTIFF::SGILOG: return "SGILOG";
115 case FileTIFF::LZMA: return "LZMA";
116 case FileTIFF::ADOBE_DEFLATE: return "Adobe Deflate";
121 const char* FileTIFF::cmodel_to_str(int value)
124 case FileTIFF::RGB_888: return "RGB-8 Bit";
125 case FileTIFF::RGB_161616: return "RGB-16 Bit";
126 case FileTIFF::RGBA_8888: return "RGBA-8 Bit";
127 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit";
128 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT";
129 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT";
130 case FileTIFF::GREYSCALE: return "Greyscale";
136 int FileTIFF::can_copy_from(Asset *asset, int64_t position)
138 if(asset->format == FILE_TIFF_LIST ||
139 asset->format == FILE_TIFF)
147 int FileTIFF::read_frame_header(char *path)
152 if(!(stream = TIFFOpen(path, "rb")))
154 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
159 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
160 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
161 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
163 printf("FileTIFF::read_frame_header: got a %s.\n",
167 // The raw format for certain cameras deviates from TIFF here.
169 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
170 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
173 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
174 int bitspersample = 0;
175 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
176 int sampleformat = 0;
177 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
179 if(bitspersample == 8 && components == 3)
180 asset->tiff_cmodel = FileTIFF::RGB_888;
182 if(bitspersample == 16 && components == 3)
183 asset->tiff_cmodel = FileTIFF::RGB_161616;
185 if(bitspersample == 8 && components == 4)
186 asset->tiff_cmodel = FileTIFF::RGBA_8888;
188 if(bitspersample == 16 && components == 4)
189 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
191 if(bitspersample == 32 && components == 3)
192 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
194 if(bitspersample == 32 && components == 4)
195 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
197 if(bitspersample == 8 && (components == 1 || components == 0))
198 asset->tiff_cmodel = FileTIFF::GREYSCALE;
200 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
203 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
207 int FileTIFF::colormodel_supported(int colormodel)
209 switch(asset->tiff_cmodel)
211 case FileTIFF::RGB_888: return BC_RGB888; break;
212 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
213 case FileTIFF::GREYSCALE: return BC_RGB888; break;
214 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
215 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
216 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
217 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
218 default: return BC_RGB888; break;
222 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
224 switch(asset->tiff_cmodel)
226 case FileTIFF::GREYSCALE: return BC_RGB888; break;
227 case FileTIFF::RGB_888: return BC_RGB888; break;
228 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
229 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
230 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
231 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
232 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
233 default: return BC_RGB888; break;
238 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
240 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
241 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
243 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
244 tiff_unit->offset += size;
248 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
250 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
251 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
253 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
257 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
258 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
259 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
262 tiff_unit->offset += size;
266 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
268 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
272 tiff_unit->offset = off;
275 tiff_unit->offset += off;
278 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
281 return tiff_unit->offset;
284 static int tiff_close(thandle_t ptr)
289 static toff_t tiff_size(thandle_t ptr)
291 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
292 return tiff_unit->data->get_compressed_size();
295 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
297 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
298 *pbase = tiff_unit->data->get_data();
299 *psize = tiff_unit->data->get_compressed_size();
303 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
307 int FileTIFF::read_frame(VFrame *output, VFrame *input)
309 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
314 stream = TIFFClientOpen("FileTIFF",
325 // This loads the original TIFF data into each scanline of the output frame,
326 // assuming the output scanlines are bigger than the input scanlines.
327 // Then it expands the input data in reverse to fill the row.
328 for(int i = 0; i < asset->height; i++)
330 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
332 // For the greyscale model, the output is RGB888 but the input must be expanded
333 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
335 unsigned char *row = output->get_rows()[i];
336 for(int j = output->get_w() - 1; j >= 0; j--)
338 unsigned char value = row[j];
340 row[j * 3 + 1] = value;
341 row[j * 3 + 2] = value;
344 // For the 16 bit models, the output is floating point.
346 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
348 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
349 float *output_row = (float*)output->get_rows()[i];
350 for(int j = output->get_w() - 1; j >= 0; j--)
352 uint16_t r = input_row[j * 3];
353 uint16_t g = input_row[j * 3 + 1];
354 uint16_t b = input_row[j * 3 + 2];
355 output_row[j * 3] = (float)r / 65535;
356 output_row[j * 3 + 1] = (float)g / 65535;
357 output_row[j * 3 + 2] = (float)b / 65535;
361 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
363 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
364 float *output_row = (float*)output->get_rows()[i];
365 for(int j = output->get_w() - 1; j >= 0; j--)
367 uint16_t r = input_row[j * 4];
368 uint16_t g = input_row[j * 4 + 1];
369 uint16_t b = input_row[j * 4 + 2];
370 output_row[j * 4] = (float)r / 65535;
371 output_row[j * 4 + 1] = (float)g / 65535;
372 output_row[j * 4 + 2] = (float)b / 65535;
383 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
385 //printf("FileTIFF::write_frame 1\n");
386 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
389 tiff_unit->offset = 0;
390 tiff_unit->data = data;
391 tiff_unit->data->set_compressed_size(0);
393 stream = TIFFClientOpen("FileTIFF",
404 int components, color_model, bits, compression;
405 int sampleformat = SAMPLEFORMAT_UINT;
406 //int bytesperrow, type;
407 switch( asset->tiff_cmodel ) {
408 case FileTIFF::RGB_888:
409 components = 3; color_model = BC_RGB888; bits = 8;
410 //type = TIFF_BYTE; bytesperrow = 3 * asset->width;
412 case FileTIFF::RGB_161616:
413 components = 3; color_model = BC_RGB_FLOAT; bits = 16;
414 //type = TIFF_SHORT; bytesperrow = 6 * asset->width;
416 case FileTIFF::RGBA_8888:
417 components = 4; color_model = BC_RGBA8888; bits = 8;
418 //type = TIFF_BYTE; bytesperrow = 4 * asset->width;
420 case FileTIFF::RGBA_16161616:
421 components = 4; color_model = BC_RGBA_FLOAT; bits = 16;
422 //type = TIFF_SHORT; bytesperrow = 8 * asset->width;
424 case FileTIFF::RGB_FLOAT:
425 components = 3; color_model = BC_RGB_FLOAT; bits = 32;
426 //type = TIFF_FLOAT; bytesperrow = 12 * asset->width;
427 sampleformat = SAMPLEFORMAT_IEEEFP;
429 case FileTIFF::RGBA_FLOAT:
430 components = 4; color_model = BC_RGBA_FLOAT; bits = 32;
431 //type = TIFF_FLOAT; bytesperrow = 16 * asset->width;
432 sampleformat = SAMPLEFORMAT_IEEEFP;
435 components = 3; color_model = BC_RGB888; bits = 8;
436 //type = TIFF_BYTE; bytesperrow = 3 * asset->width;
440 switch(asset->tiff_compression) {
441 case FileTIFF::LZW: compression = COMPRESSION_LZW; break;
442 case FileTIFF::PACK_BITS: compression = COMPRESSION_PACKBITS; break;
443 case FileTIFF::DEFLATE: compression = COMPRESSION_DEFLATE; break;
444 case FileTIFF::JPEG: compression = COMPRESSION_JPEG; break;
445 case FileTIFF::PIXARFILM: compression = COMPRESSION_PIXARFILM; break;
446 case FileTIFF::PIXARLOG: compression = COMPRESSION_PIXARLOG; break;
447 case FileTIFF::JP2000: compression = COMPRESSION_JP2000; break;
448 case FileTIFF::LZMA: compression = COMPRESSION_LZMA; break;
449 case FileTIFF::SGILOG: compression = COMPRESSION_SGILOG; break;
450 case FileTIFF::ADOBE_DEFLATE: compression = ADOBE_DEFLATE; break;
451 default: compression = COMPRESSION_NONE; break;
454 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
455 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
456 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
457 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
458 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
459 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
460 if( components == 4 ) {
461 uint16 out[1]; out[0] = EXTRASAMPLE_UNASSALPHA;
462 TIFFSetField(stream, TIFFTAG_EXTRASAMPLES, 1, &out);
464 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
465 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
466 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
467 TIFFDefaultStripSize(stream, (uint32_t)-1));
468 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
469 // (8 * 1024) / bytesperrow);
470 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
472 if( frame->get_color_model() == color_model ) {
473 for( int y=0; y<asset->height; ++y )
474 TIFFWriteScanline(stream, frame->get_rows()[y], y, 0);
477 if( tiff_unit->temp &&
478 tiff_unit->temp->get_color_model() != color_model ) {
479 delete tiff_unit->temp; tiff_unit->temp = 0;
481 if( !tiff_unit->temp )
482 tiff_unit->temp = new VFrame(asset->width, asset->height, color_model, 0);
484 BC_CModels::transfer(tiff_unit->temp->get_rows(), frame->get_rows(),
485 tiff_unit->temp->get_y(), tiff_unit->temp->get_u(), tiff_unit->temp->get_v(),
486 frame->get_y(), frame->get_u(), frame->get_v(),
487 0, 0, frame->get_w(), frame->get_h(),
488 0, 0, frame->get_w(), frame->get_h(),
489 frame->get_color_model(), color_model,
490 0, frame->get_w(), frame->get_w());
491 for( int y=0; y<asset->height; ++y )
492 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[y], y, 0);
497 //printf("FileTIFF::write_frame 10\n");
501 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
503 return new FileTIFFUnit(this, writer);
507 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
508 : FrameWriterUnit(writer)
514 FileTIFFUnit::~FileTIFFUnit()
516 if(temp) delete temp;
530 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
531 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
532 parent_window->get_abs_cursor_x(1),
533 parent_window->get_abs_cursor_y(1),
536 this->parent_window = parent_window;
538 // *** CONTEXT_HELP ***
539 context_help_set_keyword("Single File Rendering");
542 TIFFConfigVideo::~TIFFConfigVideo()
546 void TIFFConfigVideo::create_objects()
548 lock_window("TIFFConfigVideo::create_objects");
549 int x = xS(10), y = yS(10);
551 add_subwindow(new BC_Title(x, y, _("Colorspace:")));
552 TIFFColorspace *menu1;
553 add_subwindow(menu1 = new TIFFColorspace(this, x + xS(150), y, xS(200)));
554 menu1->create_objects();
556 add_subwindow(new BC_Title(x, y, _("Compression:")));
557 TIFFCompression *menu2;
558 add_subwindow(menu2 = new TIFFCompression(this, x + xS(150), y, xS(200)));
559 menu2->create_objects();
561 add_subwindow(new BC_OKButton(this));
566 int TIFFConfigVideo::close_event()
573 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
574 : BC_PopupMenu(x, y, w, FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
578 int TIFFColorspace::handle_event()
582 void TIFFColorspace::create_objects()
584 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
585 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
586 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
587 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
588 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
589 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
593 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
594 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
599 int TIFFColorspaceItem::handle_event()
601 gui->asset->tiff_cmodel = value;
611 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
612 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
616 int TIFFCompression::handle_event()
620 void TIFFCompression::create_objects()
622 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
623 add_item(new TIFFCompressionItem(gui, FileTIFF::LZW)); // patent expired in 2004 ?
624 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
625 add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE)); // works!
626 // add_item(new TIFFCompressionItem(gui, FileTIFF::SGILOG)); scanline encoding not implemented
627 add_item(new TIFFCompressionItem(gui, FileTIFF::LZMA)); // works, a bit new for data exchange?
628 // add_item(new TIFFCompressionItem(gui, FileTIFF::ADOBE_DEFLATE)); scanline encoding not implemented
629 // add_item(new TIFFCompressionItem(gui, FileTIFF::PIXARFILM)); not supported for scanline encoding
630 // add_item(new TIFFCompressionItem(gui, FileTIFF::PIXARLOG)); only 8 bit ?
631 // add_item(new TIFFCompressionItem(gui, FileTIFF::JP2000)); doesn't support scanline encoding
632 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
636 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
637 : BC_MenuItem(FileTIFF::compression_to_str(value))
642 int TIFFCompressionItem::handle_event()
644 gui->asset->tiff_compression = value;