- /* 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);