5 #include "interlacemodes.h"
14 FileTIFF::FileTIFF(Asset *asset, File *file)
15 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
17 asset->video_data = 1;
27 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
29 BC_WindowBase* &format_window,
35 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
36 format_window = window;
37 window->create_objects();
44 int FileTIFF::check_sig(Asset *asset)
46 FILE *stream = fopen(asset->path, "rb");
51 fread(test, 10, 1, stream);
54 if(test[0] == 'I' && test[1] == 'I')
59 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
60 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
65 if(strlen(asset->path) > 4 &&
66 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
71 if(strlen(asset->path) > 5 &&
72 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
80 char* FileTIFF::compression_to_str(int value)
84 case FileTIFF::NONE: return "None"; break;
85 case FileTIFF::LZW: return "LZW"; break;
86 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
87 case FileTIFF::DEFLATE: return "Deflate"; break;
88 case FileTIFF::JPEG: return "JPEG"; break;
95 char* FileTIFF::cmodel_to_str(int value)
99 case FileTIFF::GREYSCALE: return "Greyscale"; break;
100 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
101 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
102 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
103 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
104 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
105 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
113 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
115 if(edit->asset->format == FILE_TIFF_LIST ||
116 edit->asset->format == FILE_TIFF)
124 int FileTIFF::read_frame_header(char *path)
129 if(!(stream = TIFFOpen(path, "rb")))
131 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
136 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
137 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
138 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
140 printf("FileTIFF::read_frame_header: got a %s.\n",
144 // The raw format for certain cameras deviates from TIFF here.
146 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
147 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
150 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
151 int bitspersample = 0;
152 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
153 int sampleformat = 0;
154 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
156 if(bitspersample == 8 && components == 3)
157 asset->tiff_cmodel = FileTIFF::RGB_888;
159 if(bitspersample == 16 && components == 3)
160 asset->tiff_cmodel = FileTIFF::RGB_161616;
162 if(bitspersample == 8 && components == 4)
163 asset->tiff_cmodel = FileTIFF::RGBA_8888;
165 if(bitspersample == 16 && components == 4)
166 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
168 if(bitspersample == 32 && components == 3)
169 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
171 if(bitspersample == 32 && components == 4)
172 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
174 if(bitspersample == 8 && (components == 1 || components == 0))
175 asset->tiff_cmodel = FileTIFF::GREYSCALE;
177 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
180 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
185 int FileTIFF::colormodel_supported(int colormodel)
187 switch(asset->tiff_cmodel)
189 case FileTIFF::RGB_888: return BC_RGB888; break;
190 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
191 case FileTIFF::GREYSCALE: return BC_RGB888; break;
192 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
193 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
194 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
195 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
196 default: return BC_RGB888; break;
200 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
202 switch(asset->tiff_cmodel)
204 case FileTIFF::GREYSCALE: return BC_RGB888; break;
205 case FileTIFF::RGB_888: return BC_RGB888; break;
206 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
207 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
208 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
209 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
210 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
211 default: return BC_RGB888; break;
216 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
218 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
219 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
221 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
222 tiff_unit->offset += size;
226 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
228 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
229 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
231 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
235 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
236 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
237 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
240 tiff_unit->offset += size;
244 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
246 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
250 tiff_unit->offset = off;
253 tiff_unit->offset += off;
256 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
259 return tiff_unit->offset;
262 static int tiff_close(thandle_t ptr)
267 static toff_t tiff_size(thandle_t ptr)
269 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
270 return tiff_unit->data->get_compressed_size();
273 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
275 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
276 *pbase = tiff_unit->data->get_data();
277 *psize = tiff_unit->data->get_compressed_size();
281 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
285 int FileTIFF::read_frame(VFrame *output, VFrame *input)
287 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
292 stream = TIFFClientOpen("FileTIFF",
303 // This loads the original TIFF data into each scanline of the output frame,
304 // assuming the output scanlines are bigger than the input scanlines.
305 // Then it expands the input data in reverse to fill the row.
306 for(int i = 0; i < asset->height; i++)
308 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
310 // For the greyscale model, the output is RGB888 but the input must be expanded
311 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
313 unsigned char *row = output->get_rows()[i];
314 for(int j = output->get_w() - 1; j >= 0; j--)
316 unsigned char value = row[j];
318 row[j * 3 + 1] = value;
319 row[j * 3 + 2] = value;
322 // For the 16 bit models, the output is floating point.
324 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
326 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
327 float *output_row = (float*)output->get_rows()[i];
328 for(int j = output->get_w() - 1; j >= 0; j--)
330 uint16_t r = input_row[j * 3];
331 uint16_t g = input_row[j * 3 + 1];
332 uint16_t b = input_row[j * 3 + 2];
333 output_row[j * 3] = (float)r / 65535;
334 output_row[j * 3 + 1] = (float)g / 65535;
335 output_row[j * 3 + 2] = (float)b / 65535;
339 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
341 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
342 float *output_row = (float*)output->get_rows()[i];
343 for(int j = output->get_w() - 1; j >= 0; j--)
345 uint16_t r = input_row[j * 4];
346 uint16_t g = input_row[j * 4 + 1];
347 uint16_t b = input_row[j * 4 + 2];
348 output_row[j * 4] = (float)r / 65535;
349 output_row[j * 4 + 1] = (float)g / 65535;
350 output_row[j * 4 + 2] = (float)b / 65535;
361 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
363 //printf("FileTIFF::write_frame 1\n");
364 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
367 tiff_unit->offset = 0;
368 tiff_unit->data = data;
369 tiff_unit->data->set_compressed_size(0);
371 stream = TIFFClientOpen("FileTIFF",
382 int components, color_model, bits, type, compression;
383 int sampleformat = SAMPLEFORMAT_UINT;
385 switch(asset->tiff_cmodel)
387 case FileTIFF::RGB_888:
389 color_model = BC_RGB888;
392 bytesperrow = 3 * asset->width;
394 case FileTIFF::RGB_161616:
396 color_model = BC_RGB_FLOAT;
399 bytesperrow = 6 * asset->width;
401 case FileTIFF::RGBA_8888:
403 color_model = BC_RGBA8888;
406 bytesperrow = 4 * asset->width;
408 case FileTIFF::RGBA_16161616:
410 color_model = BC_RGBA_FLOAT;
413 bytesperrow = 8 * asset->width;
415 case FileTIFF::RGB_FLOAT:
417 color_model = BC_RGB_FLOAT;
420 sampleformat = SAMPLEFORMAT_IEEEFP;
421 bytesperrow = 12 * asset->width;
423 case FileTIFF::RGBA_FLOAT:
425 color_model = BC_RGBA_FLOAT;
428 sampleformat = SAMPLEFORMAT_IEEEFP;
429 bytesperrow = 16 * asset->width;
433 color_model = BC_RGB888;
436 bytesperrow = 3 * asset->width;
441 switch(asset->tiff_compression)
444 compression = COMPRESSION_LZW;
446 case FileTIFF::PACK_BITS:
447 compression = COMPRESSION_PACKBITS;
449 case FileTIFF::DEFLATE:
450 compression = COMPRESSION_DEFLATE;
453 compression = COMPRESSION_JPEG;
456 compression = COMPRESSION_NONE;
460 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
461 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
462 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
463 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
464 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
465 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
466 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
467 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
468 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
469 TIFFDefaultStripSize(stream, (uint32_t)-1));
470 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
471 // (8 * 1024) / bytesperrow);
472 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
474 if(frame->get_color_model() == color_model)
476 for(int i = 0; i < asset->height; i++)
478 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
483 if(tiff_unit->temp &&
484 tiff_unit->temp->get_color_model() != color_model)
486 delete tiff_unit->temp;
491 tiff_unit->temp = new VFrame(0,
497 cmodel_transfer(tiff_unit->temp->get_rows(),
499 tiff_unit->temp->get_y(),
500 tiff_unit->temp->get_u(),
501 tiff_unit->temp->get_v(),
513 frame->get_color_model(),
518 for(int i = 0; i < asset->height; i++)
520 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
526 //printf("FileTIFF::write_frame 10\n");
530 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
532 return new FileTIFFUnit(this, writer);
542 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
543 : FrameWriterUnit(writer)
549 FileTIFFUnit::~FileTIFFUnit()
551 if(temp) delete temp;
565 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
566 : BC_Window(PROGRAM_NAME ": Video Compression",
567 parent_window->get_abs_cursor_x(1),
568 parent_window->get_abs_cursor_y(1),
572 this->parent_window = parent_window;
576 TIFFConfigVideo::~TIFFConfigVideo()
580 int TIFFConfigVideo::create_objects()
584 add_subwindow(new BC_Title(x, y, "Colorspace:"));
585 TIFFColorspace *menu1;
586 add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
587 menu1->create_objects();
589 add_subwindow(new BC_Title(x, y, "Compression:"));
590 TIFFCompression *menu2;
591 add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
592 menu2->create_objects();
594 add_subwindow(new BC_OKButton(this));
598 int TIFFConfigVideo::close_event()
609 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
613 FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
617 int TIFFColorspace::handle_event()
621 void TIFFColorspace::create_objects()
623 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
624 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
625 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
626 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
627 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
628 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
632 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
633 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
638 int TIFFColorspaceItem::handle_event()
640 gui->asset->tiff_cmodel = value;
650 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
651 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
655 int TIFFCompression::handle_event()
659 void TIFFCompression::create_objects()
661 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
662 // add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
663 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
664 // add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
665 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
672 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
673 : BC_MenuItem(FileTIFF::compression_to_str(value))
678 int TIFFCompressionItem::handle_event()
680 gui->asset->tiff_compression = value;