Imported Upstream version 1.6.37 into tizen
[platform/upstream/libpng.git] / pngread.c
index 8fa7d9f..03f0300 100644 (file)
--- a/pngread.c
+++ b/pngread.c
@@ -755,6 +755,130 @@ png_read_image(png_structrp png_ptr, png_bytepp image)
       }
    }
 }
+
+#ifdef __TIZEN__
+void PNGAPI
+png_read_image_with_pick_color(png_structp png_ptr, png_bytepp image, PngPickColor *pickcolor)
+{
+   png_uint_32 i, image_height;
+   int pass, j;
+   png_uint_32 /*k = 0,*/ image_bpp = 0, image_width;
+   png_bytepp rp;
+   //png_bytep rc;
+   unsigned int npixels;
+       int perc_enable_y1, perc_enable_y2;
+   png_debug(1, "in png_read_image");
+
+   if (png_ptr == NULL || pickcolor == NULL)
+      return;
+
+#ifdef PNG_READ_INTERLACING_SUPPORTED
+   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
+   {
+      pass = png_set_interlace_handling(png_ptr);
+      /* And make sure transforms are initialized. */
+      png_start_read_image(png_ptr);
+   }
+   else
+   {
+      if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
+      {
+         /* Caller called png_start_read_image or png_read_update_info without
+          * first turning on the PNG_INTERLACE transform.  We can fix this here,
+          * but the caller should do it!
+          */
+         png_warning(png_ptr, "Interlace handling should be turned on when "
+            "using png_read_image");
+         /* Make sure this is set correctly */
+         png_ptr->num_rows = png_ptr->height;
+      }
+
+      /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
+       * the above error case.
+       */
+      pass = png_set_interlace_handling(png_ptr);
+   }
+#else
+   if (png_ptr->interlaced)
+      png_error(png_ptr,
+          "Cannot read interlaced image -- interlace handler disabled");
+
+   pass = 1;
+#endif
+
+   image_height=png_ptr->height;
+   image_bpp=png_ptr->rowbytes;
+   image_width=png_ptr->width;
+   png_ptr->user_chunk_ptr = pickcolor;
+
+   if(pickcolor->perc < 0)
+   {
+       png_error(png_ptr, "ColorPick percentage is negative");
+       return;
+   }
+   if( (pickcolor->region < PNG_COLORPICK_TOP) || (pickcolor->region > PNG_COLORPICK_BOTTOM))
+   {
+       png_error(png_ptr, "ColorPick Region is out of bound");
+       return;
+   }
+   if(pickcolor->region == PNG_COLORPICK_TOP)
+   {
+       perc_enable_y1 = 0;
+       perc_enable_y2 = (pickcolor->perc*image_height/100) - 1;
+   }
+   else if(pickcolor->region == PNG_COLORPICK_MIDDLE)
+   {
+       perc_enable_y1 = (image_height/2) - (((pickcolor->perc/2)*image_height)/100);
+       perc_enable_y2 = perc_enable_y1 + ((pickcolor->perc*image_height)/100) - 1;
+   }
+   else
+   {
+       perc_enable_y1 = (image_height) - ( pickcolor->perc * image_height / 100  );
+       perc_enable_y2 = image_height - 1;
+   }
+
+   for (j = 0; j < pass; j++)
+   {
+      rp = image;
+      for (i = 0; i < image_height; i++)
+      {
+         pickcolor->enable = 0;
+         if( pickcolor->perc > 0 )
+         {
+            if( (int)i >= perc_enable_y1 && (int)i <= perc_enable_y2)
+            {
+               pickcolor->enable = 1;
+            }
+         }
+         else
+         {
+            if( ((int)i >= pickcolor->y1) && ((int)i <= pickcolor->y2) )
+            {
+               pickcolor->enable = 1;
+            }
+         }
+         png_read_row(png_ptr, *rp, NULL);
+         rp++;
+      }
+   }
+
+
+   if(pickcolor->perc > 0)
+   {
+      npixels = (pickcolor->perc*image_height*image_width)/100;
+   }
+   else
+   {
+      npixels = (pickcolor->x2-pickcolor->x1+1)*(pickcolor->y2-pickcolor->y1+1);
+   }
+   if(npixels > 0)
+   {
+      pickcolor->sumR = pickcolor->sumR/npixels;
+      pickcolor->sumG = pickcolor->sumG/npixels;
+      pickcolor->sumB = pickcolor->sumB/npixels;
+   }
+}
+#endif /* __TIZEN__ */
 #endif /* SEQUENTIAL_READ */
 
 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
@@ -3765,13 +3889,13 @@ png_image_read_direct(png_voidp argument)
          mode = PNG_ALPHA_PNG;
          output_gamma = PNG_DEFAULT_sRGB;
       }
-      
+
       if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
       {
          mode = PNG_ALPHA_OPTIMIZED;
          change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
       }
-      
+
       /* If 'do_local_background' is set check for the presence of gamma
        * correction; this is part of the work-round for the libpng bug
        * described above.