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,
50 BC_WindowBase* &format_window,
56 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
57 format_window = window;
58 window->create_objects();
65 int FileTIFF::check_sig(Asset *asset)
67 FILE *stream = fopen(asset->path, "rb");
72 (void)fread(test, 10, 1, stream);
75 if(test[0] == 'I' && test[1] == 'I')
77 // Reject cr2, libtiff fails with it
78 if( test[4] == 0x10 && !test[5] && !test[6] && !test[7] &&
79 test[8] == 'C' && test[9] == 'R' )
84 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
85 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
90 if(strlen(asset->path) > 4 &&
91 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
96 if(strlen(asset->path) > 5 &&
97 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
105 const char* FileTIFF::compression_to_str(int value)
109 case FileTIFF::NONE: return "None"; break;
110 case FileTIFF::LZW: return "LZW"; break;
111 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
112 case FileTIFF::DEFLATE: return "Deflate"; break;
113 case FileTIFF::JPEG: return "JPEG"; break;
120 const char* FileTIFF::cmodel_to_str(int value)
124 case FileTIFF::GREYSCALE: return "Greyscale"; break;
125 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
126 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
127 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
128 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
129 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
130 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
138 int FileTIFF::can_copy_from(Asset *asset, int64_t position)
140 if(asset->format == FILE_TIFF_LIST ||
141 asset->format == FILE_TIFF)
149 int FileTIFF::read_frame_header(char *path)
154 if(!(stream = TIFFOpen(path, "rb")))
156 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
161 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
162 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
163 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
165 printf("FileTIFF::read_frame_header: got a %s.\n",
169 // The raw format for certain cameras deviates from TIFF here.
171 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
172 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
175 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
176 int bitspersample = 0;
177 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
178 int sampleformat = 0;
179 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
181 if(bitspersample == 8 && components == 3)
182 asset->tiff_cmodel = FileTIFF::RGB_888;
184 if(bitspersample == 16 && components == 3)
185 asset->tiff_cmodel = FileTIFF::RGB_161616;
187 if(bitspersample == 8 && components == 4)
188 asset->tiff_cmodel = FileTIFF::RGBA_8888;
190 if(bitspersample == 16 && components == 4)
191 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
193 if(bitspersample == 32 && components == 3)
194 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
196 if(bitspersample == 32 && components == 4)
197 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
199 if(bitspersample == 8 && (components == 1 || components == 0))
200 asset->tiff_cmodel = FileTIFF::GREYSCALE;
202 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
205 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
209 int FileTIFF::colormodel_supported(int colormodel)
211 switch(asset->tiff_cmodel)
213 case FileTIFF::RGB_888: return BC_RGB888; break;
214 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
215 case FileTIFF::GREYSCALE: return BC_RGB888; break;
216 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
217 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
218 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
219 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
220 default: return BC_RGB888; break;
224 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
226 switch(asset->tiff_cmodel)
228 case FileTIFF::GREYSCALE: return BC_RGB888; break;
229 case FileTIFF::RGB_888: return BC_RGB888; break;
230 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
231 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
232 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
233 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
234 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
235 default: return BC_RGB888; break;
240 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
242 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
243 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
245 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
246 tiff_unit->offset += size;
250 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
252 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
253 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
255 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
259 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
260 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
261 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
264 tiff_unit->offset += size;
268 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
270 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
274 tiff_unit->offset = off;
277 tiff_unit->offset += off;
280 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
283 return tiff_unit->offset;
286 static int tiff_close(thandle_t ptr)
291 static toff_t tiff_size(thandle_t ptr)
293 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
294 return tiff_unit->data->get_compressed_size();
297 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
299 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
300 *pbase = tiff_unit->data->get_data();
301 *psize = tiff_unit->data->get_compressed_size();
305 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
309 int FileTIFF::read_frame(VFrame *output, VFrame *input)
311 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
316 stream = TIFFClientOpen("FileTIFF",
327 // This loads the original TIFF data into each scanline of the output frame,
328 // assuming the output scanlines are bigger than the input scanlines.
329 // Then it expands the input data in reverse to fill the row.
330 for(int i = 0; i < asset->height; i++)
332 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
334 // For the greyscale model, the output is RGB888 but the input must be expanded
335 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
337 unsigned char *row = output->get_rows()[i];
338 for(int j = output->get_w() - 1; j >= 0; j--)
340 unsigned char value = row[j];
342 row[j * 3 + 1] = value;
343 row[j * 3 + 2] = value;
346 // For the 16 bit models, the output is floating point.
348 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
350 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
351 float *output_row = (float*)output->get_rows()[i];
352 for(int j = output->get_w() - 1; j >= 0; j--)
354 uint16_t r = input_row[j * 3];
355 uint16_t g = input_row[j * 3 + 1];
356 uint16_t b = input_row[j * 3 + 2];
357 output_row[j * 3] = (float)r / 65535;
358 output_row[j * 3 + 1] = (float)g / 65535;
359 output_row[j * 3 + 2] = (float)b / 65535;
363 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
365 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
366 float *output_row = (float*)output->get_rows()[i];
367 for(int j = output->get_w() - 1; j >= 0; j--)
369 uint16_t r = input_row[j * 4];
370 uint16_t g = input_row[j * 4 + 1];
371 uint16_t b = input_row[j * 4 + 2];
372 output_row[j * 4] = (float)r / 65535;
373 output_row[j * 4 + 1] = (float)g / 65535;
374 output_row[j * 4 + 2] = (float)b / 65535;
385 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
387 //printf("FileTIFF::write_frame 1\n");
388 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
391 tiff_unit->offset = 0;
392 tiff_unit->data = data;
393 tiff_unit->data->set_compressed_size(0);
395 stream = TIFFClientOpen("FileTIFF",
406 int components, color_model, bits, compression;
407 int sampleformat = SAMPLEFORMAT_UINT;
408 //int bytesperrow, type;
409 switch(asset->tiff_cmodel)
411 case FileTIFF::RGB_888:
413 color_model = BC_RGB888;
416 //bytesperrow = 3 * asset->width;
418 case FileTIFF::RGB_161616:
420 color_model = BC_RGB_FLOAT;
423 //bytesperrow = 6 * asset->width;
425 case FileTIFF::RGBA_8888:
427 color_model = BC_RGBA8888;
430 //bytesperrow = 4 * asset->width;
432 case FileTIFF::RGBA_16161616:
434 color_model = BC_RGBA_FLOAT;
437 //bytesperrow = 8 * asset->width;
439 case FileTIFF::RGB_FLOAT:
441 color_model = BC_RGB_FLOAT;
444 sampleformat = SAMPLEFORMAT_IEEEFP;
445 //bytesperrow = 12 * asset->width;
447 case FileTIFF::RGBA_FLOAT:
449 color_model = BC_RGBA_FLOAT;
452 sampleformat = SAMPLEFORMAT_IEEEFP;
453 //bytesperrow = 16 * asset->width;
457 color_model = BC_RGB888;
460 //bytesperrow = 3 * asset->width;
465 switch(asset->tiff_compression)
468 compression = COMPRESSION_LZW;
470 case FileTIFF::PACK_BITS:
471 compression = COMPRESSION_PACKBITS;
473 case FileTIFF::DEFLATE:
474 compression = COMPRESSION_DEFLATE;
477 compression = COMPRESSION_JPEG;
480 compression = COMPRESSION_NONE;
484 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
485 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
486 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
487 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
488 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
489 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
490 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
491 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
492 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
493 TIFFDefaultStripSize(stream, (uint32_t)-1));
494 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
495 // (8 * 1024) / bytesperrow);
496 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
498 if(frame->get_color_model() == color_model)
500 for(int i = 0; i < asset->height; i++)
502 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
507 if(tiff_unit->temp &&
508 tiff_unit->temp->get_color_model() != color_model)
510 delete tiff_unit->temp;
516 new VFrame(asset->width, asset->height, color_model, 0);
519 BC_CModels::transfer(tiff_unit->temp->get_rows(),
521 tiff_unit->temp->get_y(),
522 tiff_unit->temp->get_u(),
523 tiff_unit->temp->get_v(),
535 frame->get_color_model(),
540 for(int i = 0; i < asset->height; i++)
542 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
548 //printf("FileTIFF::write_frame 10\n");
552 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
554 return new FileTIFFUnit(this, writer);
564 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
565 : FrameWriterUnit(writer)
571 FileTIFFUnit::~FileTIFFUnit()
573 if(temp) delete temp;
587 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
588 : BC_Window(_(PROGRAM_NAME ": Video Compression"),
589 parent_window->get_abs_cursor_x(1),
590 parent_window->get_abs_cursor_y(1),
594 this->parent_window = parent_window;
598 TIFFConfigVideo::~TIFFConfigVideo()
602 void TIFFConfigVideo::create_objects()
604 lock_window("TIFFConfigVideo::create_objects()");
607 add_subwindow(new BC_Title(x, y, _("Colorspace:")));
608 TIFFColorspace *menu1;
609 add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
610 menu1->create_objects();
612 add_subwindow(new BC_Title(x, y, _("Compression:")));
613 TIFFCompression *menu2;
614 add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
615 menu2->create_objects();
617 add_subwindow(new BC_OKButton(this));
622 int TIFFConfigVideo::close_event()
633 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
637 FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
641 int TIFFColorspace::handle_event()
645 void TIFFColorspace::create_objects()
647 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
648 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
649 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
650 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
651 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
652 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
656 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
657 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
662 int TIFFColorspaceItem::handle_event()
664 gui->asset->tiff_cmodel = value;
674 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
675 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
679 int TIFFCompression::handle_event()
683 void TIFFCompression::create_objects()
685 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
686 // add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
687 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
688 // add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
689 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
696 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
697 : BC_MenuItem(FileTIFF::compression_to_str(value))
702 int TIFFCompressionItem::handle_event()
704 gui->asset->tiff_compression = value;