modify clr btn 16 plugins, add regdmp for sigtraps, rework mask_engine, mask rotate...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / filepng.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "asset.h"
23 #include "edit.h"
24 #include "file.h"
25 #include "filepng.h"
26 #include "interlacemodes.h"
27 #include "language.h"
28 #include "mwindow.inc"
29 #include "vframe.h"
30 #include "videodevice.inc"
31 #include "mainerror.h"
32
33 #include <png.h>
34 #include <string.h>
35
36 FilePNG::FilePNG(Asset *asset, File *file)
37  : FileList(asset, file, "PNGLIST", ".png", FILE_PNG, FILE_PNG_LIST)
38 {
39         native_cmodel = -1;
40 }
41
42 FilePNG::~FilePNG()
43 {
44 }
45
46
47
48 int FilePNG::check_sig(Asset *asset)
49 {
50         FILE *stream = fopen(asset->path, "rb");
51
52         if(stream)
53         {
54
55 //printf("FilePNG::check_sig 1\n");
56                 char test[16];
57                 (void)fread(test, 16, 1, stream);
58                 fclose(stream);
59
60 //              if(png_sig_cmp((unsigned char*)test, 0, 8))
61                 if(png_check_sig((unsigned char*)test, 8))
62                 {
63 //printf("FilePNG::check_sig 1\n");
64                         return 1;
65                 }
66                 else
67                 if(test[0] == 'P' && test[1] == 'N' && test[2] == 'G' &&
68                         test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
69                 {
70 //printf("FilePNG::check_sig 1\n");
71                         return 1;
72                 }
73         }
74         return 0;
75 }
76
77
78
79 void FilePNG::get_parameters(BC_WindowBase *parent_window,
80         Asset *asset, BC_WindowBase* &format_window,
81         int audio_options, int video_options, EDL *edl)
82 {
83         if(video_options)
84         {
85                 PNGConfigVideo *window = new PNGConfigVideo(parent_window, asset);
86                 format_window = window;
87                 window->create_objects();
88                 window->run_window();
89                 delete window;
90         }
91 }
92
93
94
95
96 int FilePNG::can_copy_from(Asset *asset, int64_t position)
97 {
98         if(asset->format == FILE_PNG ||
99                 asset->format == FILE_PNG_LIST)
100                 return 1;
101
102         return 0;
103 }
104
105
106 int FilePNG::colormodel_supported(int colormodel)
107 {
108         if( ((colormodel == BC_RGBA8888)  && (native_cmodel == BC_RGBA16161616)) ||
109             ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA16161616)) ||
110              (colormodel == BC_RGB888) || (colormodel == BC_RGBA8888) )
111                 return colormodel;
112         if( (colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA8888) )
113                 return BC_RGB888;
114         if( native_cmodel >= 0 )
115                 return native_cmodel;
116         return asset->png_use_alpha ? BC_RGBA8888 : BC_RGB888;
117 }
118
119
120 int FilePNG::get_best_colormodel(Asset *asset, int driver)
121 {
122         if(asset->png_use_alpha)
123                 return BC_RGBA8888;
124         else
125                 return BC_RGB888;
126 }
127
128
129
130
131 int FilePNG::read_frame_header(char *path)
132 {
133         int result = 0;
134         int color_type;
135         int color_depth;
136         int num_trans = 0;
137
138         FILE *stream;
139
140         if(!(stream = fopen(path, "rb")))
141         {
142                 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
143                 return 1;
144         }
145
146         png_structp png_ptr;
147         png_infop info_ptr;
148         png_infop end_info = 0;
149         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
150         info_ptr = png_create_info_struct(png_ptr);
151         png_init_io(png_ptr, stream);
152
153
154         png_read_info(png_ptr, info_ptr);
155
156         asset->width = png_get_image_width(png_ptr, info_ptr);
157         asset->height = png_get_image_height(png_ptr, info_ptr);
158         asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
159         color_type = png_get_color_type(png_ptr, info_ptr);
160         color_depth = png_get_bit_depth(png_ptr,info_ptr);
161
162         png_get_tRNS(png_ptr, info_ptr, NULL, &num_trans, NULL);
163         native_cmodel = color_depth == 16 ?
164             ((color_type & PNG_COLOR_MASK_ALPHA) ?
165                 BC_RGBA16161616 : BC_RGB161616) :
166             ((color_type & PNG_COLOR_MASK_ALPHA) || (num_trans > 0) ?
167                 BC_RGBA8888 : BC_RGB888);
168         asset->png_use_alpha = BC_CModels::has_alpha(native_cmodel) ? 1 : 0;
169
170         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
171         fclose(stream);
172         return result;
173 }
174
175
176
177
178 static void read_function(png_structp png_ptr,
179         png_bytep data,
180         png_uint_32 length)
181 {
182         VFrame *input = (VFrame*)png_get_io_ptr(png_ptr);
183
184         memcpy(data, input->get_data() + input->get_compressed_size(), length);
185         input->set_compressed_size(input->get_compressed_size() + length);
186 }
187
188 static void write_function(png_structp png_ptr, png_bytep data, png_uint_32 length)
189 {
190         VFrame *output = (VFrame*)png_get_io_ptr(png_ptr);
191         long total_length = output->get_compressed_size() + length;
192         if(output->get_compressed_allocated() < total_length) {
193                 long new_length = 2 * (output->get_compressed_allocated() + length);
194                 output->allocate_compressed_data(new_length);
195         }
196         memcpy(output->get_data() + output->get_compressed_size(), data, length);
197         output->set_compressed_size(total_length);
198 }
199
200 static void flush_function(png_structp png_ptr)
201 {
202 }
203
204
205 int FilePNG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
206 {
207         PNGUnit *png_unit = (PNGUnit*)unit;
208         png_structp png_ptr = 0;
209         png_infop info_ptr = 0;
210         VFrame *output_frame;
211         int result = 1;
212         data->set_compressed_size(0);
213 //printf("FilePNG::write_frame 1\n");
214         native_cmodel = asset->png_use_alpha ? BC_RGBA8888 : BC_RGB888;
215         if(frame->get_color_model() != native_cmodel)
216         {
217                 if(!png_unit->temp_frame) png_unit->temp_frame =
218                         new VFrame(asset->width, asset->height, native_cmodel, 0);
219
220                 png_unit->temp_frame->transfer_from(frame);
221                 output_frame = png_unit->temp_frame;
222         }
223         else
224                 output_frame = frame;
225
226 //printf("FilePNG::write_frame 1\n");
227         png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
228         if( png_ptr ) {
229                 if( !setjmp(png_jmpbuf(png_ptr)) ) {
230                         info_ptr = png_create_info_struct(png_ptr);
231                         png_set_write_fn(png_ptr, data,
232                                 (png_rw_ptr)write_function, (png_flush_ptr)flush_function);
233                         png_set_compression_level(png_ptr, 5);
234
235                         png_set_IHDR(png_ptr, info_ptr, asset->width, asset->height, 8,
236                                 asset->png_use_alpha ?  PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
237                                 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
238                         png_write_info(png_ptr, info_ptr);
239                         png_write_image(png_ptr, output_frame->get_rows());
240                         png_write_end(png_ptr, info_ptr);
241                         result = 0;
242                 }
243                 png_destroy_write_struct(&png_ptr, &info_ptr);
244         }
245         if( result ) {
246                 char error[256];  png_error(png_ptr, error);
247                 fprintf(stderr, "FilePNG::write_frame failed: %s\n", error);
248         }
249 //printf("FilePNG::write_frame 3 %d\n", data->get_compressed_size());
250         return result;
251 }
252
253 int FilePNG::read_frame(VFrame *output, VFrame *input)
254 {
255         png_structp png_ptr;
256         png_infop info_ptr;
257         png_infop end_info = 0;
258         int result = 0;
259         int color_type;
260         int color_depth;
261         int colormodel;
262         int size = input->get_compressed_size();
263         input->set_compressed_size(0);
264
265         //printf("FilePNG::read_frame 1 %d %d\n", native_cmodel, output->get_color_model());
266         png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
267         info_ptr = png_create_info_struct(png_ptr);
268         png_set_read_fn(png_ptr, input, (png_rw_ptr)read_function);
269         png_read_info(png_ptr, info_ptr);
270
271         int png_color_type = png_get_color_type(png_ptr, info_ptr);
272         if( png_color_type == PNG_COLOR_TYPE_GRAY ||
273             png_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
274                 png_set_gray_to_rgb(png_ptr);
275
276         colormodel = output->get_color_model();
277         color_type = png_get_color_type(png_ptr, info_ptr);
278         color_depth = png_get_bit_depth(png_ptr,info_ptr);
279
280         int num_trans = 0;
281         png_get_tRNS(png_ptr, info_ptr, NULL, &num_trans, NULL);
282
283         native_cmodel = color_depth == 16 ?
284             ((color_type & PNG_COLOR_MASK_ALPHA) ?
285                 BC_RGBA16161616 : BC_RGB161616) :
286             ((color_type & PNG_COLOR_MASK_ALPHA) || (num_trans > 0) ?
287                 BC_RGBA8888 : BC_RGB888);
288
289         if( ((native_cmodel == BC_RGBA16161616) || (native_cmodel == BC_RGB161616)) &&
290             ((colormodel == BC_RGBA8888) || (colormodel == BC_RGB888)) )
291                 png_set_strip_16(png_ptr);
292
293 // If we're dropping the alpha channel
294         if( !BC_CModels::has_alpha(colormodel) && BC_CModels::has_alpha(native_cmodel) ) {
295                 png_color_16 my_background;
296                 png_color_16p image_background;
297                 memset(&my_background,0,sizeof(png_color_16));
298 // use the background color of the image
299                 if( png_get_bKGD(png_ptr, info_ptr, &image_background) )
300                         png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
301                 else
302 // otherwise, use black
303                         png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
304         }
305         else if( BC_CModels::has_alpha(colormodel) && !BC_CModels::has_alpha(native_cmodel) )
306 // If we're adding the alpha channel, alpha = max pixel value
307                 png_set_add_alpha(png_ptr, BC_CModels::calculate_max(colormodel), PNG_FILLER_AFTER);
308
309 // Little endian
310         if( (color_depth == 16) &&
311             ((colormodel == BC_RGBA16161616) || (colormodel == BC_RGB161616)) )
312                 png_set_swap(png_ptr);
313
314         if( !(color_type & PNG_COLOR_MASK_COLOR) )
315                 png_set_gray_to_rgb(png_ptr);
316
317         if( color_type & PNG_COLOR_MASK_PALETTE )
318                 png_set_palette_to_rgb(png_ptr);
319
320         if( color_depth <= 8 )
321                 png_set_expand(png_ptr);
322
323 // read the image
324         png_read_image(png_ptr, output->get_rows());
325
326 //printf("FilePNG::read_frame 3\n");
327         png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
328         input->set_compressed_size(size);
329 //printf("FilePNG::read_frame 4\n");
330         return result;
331 }
332
333 FrameWriterUnit* FilePNG::new_writer_unit(FrameWriter *writer)
334 {
335         return new PNGUnit(this, writer);
336 }
337
338
339
340
341
342
343
344
345
346
347
348
349 PNGUnit::PNGUnit(FilePNG *file, FrameWriter *writer)
350  : FrameWriterUnit(writer)
351 {
352         this->file = file;
353         temp_frame = 0;
354 }
355 PNGUnit::~PNGUnit()
356 {
357         if(temp_frame) delete temp_frame;
358 }
359
360
361
362
363
364
365
366
367
368 PNGConfigVideo::PNGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
369  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
370         parent_window->get_abs_cursor_x(1),
371         parent_window->get_abs_cursor_y(1),
372         200,
373         100)
374 {
375         this->parent_window = parent_window;
376         this->asset = asset;
377 }
378
379 PNGConfigVideo::~PNGConfigVideo()
380 {
381 }
382
383 void PNGConfigVideo::create_objects()
384 {
385         lock_window("PNGConfigVideo::create_objects");
386         int x = 10, y = 10;
387         add_subwindow(new PNGUseAlpha(this, x, y));
388         add_subwindow(new BC_OKButton(this));
389         show_window(1);
390         unlock_window();
391 }
392
393 int PNGConfigVideo::close_event()
394 {
395         set_done(0);
396         return 1;
397 }
398
399
400 PNGUseAlpha::PNGUseAlpha(PNGConfigVideo *gui, int x, int y)
401  : BC_CheckBox(x, y, gui->asset->png_use_alpha, _("Use alpha"))
402 {
403         this->gui = gui;
404 }
405
406 int PNGUseAlpha::handle_event()
407 {
408         gui->asset->png_use_alpha = get_value();
409         return 1;
410 }
411
412
413