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
28 #include "mainerror.h"
34 FileTIFF::FileTIFF(Asset *asset, File *file)
35 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
37 asset->video_data = 1;
47 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
49 BC_WindowBase* &format_window,
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')
79 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
80 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
85 if(strlen(asset->path) > 4 &&
86 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
91 if(strlen(asset->path) > 5 &&
92 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
100 const char* FileTIFF::compression_to_str(int value)
104 case FileTIFF::NONE: return "None"; break;
105 case FileTIFF::LZW: return "LZW"; break;
106 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
107 case FileTIFF::DEFLATE: return "Deflate"; break;
108 case FileTIFF::JPEG: return "JPEG"; break;
115 const char* FileTIFF::cmodel_to_str(int value)
119 case FileTIFF::GREYSCALE: return "Greyscale"; break;
120 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
121 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
122 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
123 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
124 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
125 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
133 int FileTIFF::can_copy_from(Asset *asset, int64_t position)
135 if(asset->format == FILE_TIFF_LIST ||
136 asset->format == FILE_TIFF)
144 int FileTIFF::read_frame_header(char *path)
149 if(!(stream = TIFFOpen(path, "rb")))
151 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
156 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
157 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
158 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
160 printf("FileTIFF::read_frame_header: got a %s.\n",
164 // The raw format for certain cameras deviates from TIFF here.
166 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
167 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
170 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
171 int bitspersample = 0;
172 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
173 int sampleformat = 0;
174 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
176 if(bitspersample == 8 && components == 3)
177 asset->tiff_cmodel = FileTIFF::RGB_888;
179 if(bitspersample == 16 && components == 3)
180 asset->tiff_cmodel = FileTIFF::RGB_161616;
182 if(bitspersample == 8 && components == 4)
183 asset->tiff_cmodel = FileTIFF::RGBA_8888;
185 if(bitspersample == 16 && components == 4)
186 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
188 if(bitspersample == 32 && components == 3)
189 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
191 if(bitspersample == 32 && components == 4)
192 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
194 if(bitspersample == 8 && (components == 1 || components == 0))
195 asset->tiff_cmodel = FileTIFF::GREYSCALE;
197 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
203 int FileTIFF::colormodel_supported(int colormodel)
205 switch(asset->tiff_cmodel)
207 case FileTIFF::RGB_888: return BC_RGB888; break;
208 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
209 case FileTIFF::GREYSCALE: return BC_RGB888; break;
210 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
211 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
212 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
213 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
214 default: return BC_RGB888; break;
218 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
220 switch(asset->tiff_cmodel)
222 case FileTIFF::GREYSCALE: return BC_RGB888; break;
223 case FileTIFF::RGB_888: return BC_RGB888; break;
224 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
225 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
226 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
227 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
228 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
229 default: return BC_RGB888; break;
234 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
236 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
237 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
239 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
240 tiff_unit->offset += size;
244 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
246 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
247 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
249 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
253 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
254 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
255 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
258 tiff_unit->offset += size;
262 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
264 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
268 tiff_unit->offset = off;
271 tiff_unit->offset += off;
274 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
277 return tiff_unit->offset;
280 static int tiff_close(thandle_t ptr)
285 static toff_t tiff_size(thandle_t ptr)
287 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
288 return tiff_unit->data->get_compressed_size();
291 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
293 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
294 *pbase = tiff_unit->data->get_data();
295 *psize = tiff_unit->data->get_compressed_size();
299 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
303 int FileTIFF::read_frame(VFrame *output, VFrame *input)
305 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
310 stream = TIFFClientOpen("FileTIFF",
321 // This loads the original TIFF data into each scanline of the output frame,
322 // assuming the output scanlines are bigger than the input scanlines.
323 // Then it expands the input data in reverse to fill the row.
324 for(int i = 0; i < asset->height; i++)
326 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
328 // For the greyscale model, the output is RGB888 but the input must be expanded
329 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
331 unsigned char *row = output->get_rows()[i];
332 for(int j = output->get_w() - 1; j >= 0; j--)
334 unsigned char value = row[j];
336 row[j * 3 + 1] = value;
337 row[j * 3 + 2] = value;
340 // For the 16 bit models, the output is floating point.
342 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
344 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
345 float *output_row = (float*)output->get_rows()[i];
346 for(int j = output->get_w() - 1; j >= 0; j--)
348 uint16_t r = input_row[j * 3];
349 uint16_t g = input_row[j * 3 + 1];
350 uint16_t b = input_row[j * 3 + 2];
351 output_row[j * 3] = (float)r / 65535;
352 output_row[j * 3 + 1] = (float)g / 65535;
353 output_row[j * 3 + 2] = (float)b / 65535;
357 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
359 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
360 float *output_row = (float*)output->get_rows()[i];
361 for(int j = output->get_w() - 1; j >= 0; j--)
363 uint16_t r = input_row[j * 4];
364 uint16_t g = input_row[j * 4 + 1];
365 uint16_t b = input_row[j * 4 + 2];
366 output_row[j * 4] = (float)r / 65535;
367 output_row[j * 4 + 1] = (float)g / 65535;
368 output_row[j * 4 + 2] = (float)b / 65535;
379 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
381 //printf("FileTIFF::write_frame 1\n");
382 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
385 tiff_unit->offset = 0;
386 tiff_unit->data = data;
387 tiff_unit->data->set_compressed_size(0);
389 stream = TIFFClientOpen("FileTIFF",
400 int components, color_model, bits, compression;
401 int sampleformat = SAMPLEFORMAT_UINT;
402 //int bytesperrow, type;
403 switch(asset->tiff_cmodel)
405 case FileTIFF::RGB_888:
407 color_model = BC_RGB888;
410 //bytesperrow = 3 * asset->width;
412 case FileTIFF::RGB_161616:
414 color_model = BC_RGB_FLOAT;
417 //bytesperrow = 6 * asset->width;
419 case FileTIFF::RGBA_8888:
421 color_model = BC_RGBA8888;
424 //bytesperrow = 4 * asset->width;
426 case FileTIFF::RGBA_16161616:
428 color_model = BC_RGBA_FLOAT;
431 //bytesperrow = 8 * asset->width;
433 case FileTIFF::RGB_FLOAT:
435 color_model = BC_RGB_FLOAT;
438 sampleformat = SAMPLEFORMAT_IEEEFP;
439 //bytesperrow = 12 * asset->width;
441 case FileTIFF::RGBA_FLOAT:
443 color_model = BC_RGBA_FLOAT;
446 sampleformat = SAMPLEFORMAT_IEEEFP;
447 //bytesperrow = 16 * asset->width;
451 color_model = BC_RGB888;
454 //bytesperrow = 3 * asset->width;
459 switch(asset->tiff_compression)
462 compression = COMPRESSION_LZW;
464 case FileTIFF::PACK_BITS:
465 compression = COMPRESSION_PACKBITS;
467 case FileTIFF::DEFLATE:
468 compression = COMPRESSION_DEFLATE;
471 compression = COMPRESSION_JPEG;
474 compression = COMPRESSION_NONE;
478 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
479 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
480 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
481 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
482 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
483 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
484 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
485 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
486 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
487 TIFFDefaultStripSize(stream, (uint32_t)-1));
488 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
489 // (8 * 1024) / bytesperrow);
490 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
492 if(frame->get_color_model() == color_model)
494 for(int i = 0; i < asset->height; i++)
496 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
501 if(tiff_unit->temp &&
502 tiff_unit->temp->get_color_model() != color_model)
504 delete tiff_unit->temp;
509 tiff_unit->temp = new VFrame(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;