r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / filetiff.C
blob3888d85065aef3baa1a1b72219b2fa06210e9e05
1 #include "assets.h"
2 #include "edit.h"
3 #include "file.h"
4 #include "filetiff.h"
5 #include "vframe.h"
7 #include <stdint.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <libintl.h>
11 #define _(String) gettext(String)
12 #define gettext_noop(String) String
13 #define N_(String) gettext_noop (String)
15 FileTIFF::FileTIFF(Asset *asset, File *file)
16  : FileList(asset, file, "TIFFLIST", ".tif", FILE_TIFF, FILE_TIFF_LIST)
18         asset->video_data = 1;
19         temp = 0;
22 FileTIFF::~FileTIFF()
24         if(temp) delete temp;
28 void FileTIFF::get_parameters(BC_WindowBase *parent_window, 
29         Asset *asset, 
30         BC_WindowBase* &format_window,
31         int audio_options,
32         int video_options)
34         if(video_options)
35         {
36                 TIFFConfigVideo *window = new TIFFConfigVideo(parent_window, asset);
37                 format_window = window;
38                 window->create_objects();
39                 window->run_window();
40                 delete window;
41         }
45 int FileTIFF::check_sig(Asset *asset)
47         FILE *stream = fopen(asset->path, "rb");
49         if(stream)
50         {
51                 char test[10];
52                 fread(test, 10, 1, stream);
53                 fclose(stream);
55                 if(test[0] == 'I' && test[1] == 'I')
56                 {
57                         return 1;
58                 }
59                 else
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')
62                 {
63                         return 1;
64                 }
65         }
66         return 0;
69 int FileTIFF::can_copy_from(Edit *edit, int64_t position)
71         if(edit->asset->format == FILE_TIFF_LIST ||
72                 edit->asset->format == FILE_TIFF)
73                 return 1;
74         
75         return 0;
78 #define TIFF_RGB "rgb "
79 #define TIFF_RGBA "rgba"
82 int FileTIFF::read_frame_header(char *path)
84         TIFF *stream;
85         int result = 0;
87         if(!(stream = TIFFOpen(path, "rb")))
88         {
89                 perror("FileTIFF::read_header");
90                 return 1;
91         }
93         TIFFGetField(stream, TIFFTAG_IMAGEWIDTH, &(asset->width));
94         TIFFGetField(stream, TIFFTAG_IMAGELENGTH, &(asset->height));
95         int depth = 3;
96 //      TIFFGetField(stream, TIFFTAG_IMAGEDEPTH, &depth);
97         TIFFGetField(stream, TIFFTAG_SAMPLESPERPIXEL, &depth);
98         if(depth == 3)
99                 strcpy(asset->vcodec, TIFF_RGB);
100         else
101                 strcpy(asset->vcodec, TIFF_RGBA);
103 //printf("FileTIFF::read_frame_header 1 %d\n", depth);
104         TIFFClose(stream);
107         return result;
110 int FileTIFF::colormodel_supported(int colormodel)
112         if(!strcmp(asset->vcodec, TIFF_RGB))
113                 return BC_RGB888;
114         else
115                 return BC_RGBA8888;
118 int FileTIFF::get_best_colormodel(Asset *asset, int driver)
120         if(!strcmp(asset->vcodec, TIFF_RGB))
121                 return BC_RGB888;
122         else
123                 return BC_RGBA8888;
127 static tsize_t tiff_read(thandle_t ptr, tdata_t buf, tsize_t size)
129         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
130         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
131                 return 0;
132         memcpy(buf, tiff_unit->data->get_data() + tiff_unit->offset, size);
133         tiff_unit->offset += size;
134         return size;
137 static tsize_t tiff_write(thandle_t ptr, tdata_t buf, tsize_t size)
139         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
140 //printf("tiff_write 1 %d\n", size);
141         if(tiff_unit->data->get_compressed_allocated() < tiff_unit->offset + size)
142         {
143                 tiff_unit->data->allocate_compressed_data((tiff_unit->offset + size) * 2);
144         }
147         if(tiff_unit->data->get_compressed_size() < tiff_unit->offset + size)
148                 tiff_unit->data->set_compressed_size(tiff_unit->offset + size);
149         memcpy(tiff_unit->data->get_data() + tiff_unit->offset,
150                 buf,
151                 size);
152         tiff_unit->offset += size;
153 //printf("tiff_write 2\n");
154         return size;
157 static toff_t tiff_seek(thandle_t ptr, toff_t off, int whence)
159         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
160 //printf("tiff_seek 1 %d %d\n", off, whence);
161         switch(whence)
162         {
163                 case SEEK_SET:
164                         tiff_unit->offset = off;
165                         break;
166                 case SEEK_CUR:
167                         tiff_unit->offset += off;
168                         break;
169                 case SEEK_END:
170                         tiff_unit->offset = tiff_unit->data->get_compressed_size() + off;
171                         break;
172         }
173 //printf("tiff_seek 2\n");
174         return tiff_unit->offset;
177 static int tiff_close(thandle_t ptr)
179         return 0;
182 static toff_t tiff_size(thandle_t ptr)
184         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
185         return tiff_unit->data->get_compressed_size();
188 static int tiff_mmap(thandle_t ptr, tdata_t* pbase, toff_t* psize)
190 //printf("tiff_mmap 1\n");
191         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)ptr;
192         *pbase = tiff_unit->data->get_data();
193         *psize = tiff_unit->data->get_compressed_size();
194 //printf("tiff_mmap 10\n");
195         return 0;
198 void tiff_unmap(thandle_t ptr, tdata_t base, toff_t size)
202 int FileTIFF::read_frame(VFrame *output, VFrame *input)
204         FileTIFFUnit *unit = new FileTIFFUnit(this, 0);
205         TIFF *stream;
206         unit->offset = 0;
207         unit->data = input;
209         stream = TIFFClientOpen("FileTIFF", 
210                 "r",
211             (void*)unit,
212             tiff_read, 
213                 tiff_write,
214             tiff_seek, 
215                 tiff_close,
216             tiff_size,
217             tiff_mmap, 
218                 tiff_unmap);
220 //printf("FileTIFF::read_frame 1 %d\n", output->get_color_model());
221         if(output->get_color_model() == BC_RGBA8888 ||
222                 output->get_color_model() == BC_RGB888)
223         {
224 //printf("FileTIFF::read_frame 2\n");
225                 for(int i = 0; i < asset->height; i++)
226                 {
227                         TIFFReadScanline(stream, output->get_rows()[i], i, 0);
228                 }
229 //printf("FileTIFF::read_frame 4\n");
230         }
231         else
232         {
233                 printf("FileTIFF::read_frame color model = %d\n",
234                         output->get_color_model());
235         }
237         TIFFClose(stream);
238         delete unit;
240         return 0;
243 int FileTIFF::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
245         FileTIFFUnit *tiff_unit = (FileTIFFUnit*)unit;
246         int result = 0;
247         TIFF *stream;
248         tiff_unit->offset = 0;
249         tiff_unit->data = data;
250         tiff_unit->data->set_compressed_size(0);
252 //printf("FileTIFF::write_frame 1\n");
253         TIFFConfigVideo::fix_codec(asset->vcodec);
254 //printf("FileTIFF::write_frame 1\n");
255         stream = TIFFClientOpen("FileTIFF", 
256                 "w",
257             (void*)tiff_unit,
258             tiff_read, 
259                 tiff_write,
260             tiff_seek, 
261                 tiff_close,
262             tiff_size,
263             tiff_mmap, 
264                 tiff_unmap);
266 //printf("FileTIFF::write_frame 1\n");
267         int depth, color_model;
268         if(!strcmp(asset->vcodec, TIFF_RGBA))
269         {
270                 depth = 32;
271                 color_model = BC_RGBA8888;
272         }
273         else
274         {
275                 depth = 24;
276                 color_model = BC_RGB888;
277         }
279 //printf("FileTIFF::write_frame 1\n");
280         TIFFSetField(stream, TIFFTAG_IMAGEWIDTH, asset->width);
281         TIFFSetField(stream, TIFFTAG_IMAGELENGTH, asset->height);
282         TIFFSetField(stream, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
283         TIFFSetField(stream, TIFFTAG_SAMPLESPERPIXEL, depth / 8);
284         TIFFSetField(stream, TIFFTAG_BITSPERSAMPLE, 8);
285         TIFFSetField(stream, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
286         TIFFSetField(stream, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(stream, (uint32_t)-1));
287         TIFFSetField(stream, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
289 //printf("FileTIFF::write_frame 1\n");
290         if(frame->get_color_model() == color_model)
291         {
292                 for(int i = 0; i < asset->height; i++)
293                 {
294 //printf("FileTIFF::write_frame 2 %d\n", i);
295                         TIFFWriteScanline(stream, frame->get_rows()[i], i, 0);
296 //printf("FileTIFF::write_frame 3\n");
297                 }
298         }
299         else
300         {
301 //printf("FileTIFF::write_frame 2\n");
302                 if(tiff_unit->temp &&
303                         tiff_unit->temp->get_color_model() != color_model)
304                 {
305                         delete tiff_unit->temp;
306                         tiff_unit->temp = 0;
307                 }
308                 if(!tiff_unit->temp)
309                 {
310                         tiff_unit->temp = new VFrame(0,
311                                 asset->width,
312                                 asset->height,
313                                 color_model);
314                 }
315 //printf("FileTIFF::write_frame 3 %d %d\n", color_model, frame->get_color_model());
316                 cmodel_transfer(tiff_unit->temp->get_rows(), 
317                         frame->get_rows(),
318                         tiff_unit->temp->get_y(),
319                         tiff_unit->temp->get_u(),
320                         tiff_unit->temp->get_v(),
321                         frame->get_y(),
322                         frame->get_u(),
323                         frame->get_v(),
324                         0, 
325                         0, 
326                         frame->get_w(), 
327                         frame->get_h(),
328                         0, 
329                         0, 
330                         frame->get_w(), 
331                         frame->get_h(),
332                         frame->get_color_model(), 
333                         color_model,
334                         0,
335                         frame->get_w(),
336                         frame->get_w());
337 //printf("FileTIFF::write_frame 5\n");
338                 for(int i = 0; i < asset->height; i++)
339                 {
340                         TIFFWriteScanline(stream, tiff_unit->temp->get_rows()[i], i, 0);
341                 }
342 //printf("FileTIFF::write_frame 6\n");
343         }
344 //printf("FileTIFF::write_frame 7\n");
345 //sleep(1);
346 //printf("FileTIFF::write_frame 71\n");
348         TIFFClose(stream);
349 //printf("FileTIFF::write_frame 8\n");
351         return result;
354 FrameWriterUnit* FileTIFF::new_writer_unit(FrameWriter *writer)
356         return new FileTIFFUnit(this, writer);
366 FileTIFFUnit::FileTIFFUnit(FileTIFF *file, FrameWriter *writer)
367  : FrameWriterUnit(writer)
369         this->file = file;
370         temp = 0;
373 FileTIFFUnit::~FileTIFFUnit()
375         if(temp) delete temp;
389 TIFFConfigVideo::TIFFConfigVideo(BC_WindowBase *parent_window, Asset *asset)
390  : BC_Window(PROGRAM_NAME ": Video Compression",
391         parent_window->get_abs_cursor_x(),
392         parent_window->get_abs_cursor_y(),
393         400,
394         100)
396         this->parent_window = parent_window;
397         this->asset = asset;
400 TIFFConfigVideo::~TIFFConfigVideo()
404 int TIFFConfigVideo::create_objects()
406         int x = 10, y = 10;
408         fix_codec(asset->vcodec);
409         add_subwindow(new TIFFConfigAlpha(this, x, y));
411         add_subwindow(new BC_OKButton(this));
412         return 0;
415 int TIFFConfigVideo::close_event()
417         set_done(0);
418         return 1;
421 char* TIFFConfigVideo::alpha_to_codec(int use_alpha)
423         if(use_alpha) 
424                 return TIFF_RGBA;
425         else
426                 return TIFF_RGB;
429 int TIFFConfigVideo::codec_to_alpha(char *codec)
431         if(!strcmp(codec, TIFF_RGBA))
432                 return 1;
433         else
434                 return 0;
437 void TIFFConfigVideo::fix_codec(char *codec)
439         if(strcmp(codec, TIFF_RGB) &&
440                 strcmp(codec, TIFF_RGBA))
441                 strcpy(codec, TIFF_RGB);
445 TIFFConfigAlpha::TIFFConfigAlpha(TIFFConfigVideo *gui, int x, int y)
446  : BC_CheckBox(x, 
447         y, 
448         TIFFConfigVideo::codec_to_alpha(gui->asset->vcodec), 
449         _("Use alpha"))
451         this->gui = gui;
454 int TIFFConfigAlpha::handle_event()
456         if(TIFFConfigVideo::codec_to_alpha(gui->asset->vcodec))
457         {
458                 strcpy(gui->asset->vcodec, TIFF_RGB);
459         }
460         else
461                 strcpy(gui->asset->vcodec, TIFF_RGBA);
462         
463         update(TIFFConfigVideo::codec_to_alpha(gui->asset->vcodec));
464         return 1;