r915:
[cinelerra_cv/mob.git] / cinelerra / filetiff.C
blobe7fe813cb2737311def045c83ecf69b6bb2afb87
1 #include "asset.h"
2 #include "edit.h"
3 #include "file.h"
4 #include "filetiff.h"
5 #include "interlacemodes.h"
6 #include "language.h"
7 #include "vframe.h"
8 #include "mainerror.h"
10 #include <stdint.h>
11 #include <string.h>
12 #include <unistd.h>
14 FileTIFF::FileTIFF(Asset *asset, File *file)
15  : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
17         asset->video_data = 1;
18         temp = 0;
21 FileTIFF::~FileTIFF()
23         if(temp) delete temp;
27 void FileTIFF::get_parameters(BC_WindowBase *parent_window, 
28         Asset *asset, 
29         BC_WindowBase* &format_window,
30         int audio_options,
31         int video_options)
33         if(video_options)
34         {
35                 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
36                 format_window = window;
37                 window->create_objects();
38                 window->run_window();
39                 delete window;
40         }
44 int FileTIFF::check_sig(Asset *asset)
46         FILE *stream = fopen(asset->path, "rb");
48         if(stream)
49         {
50                 char test[10];
51                 fread(test, 10, 1, stream);
52                 fclose(stream);
54                 if(test[0] == 'I' && test[1] == 'I')
55                 {
56                         return 1;
57                 }
58                 else
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')
61                 {
62                         return 1;
63                 }
64                 else
65                 if(strlen(asset->path) > 4 && 
66                         !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
67                 {
68                         return 1;
69                 }
70                 else
71                 if(strlen(asset->path) > 5 && 
72                         !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
73                 {
74                         return 1;
75                 }
76         }
77         return 0;
80 char* FileTIFF::compression_to_str(int value)
82         switch(value)
83         {
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;
89                 default: 
90                         return "None"; 
91                         break;
92         }
95 char* FileTIFF::cmodel_to_str(int value)
97         switch(value)
98         {
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;
106                 default: 
107                         return "RGB-8 Bit"; 
108                         break;
109         }
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)
117                 return 1;
118         
119         return 0;
124 int FileTIFF::read_frame_header(char *path)
126         TIFF *stream;
127         int result = 0;
129         if(!(stream = TIFFOpen(path, "rb")))
130         {
131                 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
132                 return 1;
133         }
135         char *ptr = 0;
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?
139         {
140                 printf("FileTIFF::read_frame_header: got a %s.\n",
141                         ptr);
142         }
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));
149         int components = 0;
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;
158         else
159         if(bitspersample == 16 && components == 3)
160                 asset->tiff_cmodel = FileTIFF::RGB_161616;
161         else
162         if(bitspersample == 8 && components == 4)
163                 asset->tiff_cmodel = FileTIFF::RGBA_8888;
164         else
165         if(bitspersample == 16 && components == 4)
166                 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
167         else
168         if(bitspersample == 32 && components == 3)
169                 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
170         else
171         if(bitspersample == 32 && components == 4)
172                 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
173         else
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);
178         TIFFClose(stream);
180         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
182         return result;
185 int FileTIFF::colormodel_supported(int colormodel)
187         switch(asset->tiff_cmodel)
188         {
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;
197         }
200 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
202         switch(asset->tiff_cmodel)
203         {
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;
212         }
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)
220                 return 0;
221         memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
222         tiff_unit->offset += size;
223         return 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)
230         {
231                 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
232         }
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,
238                 buf,
239                 size);
240         tiff_unit->offset += size;
241         return size;
244 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
246         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
247         switch(whence)
248         {
249                 case SEEK_SET:
250                         tiff_unit->offset = off;
251                         break;
252                 case SEEK_CUR:
253                         tiff_unit->offset += off;
254                         break;
255                 case SEEK_END:
256                         tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
257                         break;
258         }
259         return tiff_unit->offset;
262 static int tiff_close(thandle_t ptr)
264         return 0;
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();
278         return 0;
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);
288         TIFF *stream;
289         unit->offset = 0;
290         unit->data = input;
292         stream = TIFFClientOpen("FileTIFF", 
293                 "r",
294             (void*)unit,
295             tiff_read, 
296                 tiff_write,
297             tiff_seek, 
298                 tiff_close,
299             tiff_size,
300             tiff_mmap, 
301                 tiff_unmap);
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++)
307         {
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)
312                 {
313                         unsigned char *row = output->get_rows()[i];
314                         for(int j = output->get_w() - 1; j >= 0; j--)
315                         {
316                                 unsigned char value = row[j];
317                                 row[j * 3] = value;
318                                 row[j * 3 + 1] = value;
319                                 row[j * 3 + 2] = value;
320                         }
321                 }
322 // For the 16 bit models, the output is floating point.
323                 else
324                 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
325                 {
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--)
329                         {
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;
336                         }
337                 }
338                 else
339                 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
340                 {
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--)
344                         {
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;
351                         }
352                 }
353         }
355         TIFFClose(stream);
356         delete unit;
358         return 0;
361 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
363 //printf("FileTIFF::write_frame 1\n");
364         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
365         int result = 0;
366         TIFF *stream;
367         tiff_unit->offset = 0;
368         tiff_unit->data = data;
369         tiff_unit->data->set_compressed_size(0);
371         stream = TIFFClientOpen("FileTIFF", 
372                 "w",
373             (void*)tiff_unit,
374             tiff_read, 
375                 tiff_write,
376             tiff_seek, 
377                 tiff_close,
378             tiff_size,
379             tiff_mmap, 
380                 tiff_unmap);
382         int components, color_model, bits, type, compression;
383         int sampleformat = SAMPLEFORMAT_UINT;
384         int bytesperrow;
385         switch(asset->tiff_cmodel)
386         {
387                 case FileTIFF::RGB_888: 
388                         components = 3;
389                         color_model = BC_RGB888;
390                         bits = 8;
391                         type = TIFF_BYTE;
392                         bytesperrow = 3 * asset->width;
393                         break;
394                 case FileTIFF::RGB_161616: 
395                         components = 3;
396                         color_model = BC_RGB_FLOAT;
397                         bits = 16;
398                         type = TIFF_SHORT;
399                         bytesperrow = 6 * asset->width;
400                         break;
401                 case FileTIFF::RGBA_8888: 
402                         components = 4;
403                         color_model = BC_RGBA8888;
404                         bits = 8;
405                         type = TIFF_BYTE;
406                         bytesperrow = 4 * asset->width;
407                         break;
408                 case FileTIFF::RGBA_16161616: 
409                         components = 4;
410                         color_model = BC_RGBA_FLOAT;
411                         bits = 16;
412                         type = TIFF_SHORT;
413                         bytesperrow = 8 * asset->width;
414                         break;
415                 case FileTIFF::RGB_FLOAT: 
416                         components = 3;
417                         color_model = BC_RGB_FLOAT;
418                         bits = 32;
419                         type = TIFF_FLOAT;
420                         sampleformat = SAMPLEFORMAT_IEEEFP;
421                         bytesperrow = 12 * asset->width;
422                         break;
423                 case FileTIFF::RGBA_FLOAT: 
424                         components = 4;
425                         color_model = BC_RGBA_FLOAT;
426                         bits = 32;
427                         type = TIFF_FLOAT;
428                         sampleformat = SAMPLEFORMAT_IEEEFP;
429                         bytesperrow = 16 * asset->width;
430                         break;
431                 default: 
432                         components = 3;
433                         color_model = BC_RGB888;
434                         bits = 8;
435                         type = TIFF_BYTE;
436                         bytesperrow = 3 * asset->width;
437                         break;
438         }
441         switch(asset->tiff_compression)
442         {
443                 case FileTIFF::LZW:
444                         compression = COMPRESSION_LZW;
445                         break;
446                 case FileTIFF::PACK_BITS:
447                         compression = COMPRESSION_PACKBITS;
448                         break;
449                 case FileTIFF::DEFLATE:
450                         compression = COMPRESSION_DEFLATE;
451                         break;
452                 case FileTIFF::JPEG:
453                         compression = COMPRESSION_JPEG;
454                         break;
455                 default:
456                         compression = COMPRESSION_NONE;
457                         break;
458         }
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)
475         {
476                 for(int i = 0; i < asset->height; i++)
477                 {
478                         TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
479                 }
480         }
481         else
482         {
483                 if(tiff_unit->temp &&
484                         tiff_unit->temp->get_color_model() != color_model)
485                 {
486                         delete tiff_unit->temp;
487                         tiff_unit->temp = 0;
488                 }
489                 if(!tiff_unit->temp)
490                 {
491                         tiff_unit->temp = new VFrame(0,
492                                 asset->width,
493                                 asset->height,
494                                 color_model);
495                 }
497                 cmodel_transfer(tiff_unit->temp->get_rows(), 
498                         frame->get_rows(),
499                         tiff_unit->temp->get_y(),
500                         tiff_unit->temp->get_u(),
501                         tiff_unit->temp->get_v(),
502                         frame->get_y(),
503                         frame->get_u(),
504                         frame->get_v(),
505                         0, 
506                         0, 
507                         frame->get_w(), 
508                         frame->get_h(),
509                         0, 
510                         0, 
511                         frame->get_w(), 
512                         frame->get_h(),
513                         frame->get_color_model(), 
514                         color_model,
515                         0,
516                         frame->get_w(),
517                         frame->get_w());
518                 for(int i = 0; i < asset->height; i++)
519                 {
520                         TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
521                 }
522         }
524         TIFFClose(stream);
526 //printf("FileTIFF::write_frame 10\n");
527         return result;
530 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
532         return new FileTIFFUnit(this, writer);
542 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
543  : FrameWriterUnit(writer)
545         this->file = file;
546         temp = 0;
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),
569         400,
570         200)
572         this->parent_window = parent_window;
573         this->asset = asset;
576 TIFFConfigVideo::~TIFFConfigVideo()
580 int TIFFConfigVideo::create_objects()
582         int x = 10, y = 10;
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();
588         y += 40;
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));
595         return 0;
598 int TIFFConfigVideo::close_event()
600         set_done(0);
601         return 1;
609 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
610  : BC_PopupMenu(x,
611         y,
612         w,
613         FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
615         this->gui = gui;
617 int TIFFColorspace::handle_event()
619         return 1;
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))
635         this->gui = gui;
636         this->value = value;
638 int TIFFColorspaceItem::handle_event()
640         gui->asset->tiff_cmodel = value;
641         return 0;
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))
653         this->gui = gui;
655 int TIFFCompression::handle_event()
657         return 1;
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))
675         this->gui = gui;
676         this->value = value;
678 int TIFFCompressionItem::handle_event()
680         gui->asset->tiff_compression = value;
681         return 0;