2 #include "bcwidgetgrid.h"
6 #include "interlacemodes.h"
15 FileTIFF::FileTIFF(Asset_GC asset, File *file)
16 : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
18 asset->video_data = 1;
28 void FileTIFF::get_parameters(BC_WindowBase *parent_window,
30 BC_WindowBase* &format_window,
36 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
37 format_window = window;
38 window->create_objects();
45 int FileTIFF::check_sig(Asset_GC asset)
47 FILE *stream = fopen(asset->path, "rb");
52 fread(test, 10, 1, stream);
55 if(test[0] == 'I' && test[1] == 'I')
60 if(test[0] == 'T' && test[1] == 'I' && test[2] == 'F' && test[3] == 'F' &&
61 test[4] == 'L' && test[5] == 'I' && test[6] == 'S' && test[7] == 'T')
66 if(strlen(asset->path) > 4 &&
67 !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
72 if(strlen(asset->path) > 5 &&
73 !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
81 char* FileTIFF::compression_to_str(int value)
85 case FileTIFF::NONE: return "None"; break;
86 case FileTIFF::LZW: return "LZW"; break;
87 case FileTIFF::PACK_BITS: return "Pack Bits"; break;
88 case FileTIFF::DEFLATE: return "Deflate"; break;
89 case FileTIFF::JPEG: return "JPEG"; break;
96 char* FileTIFF::cmodel_to_str(int value)
100 case FileTIFF::GREYSCALE: return "Greyscale"; break;
101 case FileTIFF::RGB_888: return "RGB-8 Bit"; break;
102 case FileTIFF::RGB_161616: return "RGB-16 Bit"; break;
103 case FileTIFF::RGBA_8888: return "RGBA-8 Bit"; break;
104 case FileTIFF::RGBA_16161616: return "RGBA-16 Bit"; break;
105 case FileTIFF::RGB_FLOAT: return "RGB-FLOAT"; break;
106 case FileTIFF::RGBA_FLOAT: return "RGBA-FLOAT"; break;
114 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
116 if(edit->asset->format == FILE_TIFF_LIST ||
117 edit->asset->format == FILE_TIFF)
125 int FileTIFF::read_frame_header(char *path)
130 if(!(stream = TIFFOpen(path, "rb")))
132 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
137 TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
138 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
139 if(ptr && !strcmp(ptr, "Canon EOS-1DS")) // FIXME: Does this have a purpose?
141 printf("FileTIFF::read_frame_header: got a %s.\n",
145 // The raw format for certain cameras deviates from TIFF here.
147 TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
148 TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
151 TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &components);
152 int bitspersample = 0;
153 TIFFGetField(stream, TIFFTAG_BITSPERSAMPLE, &bitspersample);
154 int sampleformat = 0;
155 TIFFGetField(stream, TIFFTAG_SAMPLEFORMAT, &sampleformat);
157 if(bitspersample == 8 && components == 3)
158 asset->tiff_cmodel = FileTIFF::RGB_888;
160 if(bitspersample == 16 && components == 3)
161 asset->tiff_cmodel = FileTIFF::RGB_161616;
163 if(bitspersample == 8 && components == 4)
164 asset->tiff_cmodel = FileTIFF::RGBA_8888;
166 if(bitspersample == 16 && components == 4)
167 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
169 if(bitspersample == 32 && components == 3)
170 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
172 if(bitspersample == 32 && components == 4)
173 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
175 if(bitspersample == 8 && (components == 1 || components == 0))
176 asset->tiff_cmodel = FileTIFF::GREYSCALE;
178 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
181 asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
186 int FileTIFF::colormodel_supported(int colormodel)
188 switch(asset->tiff_cmodel)
190 case FileTIFF::RGB_888: return BC_RGB888; break;
191 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
192 case FileTIFF::GREYSCALE: return BC_RGB888; break;
193 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
194 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
195 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
196 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
197 default: return BC_RGB888; break;
201 int FileTIFF::get_best_colormodel(Asset_GC asset, int driver)
203 switch(asset->tiff_cmodel)
205 case FileTIFF::GREYSCALE: return BC_RGB888; break;
206 case FileTIFF::RGB_888: return BC_RGB888; break;
207 case FileTIFF::RGB_161616: return BC_RGB_FLOAT; break;
208 case FileTIFF::RGBA_8888: return BC_RGBA8888; break;
209 case FileTIFF::RGBA_16161616: return BC_RGBA_FLOAT; break;
210 case FileTIFF::RGB_FLOAT: return BC_RGB_FLOAT; break;
211 case FileTIFF::RGBA_FLOAT: return BC_RGBA_FLOAT; break;
212 default: return BC_RGB888; break;
217 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
219 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
220 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
222 memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
223 tiff_unit->offset += size;
227 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
229 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
230 if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
232 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
236 if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
237 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
238 memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
241 tiff_unit->offset += size;
245 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
247 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
251 tiff_unit->offset = off;
254 tiff_unit->offset += off;
257 tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
260 return tiff_unit->offset;
263 static int tiff_close(thandle_t ptr)
268 static toff_t tiff_size(thandle_t ptr)
270 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
271 return tiff_unit->data->get_compressed_size();
274 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
276 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
277 *pbase = tiff_unit->data->get_data();
278 *psize = tiff_unit->data->get_compressed_size();
282 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
286 int FileTIFF::read_frame(VFrame *output, VFrame *input)
288 FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
293 stream = TIFFClientOpen("FileTIFF",
304 // This loads the original TIFF data into each scanline of the output frame,
305 // assuming the output scanlines are bigger than the input scanlines.
306 // Then it expands the input data in reverse to fill the row.
307 for(int i = 0; i < asset->height; i++)
309 TIFFReadScanline(stream, output->get_rows()[i], i, 0);
311 // For the greyscale model, the output is RGB888 but the input must be expanded
312 if(asset->tiff_cmodel == FileTIFF::GREYSCALE)
314 unsigned char *row = output->get_rows()[i];
315 for(int j = output->get_w() - 1; j >= 0; j--)
317 unsigned char value = row[j];
319 row[j * 3 + 1] = value;
320 row[j * 3 + 2] = value;
323 // For the 16 bit models, the output is floating point.
325 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
327 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
328 float *output_row = (float*)output->get_rows()[i];
329 for(int j = output->get_w() - 1; j >= 0; j--)
331 uint16_t r = input_row[j * 3];
332 uint16_t g = input_row[j * 3 + 1];
333 uint16_t b = input_row[j * 3 + 2];
334 output_row[j * 3] = (float)r / 65535;
335 output_row[j * 3 + 1] = (float)g / 65535;
336 output_row[j * 3 + 2] = (float)b / 65535;
340 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
342 uint16_t *input_row = (uint16_t*)output->get_rows()[i];
343 float *output_row = (float*)output->get_rows()[i];
344 for(int j = output->get_w() - 1; j >= 0; j--)
346 uint16_t r = input_row[j * 4];
347 uint16_t g = input_row[j * 4 + 1];
348 uint16_t b = input_row[j * 4 + 2];
349 output_row[j * 4] = (float)r / 65535;
350 output_row[j * 4 + 1] = (float)g / 65535;
351 output_row[j * 4 + 2] = (float)b / 65535;
362 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
364 //printf("FileTIFF::write_frame 1\n");
365 FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
368 tiff_unit->offset = 0;
369 tiff_unit->data = data;
370 tiff_unit->data->set_compressed_size(0);
372 stream = TIFFClientOpen("FileTIFF",
383 int components, color_model, bits, type, compression;
384 int sampleformat = SAMPLEFORMAT_UINT;
386 switch(asset->tiff_cmodel)
388 case FileTIFF::RGB_888:
390 color_model = BC_RGB888;
393 bytesperrow = 3 * asset->width;
395 case FileTIFF::RGB_161616:
397 color_model = BC_RGB_FLOAT;
400 bytesperrow = 6 * asset->width;
402 case FileTIFF::RGBA_8888:
404 color_model = BC_RGBA8888;
407 bytesperrow = 4 * asset->width;
409 case FileTIFF::RGBA_16161616:
411 color_model = BC_RGBA_FLOAT;
414 bytesperrow = 8 * asset->width;
416 case FileTIFF::RGB_FLOAT:
418 color_model = BC_RGB_FLOAT;
421 sampleformat = SAMPLEFORMAT_IEEEFP;
422 bytesperrow = 12 * asset->width;
424 case FileTIFF::RGBA_FLOAT:
426 color_model = BC_RGBA_FLOAT;
429 sampleformat = SAMPLEFORMAT_IEEEFP;
430 bytesperrow = 16 * asset->width;
434 color_model = BC_RGB888;
437 bytesperrow = 3 * asset->width;
442 switch(asset->tiff_compression)
445 compression = COMPRESSION_LZW;
447 case FileTIFF::PACK_BITS:
448 compression = COMPRESSION_PACKBITS;
450 case FileTIFF::DEFLATE:
451 compression = COMPRESSION_DEFLATE;
454 compression = COMPRESSION_JPEG;
457 compression = COMPRESSION_NONE;
461 TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
462 TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
463 TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
464 TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
465 TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
466 TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
467 TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
468 TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
469 TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
470 TIFFDefaultStripSize(stream, (uint32_t)-1));
471 // TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP,
472 // (8 * 1024) / bytesperrow);
473 TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
475 if(frame->get_color_model() == color_model)
477 for(int i = 0; i < asset->height; i++)
479 TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
484 if(tiff_unit->temp &&
485 tiff_unit->temp->get_color_model() != color_model)
487 delete tiff_unit->temp;
492 tiff_unit->temp = new VFrame(0,
498 cmodel_transfer(tiff_unit->temp->get_rows(),
500 tiff_unit->temp->get_y(),
501 tiff_unit->temp->get_u(),
502 tiff_unit->temp->get_v(),
514 frame->get_color_model(),
519 for(int i = 0; i < asset->height; i++)
521 TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
527 //printf("FileTIFF::write_frame 10\n");
531 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
533 return new FileTIFFUnit(this, writer);
543 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
544 : FrameWriterUnit(writer)
550 FileTIFFUnit::~FileTIFFUnit()
552 if(temp) delete temp;
566 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset_GC asset)
567 : BC_Window(PROGRAM_NAME ": Video Compression",
568 parent_window->get_abs_cursor_x(1),
569 parent_window->get_abs_cursor_y(1),
577 this->parent_window = parent_window;
581 TIFFConfigVideo::~TIFFConfigVideo()
585 int TIFFConfigVideo::create_objects()
590 BC_RelocatableWidget *rw;
592 wg = add_widgetgrid(new BC_WidgetGrid(10, 10, 10, 10, 3, 3));
595 add_subwindow(new BC_Title(x, y, "Colorspace:"));
596 TIFFColorspace *menu1;
597 add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
598 menu1->create_objects();
600 wg->add(menu1, 1, 1);
603 add_subwindow(new BC_Title(x, y, "Compression:"));
604 TIFFCompression *menu2;
605 add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
606 menu2->create_objects();
608 wg->add(menu2, 0, 1);
611 add_subwindow(new BC_OKButton(this));
614 resize_window(wg->get_w_wm(), wg->get_h_wm() + rw->get_h());
618 int TIFFConfigVideo::close_event()
629 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
633 FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
637 int TIFFColorspace::handle_event()
641 void TIFFColorspace::create_objects()
643 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
644 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
645 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
646 // add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
647 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
648 add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
652 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
653 : BC_MenuItem(FileTIFF::cmodel_to_str(value))
658 int TIFFColorspaceItem::handle_event()
660 gui->asset->tiff_cmodel = value;
670 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
671 : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
675 int TIFFCompression::handle_event()
679 void TIFFCompression::create_objects()
681 add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
682 // add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
683 add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
684 // add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
685 // add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
692 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
693 : BC_MenuItem(FileTIFF::compression_to_str(value))
698 int TIFFCompressionItem::handle_event()
700 gui->asset->tiff_compression = value;
709 // c-file-style: "linux"