5 #include "interlacemodes.h"
13 FileTIFF::FileTIFF(Asset *asset, File *file)
14 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
16 asset->video_data = 1;
26 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
28 BC_WindowBase* &format_window,
34 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
35 format_window = window;
36 window->create_objects();
43 int FileTIFF::check_sig(Asset *asset)
45 FILE *stream = fopen(asset->path, "rb");
50 fread(test, 10, 1, stream);
53 if(test[0] == 'I' && test[1] == 'I')
58 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
59 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
64 if(strlen(asset->path) > 4 &&
65 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
70 if(strlen(asset->path) > 5 &&
71 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
79 char* FileTIFF::compression_to_str(int value)
83 case FileTIFF::NONE: return "None"; break;
84 case FileTIFF::LZW: return "LZW"; break;
85 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
86 case FileTIFF::DEFLATE: return "Deflate"; break;
87 case FileTIFF::JPEG: return "JPEG"; break;
94 char* FileTIFF::cmodel_to_str(int value)
98 case FileTIFF::GREYSCALE: return "Greyscale"; break;
99 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
100 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
101 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
102 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
103 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
104 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
112 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
114 if(edit->asset->format == FILE_TIFF_LIST ||
115 edit->asset->format == FILE_TIFF)
123 int FileTIFF::read_frame_header(char *path)
128 if(!(stream = TIFFOpen(path, "rb")))
130 fprintf(stderr, "FileTIFF::read_frame_header failed from %s\n", asset->path);
135 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
136 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
137 if(ptr && !strcmp(ptr, "Canon EOS-1DS"))
139 printf("FileTIFF::read_frame_header: got a %s.\n",
143 // The raw format for certain cameras deviates from TIFF here.
145 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
146 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
149 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
150 int bitspersample = 0;
151 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
152 int sampleformat = 0;
153 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
155 if(bitspersample == 8 && components == 3)
156 asset->tiff_cmodel = FileTIFF::RGB_888;
158 if(bitspersample == 16 && components == 3)
159 asset->tiff_cmodel = FileTIFF::RGB_161616;
161 if(bitspersample == 8 && components == 4)
162 asset->tiff_cmodel = FileTIFF::RGBA_8888;
164 if(bitspersample == 16 && components == 4)
165 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
167 if(bitspersample == 32 && components == 3)
168 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
170 if(bitspersample == 32 && components == 4)
171 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
173 if(bitspersample == 8 && (components == 1 || components == 0))
174 asset->tiff_cmodel = FileTIFF::GREYSCALE;
176 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
179 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
184 int FileTIFF::colormodel_supported(int colormodel)
186 switch(asset->tiff_cmodel)
188 case FileTIFF::RGB_888: return BC_RGB888; break;
189 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
190 case FileTIFF::GREYSCALE: return BC_RGB888; break;
191 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
192 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
193 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
194 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
195 default: return BC_RGB888; break;
199 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
201 switch(asset->tiff_cmodel)
203 case FileTIFF::GREYSCALE: return BC_RGB888; break;
204 case FileTIFF::RGB_888: return BC_RGB888; break;
205 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
206 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
207 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
208 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
209 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
210 default: return BC_RGB888; break;
215 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
217 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
218 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
220 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
221 tiff_unit->offset += size;
225 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
227 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
228 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
230 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
234 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
235 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
236 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
239 tiff_unit->offset += size;
243 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
245 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
249 tiff_unit->offset = off;
252 tiff_unit->offset += off;
255 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
258 return tiff_unit->offset;
261 static int tiff_close(thandle_t ptr)
266 static toff_t tiff_size(thandle_t ptr)
268 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
269 return tiff_unit->data->get_compressed_size();
272 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
274 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
275 *pbase = tiff_unit->data->get_data();
276 *psize = tiff_unit->data->get_compressed_size();
280 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
284 int FileTIFF::read_frame(VFrame *output, VFrame *input)
286 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
291 stream = TIFFClientOpen("FileTIFF",
302 // This loads the original TIFF data into each scanline of the output frame,
303 // assuming the output scanlines are bigger than the input scanlines.
304 // Then it expands the input data in reverse to fill the row.
305 for(int i = 0; i < asset->height; i++)
307 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
309 // For the greyscale model, the output is RGB888 but the input must be expanded
310 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
312 unsigned char *row = output->get_rows()[i];
313 for(int j = output->get_w() - 1; j >= 0; j--)
315 unsigned char value = row[j];
317 row[j * 3 + 1] = value;
318 row[j * 3 + 2] = value;
321 // For the 16 bit models, the output is floating point.
323 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
325 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
326 float *output_row = (float*)output->get_rows()[i];
327 for(int j = output->get_w() - 1; j >= 0; j--)
329 uint16_t r = input_row[j * 3];
330 uint16_t g = input_row[j * 3 + 1];
331 uint16_t b = input_row[j * 3 + 2];
332 output_row[j * 3] = (float)r / 65535;
333 output_row[j * 3 + 1] = (float)g / 65535;
334 output_row[j * 3 + 2] = (float)b / 65535;
338 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
340 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
341 float *output_row = (float*)output->get_rows()[i];
342 for(int j = output->get_w() - 1; j >= 0; j--)
344 uint16_t r = input_row[j * 4];
345 uint16_t g = input_row[j * 4 + 1];
346 uint16_t b = input_row[j * 4 + 2];
347 output_row[j * 4] = (float)r / 65535;
348 output_row[j * 4 + 1] = (float)g / 65535;
349 output_row[j * 4 + 2] = (float)b / 65535;
360 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
362 //printf("FileTIFF::write_frame 1\n");
363 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
366 tiff_unit->offset = 0;
367 tiff_unit->data = data;
368 tiff_unit->data->set_compressed_size(0);
370 stream = TIFFClientOpen("FileTIFF",
381 int components, color_model, bits, type, compression;
382 int sampleformat = SAMPLEFORMAT_UINT;
384 switch(asset->tiff_cmodel)
386 case FileTIFF::RGB_888:
388 color_model = BC_RGB888;
391 bytesperrow = 3 * asset->width;
393 case FileTIFF::RGB_161616:
395 color_model = BC_RGB_FLOAT;
398 bytesperrow = 6 * asset->width;
400 case FileTIFF::RGBA_8888:
402 color_model = BC_RGBA8888;
405 bytesperrow = 4 * asset->width;
407 case FileTIFF::RGBA_16161616:
409 color_model = BC_RGBA_FLOAT;
412 bytesperrow = 8 * asset->width;
414 case FileTIFF::RGB_FLOAT:
416 color_model = BC_RGB_FLOAT;
419 sampleformat = SAMPLEFORMAT_IEEEFP;
420 bytesperrow = 12 * asset->width;
422 case FileTIFF::RGBA_FLOAT:
424 color_model = BC_RGBA_FLOAT;
427 sampleformat = SAMPLEFORMAT_IEEEFP;
428 bytesperrow = 16 * asset->width;
432 color_model = BC_RGB888;
435 bytesperrow = 3 * asset->width;
440 switch(asset->tiff_compression)
443 compression = COMPRESSION_LZW;
445 case FileTIFF::PACK_BITS:
446 compression = COMPRESSION_PACKBITS;
448 case FileTIFF::DEFLATE:
449 compression = COMPRESSION_DEFLATE;
452 compression = COMPRESSION_JPEG;
455 compression = COMPRESSION_NONE;
459 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
460 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
461 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
462 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
463 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
464 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
465 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
466 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
467 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
468 TIFFDefaultStripSize(stream, (uint32_t)-1));
469 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
470 // (8 * 1024) / bytesperrow);
471 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
473 if(frame->get_color_model() == color_model)
475 for(int i = 0; i < asset->height; i++)
477 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
482 if(tiff_unit->temp &&
483 tiff_unit->temp->get_color_model() != color_model)
485 delete tiff_unit->temp;
490 tiff_unit->temp = new VFrame(0,
496 cmodel_transfer(tiff_unit->temp->get_rows(),
498 tiff_unit->temp->get_y(),
499 tiff_unit->temp->get_u(),
500 tiff_unit->temp->get_v(),
512 frame->get_color_model(),
517 for(int i = 0; i < asset->height; i++)
519 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
525 //printf("FileTIFF::write_frame 10\n");
529 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
531 return new FileTIFFUnit(this, writer);
541 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
542 : FrameWriterUnit(writer)
548 FileTIFFUnit::~FileTIFFUnit()
550 if(temp) delete temp;
564 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
565 : BC_Window(PROGRAM_NAME ": Video Compression",
566 parent_window->get_abs_cursor_x(1),
567 parent_window->get_abs_cursor_y(1),
571 this->parent_window = parent_window;
575 TIFFConfigVideo::~TIFFConfigVideo()
579 int TIFFConfigVideo::create_objects()
583 add_subwindow(new BC_Title(x, y, "Colorspace:"));
584 TIFFColorspace *menu1;
585 add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
586 menu1->create_objects();
588 add_subwindow(new BC_Title(x, y, "Compression:"));
589 TIFFCompression *menu2;
590 add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
591 menu2->create_objects();
593 add_subwindow(new BC_OKButton(this));
597 int TIFFConfigVideo::close_event()
608 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
612 FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
616 int TIFFColorspace::handle_event()
620 void TIFFColorspace::create_objects()
622 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
623 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
624 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
625 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
626 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
627 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
631 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
632 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
637 int TIFFColorspaceItem::handle_event()
639 gui->asset->tiff_cmodel = value;
649 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
650 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
654 int TIFFCompression::handle_event()
658 void TIFFCompression::create_objects()
660 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
661 // add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
662 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
663 // add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
664 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
671 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
672 : BC_MenuItem(FileTIFF::compression_to_str(value))
677 int TIFFCompressionItem::handle_event()
679 gui->asset->tiff_compression = value;