r793: Small API addon, so plugins can 'see' camera and projector automation
[cinelerra_cv/mob.git] / cinelerra / filetiff.C
blob322f3383566fe880a80268d4e2d341f0acd779ed
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"
9 #include <stdint.h>
10 #include <string.h>
11 #include <unistd.h>
13 FileTIFF::FileTIFF(Asset *asset, File *file)
14  : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
16         asset->video_data = 1;
17         temp = 0;
20 FileTIFF::~FileTIFF()
22         if(temp) delete temp;
26 void FileTIFF::get_parameters(BC_WindowBase *parent_window, 
27         Asset *asset, 
28         BC_WindowBase* &format_window,
29         int audio_options,
30         int video_options)
32         if(video_options)
33         {
34                 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
35                 format_window = window;
36                 window->create_objects();
37                 window->run_window();
38                 delete window;
39         }
43 int FileTIFF::check_sig(Asset *asset)
45         FILE *stream = fopen(asset->path, "rb");
47         if(stream)
48         {
49                 char test[10];
50                 fread(test, 10, 1, stream);
51                 fclose(stream);
53                 if(test[0] == 'I' && test[1] == 'I')
54                 {
55                         return 1;
56                 }
57                 else
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')
60                 {
61                         return 1;
62                 }
63                 else
64                 if(strlen(asset->path) > 4 && 
65                         !strcasecmp(asset->path + strlen(asset->path) - 4, ".tif"))
66                 {
67                         return 1;
68                 }
69                 else
70                 if(strlen(asset->path) > 5 && 
71                         !strcasecmp(asset->path + strlen(asset->path) - 5, ".tiff"))
72                 {
73                         return 1;
74                 }
75         }
76         return 0;
79 char* FileTIFF::compression_to_str(int value)
81         switch(value)
82         {
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;
88                 default: 
89                         return "None"; 
90                         break;
91         }
94 char* FileTIFF::cmodel_to_str(int value)
96         switch(value)
97         {
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;
105                 default: 
106                         return "RGB-8 Bit"; 
107                         break;
108         }
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)
116                 return 1;
117         
118         return 0;
123 int FileTIFF::read_frame_header(char *path)
125         TIFF *stream;
126         int result = 0;
128         if(!(stream = TIFFOpen(path, "rb")))
129         {
130                 fprintf(stderr, "FileTIFF::read_frame_header failed from %s\n", asset->path);
131                 return 1;
132         }
134         char *ptr = 0;
135         TIFFGetField(stream, TIFFTAG_MODEL, &ptr);
136 //printf("FileTIFF::read_frame_header 1 %s\n", ptr);
137         if(ptr && !strcmp(ptr, "Canon EOS-1DS"))
138         {
139                 printf("FileTIFF::read_frame_header: got a %s.\n",
140                         ptr);
141         }
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));
148         int components = 0;
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;
157         else
158         if(bitspersample == 16 && components == 3)
159                 asset->tiff_cmodel = FileTIFF::RGB_161616;
160         else
161         if(bitspersample == 8 && components == 4)
162                 asset->tiff_cmodel = FileTIFF::RGBA_8888;
163         else
164         if(bitspersample == 16 && components == 4)
165                 asset->tiff_cmodel = FileTIFF::RGBA_16161616;
166         else
167         if(bitspersample == 32 && components == 3)
168                 asset->tiff_cmodel = FileTIFF::RGB_FLOAT;
169         else
170         if(bitspersample == 32 && components == 4)
171                 asset->tiff_cmodel = FileTIFF::RGBA_FLOAT;
172         else
173         if(bitspersample == 8 && components == 1)
174                 asset->tiff_cmodel = FileTIFF::GREYSCALE;
176 //printf("FileTIFF::read_frame_header %d %d %d\n", bitspersample, components, asset->tiff_cmodel);
177         TIFFClose(stream);
178         
179         asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
181         return result;
184 int FileTIFF::colormodel_supported(int colormodel)
186         switch(asset->tiff_cmodel)
187         {
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;
196         }
199 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
201         switch(asset->tiff_cmodel)
202         {
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;
211         }
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)
219                 return 0;
220         memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
221         tiff_unit->offset += size;
222         return 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)
229         {
230                 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
231         }
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,
237                 buf,
238                 size);
239         tiff_unit->offset += size;
240         return size;
243 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
245         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
246         switch(whence)
247         {
248                 case SEEK_SET:
249                         tiff_unit->offset = off;
250                         break;
251                 case SEEK_CUR:
252                         tiff_unit->offset += off;
253                         break;
254                 case SEEK_END:
255                         tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
256                         break;
257         }
258         return tiff_unit->offset;
261 static int tiff_close(thandle_t ptr)
263         return 0;
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();
277         return 0;
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);
287         TIFF *stream;
288         unit->offset = 0;
289         unit->data = input;
291         stream = TIFFClientOpen("FileTIFF", 
292                 "r",
293             (void*)unit,
294             tiff_read, 
295                 tiff_write,
296             tiff_seek, 
297                 tiff_close,
298             tiff_size,
299             tiff_mmap, 
300                 tiff_unmap);
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++)
306         {
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)
311                 {
312                         unsigned char *row = output->get_rows()[i];
313                         for(int j = output->get_w() - 1; j >= 0; j--)
314                         {
315                                 unsigned char value = row[j];
316                                 row[j * 3] = value;
317                                 row[j * 3 + 1] = value;
318                                 row[j * 3 + 2] = value;
319                         }
320                 }
321 // For the 16 bit models, the output is floating point.
322                 else
323                 if(asset->tiff_cmodel == FileTIFF::RGB_161616)
324                 {
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--)
328                         {
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;
335                         }
336                 }
337                 else
338                 if(asset->tiff_cmodel == FileTIFF::RGBA_16161616)
339                 {
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--)
343                         {
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;
350                         }
351                 }
352         }
353         TIFFClose(stream);
354         delete unit;
356         return 0;
359 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
361 //printf("FileTIFF::write_frame 1\n");
362         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
363         int result = 0;
364         TIFF *stream;
365         tiff_unit->offset = 0;
366         tiff_unit->data = data;
367         tiff_unit->data->set_compressed_size(0);
369         stream = TIFFClientOpen("FileTIFF", 
370                 "w",
371             (void*)tiff_unit,
372             tiff_read, 
373                 tiff_write,
374             tiff_seek, 
375                 tiff_close,
376             tiff_size,
377             tiff_mmap, 
378                 tiff_unmap);
380         int components, color_model, bits, type, compression;
381         int sampleformat = SAMPLEFORMAT_UINT;
382         int bytesperrow;
383         switch(asset->tiff_cmodel)
384         {
385                 case FileTIFF::RGB_888: 
386                         components = 3;
387                         color_model = BC_RGB888;
388                         bits = 8;
389                         type = TIFF_BYTE;
390                         bytesperrow = 3 * asset->width;
391                         break;
392                 case FileTIFF::RGB_161616: 
393                         components = 3;
394                         color_model = BC_RGB_FLOAT;
395                         bits = 16;
396                         type = TIFF_SHORT;
397                         bytesperrow = 6 * asset->width;
398                         break;
399                 case FileTIFF::RGBA_8888: 
400                         components = 4;
401                         color_model = BC_RGBA8888;
402                         bits = 8;
403                         type = TIFF_BYTE;
404                         bytesperrow = 4 * asset->width;
405                         break;
406                 case FileTIFF::RGBA_16161616: 
407                         components = 4;
408                         color_model = BC_RGBA_FLOAT;
409                         bits = 16;
410                         type = TIFF_SHORT;
411                         bytesperrow = 8 * asset->width;
412                         break;
413                 case FileTIFF::RGB_FLOAT: 
414                         components = 3;
415                         color_model = BC_RGB_FLOAT;
416                         bits = 32;
417                         type = TIFF_FLOAT;
418                         sampleformat = SAMPLEFORMAT_IEEEFP;
419                         bytesperrow = 12 * asset->width;
420                         break;
421                 case FileTIFF::RGBA_FLOAT: 
422                         components = 4;
423                         color_model = BC_RGBA_FLOAT;
424                         bits = 32;
425                         type = TIFF_FLOAT;
426                         sampleformat = SAMPLEFORMAT_IEEEFP;
427                         bytesperrow = 16 * asset->width;
428                         break;
429                 default: 
430                         components = 3;
431                         color_model = BC_RGB888;
432                         bits = 8;
433                         type = TIFF_BYTE;
434                         bytesperrow = 3 * asset->width;
435                         break;
436         }
439         switch(asset->tiff_compression)
440         {
441                 case FileTIFF::LZW:
442                         compression = COMPRESSION_LZW;
443                         break;
444                 case FileTIFF::PACK_BITS:
445                         compression = COMPRESSION_PACKBITS;
446                         break;
447                 case FileTIFF::DEFLATE:
448                         compression = COMPRESSION_DEFLATE;
449                         break;
450                 case FileTIFF::JPEG:
451                         compression = COMPRESSION_JPEG;
452                         break;
453                 default:
454                         compression = COMPRESSION_NONE;
455                         break;
456         }
458         TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
459         TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
460         TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
461         TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, components);
462         TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, bits);
463     TIFFSetField(stream, TIFFTAG_SAMPLEFORMAT, sampleformat);
464         TIFFSetField(stream, TIFFTAG_COMPRESSION, compression);
465         TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
466         TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
467                 TIFFDefaultStripSize(stream, (uint32_t)-1));
468 //      TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, 
469 //              (8 * 1024) / bytesperrow);
470         TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
472         if(frame->get_color_model() == color_model)
473         {
474                 for(int i = 0; i < asset->height; i++)
475                 {
476                         TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
477                 }
478         }
479         else
480         {
481                 if(tiff_unit->temp &&
482                         tiff_unit->temp->get_color_model() != color_model)
483                 {
484                         delete tiff_unit->temp;
485                         tiff_unit->temp = 0;
486                 }
487                 if(!tiff_unit->temp)
488                 {
489                         tiff_unit->temp = new VFrame(0,
490                                 asset->width,
491                                 asset->height,
492                                 color_model);
493                 }
495                 cmodel_transfer(tiff_unit->temp->get_rows(), 
496                         frame->get_rows(),
497                         tiff_unit->temp->get_y(),
498                         tiff_unit->temp->get_u(),
499                         tiff_unit->temp->get_v(),
500                         frame->get_y(),
501                         frame->get_u(),
502                         frame->get_v(),
503                         0, 
504                         0, 
505                         frame->get_w(), 
506                         frame->get_h(),
507                         0, 
508                         0, 
509                         frame->get_w(), 
510                         frame->get_h(),
511                         frame->get_color_model(), 
512                         color_model,
513                         0,
514                         frame->get_w(),
515                         frame->get_w());
516                 for(int i = 0; i < asset->height; i++)
517                 {
518                         TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
519                 }
520         }
522         TIFFClose(stream);
524 //printf("FileTIFF::write_frame 10\n");
525         return result;
528 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
530         return new FileTIFFUnit(this, writer);
540 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
541  : FrameWriterUnit(writer)
543         this->file = file;
544         temp = 0;
547 FileTIFFUnit::~FileTIFFUnit()
549         if(temp) delete temp;
563 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
564  : BC_Window(PROGRAM_NAME ": Video Compression",
565         parent_window->get_abs_cursor_x(1),
566         parent_window->get_abs_cursor_y(1),
567         400,
568         200)
570         this->parent_window = parent_window;
571         this->asset = asset;
574 TIFFConfigVideo::~TIFFConfigVideo()
578 int TIFFConfigVideo::create_objects()
580         int x = 10, y = 10;
582         add_subwindow(new BC_Title(x, y, "Colorspace:"));
583         TIFFColorspace *menu1;
584         add_subwindow(menu1 = new TIFFColorspace(this, x + 150, y, 200));
585         menu1->create_objects();
586         y += 40;
587         add_subwindow(new BC_Title(x, y, "Compression:"));
588         TIFFCompression *menu2;
589         add_subwindow(menu2 = new TIFFCompression(this, x + 150, y, 200));
590         menu2->create_objects();
592         add_subwindow(new BC_OKButton(this));
593         return 0;
596 int TIFFConfigVideo::close_event()
598         set_done(0);
599         return 1;
607 TIFFColorspace::TIFFColorspace(TIFFConfigVideo *gui, int x, int y, int w)
608  : BC_PopupMenu(x,
609         y,
610         w,
611         FileTIFF::cmodel_to_str(gui->asset->tiff_cmodel))
613         this->gui = gui;
615 int TIFFColorspace::handle_event()
617         return 1;
619 void TIFFColorspace::create_objects()
621         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_888));
622 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_16161616));
623         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_8888));
624 //      add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_16161616));
625         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGB_FLOAT));
626         add_item(new TIFFColorspaceItem(gui, FileTIFF::RGBA_FLOAT));
630 TIFFColorspaceItem::TIFFColorspaceItem(TIFFConfigVideo *gui, int value)
631  : BC_MenuItem(FileTIFF::cmodel_to_str(value))
633         this->gui = gui;
634         this->value = value;
636 int TIFFColorspaceItem::handle_event()
638         gui->asset->tiff_cmodel = value;
639         return 0;
648 TIFFCompression::TIFFCompression(TIFFConfigVideo *gui, int x, int y, int w)
649  : BC_PopupMenu(x, y, w, FileTIFF::compression_to_str(gui->asset->tiff_compression))
651         this->gui = gui;
653 int TIFFCompression::handle_event()
655         return 1;
657 void TIFFCompression::create_objects()
659         add_item(new TIFFCompressionItem(gui, FileTIFF::NONE));
660 //      add_item(new TIFFCompressionItem(gui, FileTIFF::LZW));
661         add_item(new TIFFCompressionItem(gui, FileTIFF::PACK_BITS));
662 //      add_item(new TIFFCompressionItem(gui, FileTIFF::DEFLATE));
663 //      add_item(new TIFFCompressionItem(gui, FileTIFF::JPEG));
670 TIFFCompressionItem::TIFFCompressionItem(TIFFConfigVideo *gui, int value)
671  : BC_MenuItem(FileTIFF::compression_to_str(value))
673         this->gui = gui;
674         this->value = value;
676 int TIFFCompressionItem::handle_event()
678         gui->asset->tiff_compression = value;
679         return 0;