rework histogram_bezier, init wm icon set_icon(gg), update de.po+msg/txt
[goodguy/history.git] / cinelerra-5.1 / cinelerra / filepng.C
index 6ccbf28819f51343957018d075574d54f45cbbbb..d4b581d7f0f9f40bda46305f6728a806976963c9 100644 (file)
@@ -36,6 +36,7 @@
 FilePNG::FilePNG(Asset *asset, File *file)
  : FileList(asset, file, "PNGLIST", ".png", FILE_PNG, FILE_PNG_LIST)
 {
+       native_cmodel = -1;
 }
 
 FilePNG::~FilePNG()
@@ -76,10 +77,8 @@ int FilePNG::check_sig(Asset *asset)
 
 
 void FilePNG::get_parameters(BC_WindowBase *parent_window,
-       Asset *asset,
-       BC_WindowBase* &format_window,
-       int audio_options,
-       int video_options)
+       Asset *asset, BC_WindowBase* &format_window,
+       int audio_options, int video_options, EDL *edl)
 {
        if(video_options)
        {
@@ -106,20 +105,15 @@ int FilePNG::can_copy_from(Asset *asset, int64_t position)
 
 int FilePNG::colormodel_supported(int colormodel)
 {
-       if (((colormodel == BC_RGBA8888) && (native_cmodel == BC_RGBA16161616))
-           || ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA16161616))
-           || (colormodel == BC_RGB888))
-       {
-           return colormodel;
-       }
-       else if ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA8888))
-       {
-           return BC_RGB888;
-       }
-       else
-       {
-           return native_cmodel;
-       }
+       if( ((colormodel == BC_RGBA8888)  && (native_cmodel == BC_RGBA16161616)) ||
+           ((colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA16161616)) ||
+            (colormodel == BC_RGB888) || (colormodel == BC_RGBA8888) )
+               return colormodel;
+       if( (colormodel == BC_RGB161616) && (native_cmodel == BC_RGBA8888) )
+               return BC_RGB888;
+       if( native_cmodel >= 0 )
+               return native_cmodel;
+       return asset->png_use_alpha ? BC_RGBA8888 : BC_RGB888;
 }
 
 
@@ -161,43 +155,20 @@ int FilePNG::read_frame_header(char *path)
 
        asset->width = png_get_image_width(png_ptr, info_ptr);
        asset->height = png_get_image_height(png_ptr, info_ptr);
-
-       asset->interlace_mode = BC_ILACE_MODE_NOTINTERLACED;
-
+       asset->interlace_mode = ILACE_MODE_NOTINTERLACED;
        color_type = png_get_color_type(png_ptr, info_ptr);
        color_depth = png_get_bit_depth(png_ptr,info_ptr);
 
        png_get_tRNS(png_ptr, info_ptr, NULL, &num_trans, NULL);
-
-       if (color_depth == 16)
-       {
-           if (color_type & PNG_COLOR_MASK_ALPHA)
-           {
-               native_cmodel = BC_RGBA16161616;
-           }
-           else
-           {
-               native_cmodel = BC_RGB161616;
-           }
-       }
-       else
-       if ((color_type & PNG_COLOR_MASK_ALPHA)
-           || (num_trans > 0))
-       {
-           native_cmodel = BC_RGBA8888;
-       }
-       else
-       {
-           native_cmodel = BC_RGB888;
-       }
-
+       native_cmodel = color_depth == 16 ?
+           ((color_type & PNG_COLOR_MASK_ALPHA) ?
+               BC_RGBA16161616 : BC_RGB161616) :
+           ((color_type & PNG_COLOR_MASK_ALPHA) || (num_trans > 0) ?
+               BC_RGBA8888 : BC_RGB888);
        asset->png_use_alpha = BC_CModels::has_alpha(native_cmodel) ? 1 : 0;
 
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(stream);
-
-
-
        return result;
 }
 
@@ -243,8 +214,8 @@ int FilePNG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
        native_cmodel = asset->png_use_alpha ? BC_RGBA8888 : BC_RGB888;
        if(frame->get_color_model() != native_cmodel)
        {
-               if(!png_unit->temp_frame) png_unit->temp_frame = new VFrame(0,
-                       -1, asset->width, asset->height, native_cmodel, -1);
+               if(!png_unit->temp_frame) png_unit->temp_frame =
+                       new VFrame(asset->width, asset->height, native_cmodel, 0);
 
                png_unit->temp_frame->transfer_from(frame);
                output_frame = png_unit->temp_frame;
@@ -254,27 +225,27 @@ int FilePNG::write_frame(VFrame *frame, VFrame *data, FrameWriterUnit *unit)
 
 //printf("FilePNG::write_frame 1\n");
        png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
-       if( png_ptr && !setjmp(png_jmpbuf(png_ptr)) ) {
-               info_ptr = png_create_info_struct(png_ptr);
-               png_set_write_fn(png_ptr, data,
-                       (png_rw_ptr)write_function, (png_flush_ptr)flush_function);
-               png_set_compression_level(png_ptr, 5);
-
-               png_set_IHDR(png_ptr, info_ptr, asset->width, asset->height, 8,
-                       asset->png_use_alpha ?  PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
-                       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
-               png_write_info(png_ptr, info_ptr);
-               png_write_image(png_ptr, output_frame->get_rows());
-               png_write_end(png_ptr, info_ptr);
+       if( png_ptr ) {
+               if( !setjmp(png_jmpbuf(png_ptr)) ) {
+                       info_ptr = png_create_info_struct(png_ptr);
+                       png_set_write_fn(png_ptr, data,
+                               (png_rw_ptr)write_function, (png_flush_ptr)flush_function);
+                       png_set_compression_level(png_ptr, 5);
+
+                       png_set_IHDR(png_ptr, info_ptr, asset->width, asset->height, 8,
+                               asset->png_use_alpha ?  PNG_COLOR_TYPE_RGB_ALPHA : PNG_COLOR_TYPE_RGB,
+                               PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+                       png_write_info(png_ptr, info_ptr);
+                       png_write_image(png_ptr, output_frame->get_rows());
+                       png_write_end(png_ptr, info_ptr);
+                       result = 0;
+               }
                png_destroy_write_struct(&png_ptr, &info_ptr);
-               result = 0;
        }
-       else {
+       if( result ) {
                char error[256];  png_error(png_ptr, error);
                fprintf(stderr, "FilePNG::write_frame failed: %s\n", error);
        }
-
-       png_destroy_write_struct(&png_ptr, &info_ptr);
 //printf("FilePNG::write_frame 3 %d\n", data->get_compressed_size());
        return result;
 }
@@ -291,79 +262,70 @@ int FilePNG::read_frame(VFrame *output, VFrame *input)
        int size = input->get_compressed_size();
        input->set_compressed_size(0);
 
-
        //printf("FilePNG::read_frame 1 %d %d\n", native_cmodel, output->get_color_model());
        png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
        info_ptr = png_create_info_struct(png_ptr);
        png_set_read_fn(png_ptr, input, (png_rw_ptr)read_function);
        png_read_info(png_ptr, info_ptr);
 
-       int png_color_type = png_get_color_type(png_ptr, info_ptr);
-       if (png_color_type == PNG_COLOR_TYPE_GRAY ||
-               png_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
-       {
-               png_set_gray_to_rgb(png_ptr);
-       }
+       int png_color_type = png_get_color_type(png_ptr, info_ptr);
+       if( png_color_type == PNG_COLOR_TYPE_GRAY ||
+           png_color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
+               png_set_gray_to_rgb(png_ptr);
 
        colormodel = output->get_color_model();
        color_type = png_get_color_type(png_ptr, info_ptr);
        color_depth = png_get_bit_depth(png_ptr,info_ptr);
 
-       if (((native_cmodel == BC_RGBA16161616)||(native_cmodel == BC_RGB161616))
-           && ((colormodel == BC_RGBA8888)||(colormodel == BC_RGB888)))
-       {
-           png_set_strip_16(png_ptr);
-       }
+       int num_trans = 0;
+       png_get_tRNS(png_ptr, info_ptr, NULL, &num_trans, NULL);
 
-       /* If we're dropping the alpha channel, use the background color of the image
-          otherwise, use black */
-       if (((native_cmodel == BC_RGBA16161616)||(native_cmodel == BC_RGBA8888))
-           && ((colormodel == BC_RGB161616)||(colormodel == BC_RGB888)))
-       {
-           png_color_16 my_background;
-           png_color_16p image_background;
-
-           memset(&my_background,0,sizeof(png_color_16));
-
-           if (png_get_bKGD(png_ptr, info_ptr, &image_background))
-           {
-               png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
-           }
-           else
-           {
-               png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
-           }
+       native_cmodel = color_depth == 16 ?
+           ((color_type & PNG_COLOR_MASK_ALPHA) ?
+               BC_RGBA16161616 : BC_RGB161616) :
+           ((color_type & PNG_COLOR_MASK_ALPHA) || (num_trans > 0) ?
+               BC_RGBA8888 : BC_RGB888);
+
+       if( ((native_cmodel == BC_RGBA16161616) || (native_cmodel == BC_RGB161616)) &&
+           ((colormodel == BC_RGBA8888) || (colormodel == BC_RGB888)) )
+               png_set_strip_16(png_ptr);
+
+// If we're dropping the alpha channel
+       if( !BC_CModels::has_alpha(colormodel) && BC_CModels::has_alpha(native_cmodel) ) {
+               png_color_16 my_background;
+               png_color_16p image_background;
+               memset(&my_background,0,sizeof(png_color_16));
+// use the background color of the image
+               if( png_get_bKGD(png_ptr, info_ptr, &image_background) )
+                       png_set_background(png_ptr, image_background, PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
+               else
+// otherwise, use black
+                       png_set_background(png_ptr, &my_background, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
        }
+       else if( BC_CModels::has_alpha(colormodel) && !BC_CModels::has_alpha(native_cmodel) )
+// If we're adding the alpha channel, alpha = max pixel value
+               png_set_add_alpha(png_ptr, BC_CModels::calculate_max(colormodel), PNG_FILLER_AFTER);
 
-       /* Little endian */
-       if ((color_depth == 16)
-           &&((colormodel == BC_RGBA16161616)||(colormodel == BC_RGB161616)))
-       {
-           png_set_swap(png_ptr);
-       }
+// Little endian
+       if( (color_depth == 16) &&
+           ((colormodel == BC_RGBA16161616) || (colormodel == BC_RGB161616)) )
+               png_set_swap(png_ptr);
 
-       if (!(color_type & PNG_COLOR_MASK_COLOR))
-       {
-           png_set_gray_to_rgb(png_ptr);
-       }
+       if( !(color_type & PNG_COLOR_MASK_COLOR) )
+               png_set_gray_to_rgb(png_ptr);
 
-       if (color_type & PNG_COLOR_MASK_PALETTE)
-       {
-           png_set_palette_to_rgb(png_ptr);
-       }
+       if( color_type & PNG_COLOR_MASK_PALETTE )
+               png_set_palette_to_rgb(png_ptr);
 
-       if (color_depth <= 8)
-       {
-           png_set_expand(png_ptr);
-       }
+       if( color_depth <= 8 )
+               png_set_expand(png_ptr);
 
-/* read the image */
+// read the image
        png_read_image(png_ptr, output->get_rows());
+
 //printf("FilePNG::read_frame 3\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
-
        input->set_compressed_size(size);
-
 //printf("FilePNG::read_frame 4\n");
        return result;
 }
@@ -405,8 +367,8 @@ PNGUnit::~PNGUnit()
 
 PNGConfigVideo::PNGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
  : BC_Window(_(PROGRAM_NAME ": Video Compression"),
-       parent_window->get_abs_cursor_x(1),
-       parent_window->get_abs_cursor_y(1),
+       parent_window->get_abs_cursor_x(1),
+       parent_window->get_abs_cursor_y(1),
        200,
        100)
 {
@@ -420,7 +382,7 @@ PNGConfigVideo::~PNGConfigVideo()
 
 void PNGConfigVideo::create_objects()
 {
-       lock_window("PNGConfigVideo::create_objects()");
+       lock_window("PNGConfigVideo::create_objects");
        int x = 10, y = 10;
        add_subwindow(new PNGUseAlpha(this, x, y));
        add_subwindow(new BC_OKButton(this));