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')
80 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
81 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
86 if(strlen(asset->path) > 4 &&
87 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
92 if(strlen(asset->path) > 5 &&
93 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
101 const char* FileTIFF::compression_to_str(int value)
105 case FileTIFF::NONE: return "None"; break;
106 case FileTIFF::LZW: return "LZW"; break;
107 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
108 case FileTIFF::DEFLATE: return "Deflate"; break;
109 case FileTIFF::JPEG: return "JPEG"; break;
116 const char* FileTIFF::cmodel_to_str(int value)
120 case FileTIFF::GREYSCALE: return "Greyscale"; break;
121 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
122 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
123 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
124 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
125 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
126 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
134 int FileTIFF::can_copy_from(Asset *asset, int64_t position)
136 if(asset->format == FILE_TIFF_LIST ||
137 asset->format == FILE_TIFF)
145 int FileTIFF::read_frame_header(char *path)
150 if(!(stream = TIFFOpen(path, "rb")))
152 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
157 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
158 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
159 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
161 printf("FileTIFF::read_frame_header: got a %s.\n",
165 // The raw format for certain cameras deviates from TIFF here.
167 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
168 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
171 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
172 int bitspersample = 0;
173 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
174 int sampleformat = 0;
175 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
177 if(bitspersample == 8 && components == 3)
178 asset->tiff_cmodel = FileTIFF::RGB_888;
180 if(bitspersample == 16 && components == 3)
181 asset->tiff_cmodel = FileTIFF::RGB_161616;
183 if(bitspersample == 8 && components == 4)
184 asset->tiff_cmodel = FileTIFF::RGBA_8888;
186 if(bitspersample == 16 && components == 4)
187 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
189 if(bitspersample == 32 && components == 3)
190 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
192 if(bitspersample == 32 && components == 4)
193 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
195 if(bitspersample == 8 && (components == 1 || components == 0))
196 asset->tiff_cmodel = FileTIFF::GREYSCALE;
198 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
201 asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
205 int FileTIFF::colormodel_supported(int colormodel)
207 switch(asset->tiff_cmodel)
209 case FileTIFF::RGB_888: return BC_RGB888; break;
210 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
211 case FileTIFF::GREYSCALE: return BC_RGB888; break;
212 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
213 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
214 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
215 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
216 default: return BC_RGB888; break;
220 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
222 switch(asset->tiff_cmodel)
224 case FileTIFF::GREYSCALE: return BC_RGB888; break;
225 case FileTIFF::RGB_888: return BC_RGB888; break;
226 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
227 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
228 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
229 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
230 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
231 default: return BC_RGB888; break;
236 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
238 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
239 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
241 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
242 tiff_unit->offset += size;
246 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
248 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
249 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
251 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
255 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
256 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
257 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
260 tiff_unit->offset += size;
264 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
266 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
270 tiff_unit->offset = off;
273 tiff_unit->offset += off;
276 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
279 return tiff_unit->offset;
282 static int tiff_close(thandle_t ptr)
287 static toff_t tiff_size(thandle_t ptr)
289 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
290 return tiff_unit->data->get_compressed_size();
293 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
295 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
296 *pbase = tiff_unit->data->get_data();
297 *psize = tiff_unit->data->get_compressed_size();
301 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
305 int FileTIFF::read_frame(VFrame *output, VFrame *input)
307 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
312 stream = TIFFClientOpen("FileTIFF",
323 // This loads the original TIFF data into each scanline of the output frame,
324 // assuming the output scanlines are bigger than the input scanlines.
325 // Then it expands the input data in reverse to fill the row.
326 for(int i = 0; i < asset->height; i++)
328 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
330 // For the greyscale model, the output is RGB888 but the input must be expanded
331 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
333 unsigned char *row = output->get_rows()[i];
334 for(int j = output->get_w() - 1; j >= 0; j--)
336 unsigned char value = row[j];
338 row[j * 3 + 1] = value;
339 row[j * 3 + 2] = value;
342 // For the 16 bit models, the output is floating point.
344 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
346 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
347 float *output_row = (float*)output->get_rows()[i];
348 for(int j = output->get_w() - 1; j >= 0; j--)
350 uint16_t r = input_row[j * 3];
351 uint16_t g = input_row[j * 3 + 1];
352 uint16_t b = input_row[j * 3 + 2];
353 output_row[j * 3] = (float)r / 65535;
354 output_row[j * 3 + 1] = (float)g / 65535;
355 output_row[j * 3 + 2] = (float)b / 65535;
359 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
361 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
362 float *output_row = (float*)output->get_rows()[i];
363 for(int j = output->get_w() - 1; j >= 0; j--)
365 uint16_t r = input_row[j * 4];
366 uint16_t g = input_row[j * 4 + 1];
367 uint16_t b = input_row[j * 4 + 2];
368 output_row[j * 4] = (float)r / 65535;
369 output_row[j * 4 + 1] = (float)g / 65535;
370 output_row[j * 4 + 2] = (float)b / 65535;
381 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
383 //printf("FileTIFF::write_frame 1\n");
384 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
387 tiff_unit->offset = 0;
388 tiff_unit->data = data;
389 tiff_unit->data->set_compressed_size(0);
391 stream = TIFFClientOpen("FileTIFF",
402 int components, color_model, bits, compression;
403 int sampleformat = SAMPLEFORMAT_UINT;
404 //int bytesperrow, type;
405 switch(asset->tiff_cmodel)
407 case FileTIFF::RGB_888:
409 color_model = BC_RGB888;
412 //bytesperrow = 3 * asset->width;
414 case FileTIFF::RGB_161616:
416 color_model = BC_RGB_FLOAT;
419 //bytesperrow = 6 * asset->width;
421 case FileTIFF::RGBA_8888:
423 color_model = BC_RGBA8888;
426 //bytesperrow = 4 * asset->width;
428 case FileTIFF::RGBA_16161616:
430 color_model = BC_RGBA_FLOAT;
433 //bytesperrow = 8 * asset->width;
435 case FileTIFF::RGB_FLOAT:
437 color_model = BC_RGB_FLOAT;
440 sampleformat = SAMPLEFORMAT_IEEEFP;
441 //bytesperrow = 12 * asset->width;
443 case FileTIFF::RGBA_FLOAT:
445 color_model = BC_RGBA_FLOAT;
448 sampleformat = SAMPLEFORMAT_IEEEFP;
449 //bytesperrow = 16 * asset->width;
453 color_model = BC_RGB888;
456 //bytesperrow = 3 * asset->width;
461 switch(asset->tiff_compression)
464 compression = COMPRESSION_LZW;
466 case FileTIFF::PACK_BITS:
467 compression = COMPRESSION_PACKBITS;
469 case FileTIFF::DEFLATE:
470 compression = COMPRESSION_DEFLATE;
473 compression = COMPRESSION_JPEG;
476 compression = COMPRESSION_NONE;
480 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
481 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
482 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
483 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
484 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
485 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
486 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
487 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
488 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
489 TIFFDefaultStripSize(stream, (uint32_t)-1));
490 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
491 // (8 * 1024) / bytesperrow);
492 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
494 if(frame->get_color_model() == color_model)
496 for(int i = 0; i < asset->height; i++)
498 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
503 if(tiff_unit->temp &&
504 tiff_unit->temp->get_color_model() != color_model)
506 delete tiff_unit->temp;
511 tiff_unit->temp = new VFrame(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;