4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
27 #include "mwindow.inc"
28 #include "quicktime.h"
30 #include "videodevice.inc"
31 #include "mainerror.h"
36 FilePNG::FilePNG(Asset *asset, File *file)
37 : FileList(asset, file, "PNGLIST", ".png", FILE_PNG, FILE_PNG_LIST)
47 int FilePNG::check_sig(Asset *asset)
49 FILE *stream = fopen(asset->path, "rb");
54 //printf("FilePNG::check_sig 1\n");
56 (void)fread(test, 16, 1, stream);
59 // if(png_sig_cmp((unsigned char*)test, 0, 8))
60 if(png_check_sig((unsigned char*)test, 8))
62 //printf("FilePNG::check_sig 1\n");
66 if(test[0] == 'P' && test[1] == 'N' && test[2] == 'G' &&
67 test[3] == 'L' && test[4] == 'I' && test[5] == 'S' && test[6] == 'T')
69 //printf("FilePNG::check_sig 1\n");
78 void FilePNG::get_parameters(BC_WindowBase *parent_window,
80 BC_WindowBase* &format_window,
86 PNGConfigVideo *window = new PNGConfigVideo(parent_window, asset);
87 format_window = window;
88 window->create_objects();
97 int FilePNG::can_copy_from(Asset *asset, int64_t position)
99 if(asset->format == FILE_MOV)
101 if(match4(asset->vcodec, QUICKTIME_PNG)) return 1;
104 if(asset->format == FILE_PNG ||
105 asset->format == FILE_PNG_LIST)
112 int FilePNG::colormodel_supported(int colormodel)
114 if (((colormodel == BC_RGBA8888) && (native_cmodel == BC_RGBA16161616))
115 || ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA16161616))
116 || (colormodel == BC_RGB888))
120 else if ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA8888))
126 return native_cmodel;
131 int FilePNG::get_best_colormodel(Asset *asset, int driver)
133 if(asset->png_use_alpha)
142 int FilePNG::read_frame_header(char *path)
151 if(!(stream = fopen(path, "rb")))
153 eprintf("Error while opening \"%s\" for reading. \n%m\n", asset->path);
159 png_infop end_info = 0;
160 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
161 info_ptr = png_create_info_struct(png_ptr);
162 png_init_io(png_ptr, stream);
165 png_read_info(png_ptr, info_ptr);
167 asset->width = png_get_image_width(png_ptr, info_ptr);
168 asset->height = png_get_image_height(png_ptr, info_ptr);
169 color_type = png_get_color_type(png_ptr, info_ptr);
170 color_depth = png_get_bit_depth(png_ptr,info_ptr);
172 png_get_tRNS(png_ptr, info_ptr, NULL, &num_trans, NULL);
174 if (color_depth == 16)
176 if (color_type & PNG_COLOR_MASK_ALPHA)
178 native_cmodel = BC_RGBA16161616;
182 native_cmodel = BC_RGB161616;
186 if ((color_type & PNG_COLOR_MASK_ALPHA)
189 native_cmodel = BC_RGBA8888;
193 native_cmodel = BC_RGB888;
197 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
208 static void read_function(png_structp png_ptr,
212 VFrame *input = (VFrame*)png_get_io_ptr(png_ptr);
214 memcpy(data, input->get_data() + input->get_compressed_size(), length);
215 input->set_compressed_size(input->get_compressed_size() + length);
218 static void write_function(png_structp png_ptr, png_bytep data, png_uint_32 length)
220 VFrame *output = (VFrame*)png_get_io_ptr(png_ptr);
221 long total_length = output->get_compressed_size() + length;
222 if(output->get_compressed_allocated() < total_length) {
223 long new_length = 2 * (output->get_compressed_allocated() + length);
224 output->allocate_compressed_data(new_length);
226 memcpy(output->get_data() + output->get_compressed_size(), data, length);
227 output->set_compressed_size(total_length);
230 static void flush_function(png_structp png_ptr)
235 int FilePNG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
237 PNGUnit *png_unit = (PNGUnit*)unit;
238 png_structp png_ptr = 0;
239 png_infop info_ptr = 0;
240 VFrame *output_frame;
242 data->set_compressed_size(0);
243 //printf("FilePNG::write_frame 1\n");
244 native_cmodel = asset->png_use_alpha ? BC_RGBA8888 : BC_RGB888;
245 if(frame->get_color_model() != native_cmodel)
247 if(!png_unit->temp_frame) png_unit->temp_frame = new VFrame(0,
248 -1, asset->width, asset->height, native_cmodel, -1);
250 png_unit->temp_frame->transfer_from(frame);
251 output_frame = png_unit->temp_frame;
254 output_frame = frame;
256 //printf("FilePNG::write_frame 1\n");
257 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
258 if( png_ptr && !setjmp(png_jmpbuf(png_ptr)) ) {
259 info_ptr = png_create_info_struct(png_ptr);
260 png_set_write_fn(png_ptr, data,
261 (png_rw_ptr)write_function, (png_flush_ptr)flush_function);
262 png_set_compression_level(png_ptr, 5);
264 png_set_IHDR(png_ptr, info_ptr, asset->width, asset->height, 8,
265 asset->png_use_alpha ? PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
266 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
267 png_write_info(png_ptr, info_ptr);
268 png_write_image(png_ptr, output_frame->get_rows());
269 png_write_end(png_ptr, info_ptr);
270 png_destroy_write_struct(&png_ptr, &info_ptr);
274 char error[256]; png_error(png_ptr, error);
275 fprintf(stderr, "FilePNG::write_frame failed: %s\n", error);
278 png_destroy_write_struct(&png_ptr, &info_ptr);
279 //printf("FilePNG::write_frame 3 %d\n", data->get_compressed_size());
283 int FilePNG::read_frame(VFrame *output, VFrame *input)
287 png_infop end_info = 0;
292 int size = input->get_compressed_size();
293 input->set_compressed_size(0);
296 //printf("FilePNG::read_frame 1 %d %d\n", native_cmodel, output->get_color_model());
297 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
298 info_ptr = png_create_info_struct(png_ptr);
299 png_set_read_fn(png_ptr, input, (png_rw_ptr)read_function);
300 png_read_info(png_ptr, info_ptr);
302 int png_color_type = png_get_color_type(png_ptr, info_ptr);
303 if (png_color_type == PNG_COLOR_TYPE_GRAY ||
304 png_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
306 png_set_gray_to_rgb(png_ptr);
309 colormodel = output->get_color_model();
310 color_type = png_get_color_type(png_ptr, info_ptr);
311 color_depth = png_get_bit_depth(png_ptr,info_ptr);
313 if (((native_cmodel == BC_RGBA16161616)||(native_cmodel == BC_RGB161616))
314 && ((colormodel == BC_RGBA8888)||(colormodel == BC_RGB888)))
316 png_set_strip_16(png_ptr);
319 /* If we're dropping the alpha channel, use the background color of the image
320 otherwise, use black */
321 if (((native_cmodel == BC_RGBA16161616)||(native_cmodel == BC_RGBA8888))
322 && ((colormodel == BC_RGB161616)||(colormodel == BC_RGB888)))
324 png_color_16 my_background;
325 png_color_16p image_background;
327 memset(&my_background,0,sizeof(png_color_16));
329 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
331 png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
335 png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
340 if ((color_depth == 16)
341 &&((colormodel == BC_RGBA16161616)||(colormodel == BC_RGB161616)))
343 png_set_swap(png_ptr);
346 if (!(color_type & PNG_COLOR_MASK_COLOR))
348 png_set_gray_to_rgb(png_ptr);
351 if (color_type & PNG_COLOR_MASK_PALETTE)
353 png_set_palette_to_rgb(png_ptr);
356 if (color_depth <= 8)
358 png_set_expand(png_ptr);
362 png_read_image(png_ptr, output->get_rows());
363 //printf("FilePNG::read_frame 3\n");
364 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
366 input->set_compressed_size(size);
368 //printf("FilePNG::read_frame 4\n");
372 FrameWriterUnit* FilePNG::new_writer_unit(FrameWriter *writer)
374 return new PNGUnit(this, writer);
388 PNGUnit::PNGUnit(FilePNG *file, FrameWriter *writer)
389 : FrameWriterUnit(writer)
396 if(temp_frame) delete temp_frame;
407 PNGConfigVideo::PNGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
408 : BC_Window(PROGRAM_NAME ": Video Compression",
409 parent_window->get_abs_cursor_x(1),
410 parent_window->get_abs_cursor_y(1),
414 this->parent_window = parent_window;
418 PNGConfigVideo::~PNGConfigVideo()
422 void PNGConfigVideo::create_objects()
424 lock_window("PNGConfigVideo::create_objects()");
426 add_subwindow(new PNGUseAlpha(this, x, y));
427 add_subwindow(new BC_OKButton(this));
432 int PNGConfigVideo::close_event()
439 PNGUseAlpha::PNGUseAlpha(PNGConfigVideo *gui, int x, int y)
440 : BC_CheckBox(x, y, gui->asset->png_use_alpha, _("Use alpha"))
445 int PNGUseAlpha::handle_event()
447 gui->asset->png_use_alpha = get_value();