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)
107 case FileTIFF::NONE: return "None"; break;
108 case FileTIFF::LZW: return "LZW"; break;
109 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
110 case FileTIFF::DEFLATE: return "Deflate"; break;
111 case FileTIFF::JPEG: return "JPEG"; break;
118 const char* FileTIFF::cmodel_to_str(int value)
122 case FileTIFF::GREYSCALE: return "Greyscale"; break;
123 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
124 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
125 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
126 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
127 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
128 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
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)
409 case FileTIFF::RGB_888:
411 color_model = BC_RGB888;
414 //bytesperrow = 3 * asset->width;
416 case FileTIFF::RGB_161616:
418 color_model = BC_RGB_FLOAT;
421 //bytesperrow = 6 * asset->width;
423 case FileTIFF::RGBA_8888:
425 color_model = BC_RGBA8888;
428 //bytesperrow = 4 * asset->width;
430 case FileTIFF::RGBA_16161616:
432 color_model = BC_RGBA_FLOAT;
435 //bytesperrow = 8 * asset->width;
437 case FileTIFF::RGB_FLOAT:
439 color_model = BC_RGB_FLOAT;
442 sampleformat = SAMPLEFORMAT_IEEEFP;
443 //bytesperrow = 12 * asset->width;
445 case FileTIFF::RGBA_FLOAT:
447 color_model = BC_RGBA_FLOAT;
450 sampleformat = SAMPLEFORMAT_IEEEFP;
451 //bytesperrow = 16 * asset->width;
455 color_model = BC_RGB888;
458 //bytesperrow = 3 * asset->width;
463 switch(asset->tiff_compression)
466 compression = COMPRESSION_LZW;
468 case FileTIFF::PACK_BITS:
469 compression = COMPRESSION_PACKBITS;
471 case FileTIFF::DEFLATE:
472 compression = COMPRESSION_DEFLATE;
475 compression = COMPRESSION_JPEG;
478 compression = COMPRESSION_NONE;
482 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
483 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
484 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
485 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
486 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
487 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
488 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
489 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
490 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
491 TIFFDefaultStripSize(stream, (uint32_t)-1));
492 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
493 // (8 * 1024) / bytesperrow);
494 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
496 if(frame->get_color_model() == color_model)
498 for(int i = 0; i < asset->height; i++)
500 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
505 if(tiff_unit->temp &&
506 tiff_unit->temp->get_color_model() != color_model)
508 delete tiff_unit->temp;
514 new VFrame(asset->width, asset->height, color_model, 0);
517 BC_CModels::transfer(tiff_unit->temp->get_rows(),
519 tiff_unit->temp->get_y(),
520 tiff_unit->temp->get_u(),
521 tiff_unit->temp->get_v(),
533 frame->get_color_model(),
538 for(int i = 0; i < asset->height; i++)
540 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
546 //printf("FileTIFF::write_frame 10\n");
550 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
552 return new FileTIFFUnit(this, writer);
562 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
563 : FrameWriterUnit(writer)
569 FileTIFFUnit::~FileTIFFUnit()
571 if(temp) delete temp;
585 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
586 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
587 parent_window->get_abs_cursor_x(1),
588 parent_window->get_abs_cursor_y(1),
592 this->parent_window = parent_window;
596 TIFFConfigVideo::~TIFFConfigVideo()
600 void TIFFConfigVideo::create_objects()
602 lock_window("TIFFConfigVideo::create_objects");
605 add_subwindow(new BC_Title(x, y, _("Colorspace:")));
606 TIFFColorspace *menu1;
607 add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
608 menu1->create_objects();
610 add_subwindow(new BC_Title(x, y, _("Compression:")));
611 TIFFCompression *menu2;
612 add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
613 menu2->create_objects();
615 add_subwindow(new BC_OKButton(this));
620 int TIFFConfigVideo::close_event()
631 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
635 FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
639 int TIFFColorspace::handle_event()
643 void TIFFColorspace::create_objects()
645 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
646 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
647 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
648 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
649 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
650 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
654 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
655 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
660 int TIFFColorspaceItem::handle_event()
662 gui->asset->tiff_cmodel = value;
672 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
673 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
677 int TIFFCompression::handle_event()
681 void TIFFCompression::create_objects()
683 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
684 // add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
685 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
686 // add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
687 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
694 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
695 : BC_MenuItem(FileTIFF::compression_to_str(value))
700 int TIFFCompressionItem::handle_event()
702 gui->asset->tiff_compression = value;