[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53804] trunk/blender/source/blender/imbuf /intern/png.c: write 16 bit PNG files even when there is no float buffer,

Campbell Barton ideasman42 at gmail.com
Tue Jan 15 08:35:36 CET 2013


Revision: 53804
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53804
Author:   campbellbarton
Date:     2013-01-15 07:35:32 +0000 (Tue, 15 Jan 2013)
Log Message:
-----------
write 16 bit PNG files even when there is no float buffer,
the existence of a float buffer can depend on the image being used in the compositor for example, so better give the user what they expect - 16bit png if its selected from the UI.

Modified Paths:
--------------
    trunk/blender/source/blender/imbuf/intern/png.c

Modified: trunk/blender/source/blender/imbuf/intern/png.c
===================================================================
--- trunk/blender/source/blender/imbuf/intern/png.c	2013-01-15 04:33:08 UTC (rev 53803)
+++ trunk/blender/source/blender/imbuf/intern/png.c	2013-01-15 07:35:32 UTC (rev 53804)
@@ -60,6 +60,11 @@
 static void WriteData(png_structp png_ptr, png_bytep data, png_size_t length);
 static void Flush(png_structp png_ptr);
 
+BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val)
+{
+	return (_val << 8) + _val;
+}
+
 int imb_is_a_png(unsigned char *mem)
 {
 	int ret_val = 0;
@@ -115,7 +120,8 @@
 	int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
 	FILE *fp = NULL;
 
-	int is_16bit = (ibuf->ftype & PNG_16BIT) && ibuf->rect_float;
+	bool is_16bit  = (ibuf->ftype & PNG_16BIT);
+	bool has_float = (ibuf->rect_float != NULL);
 
 	/* use the jpeg quality setting for compression */
 	int compression;
@@ -174,14 +180,25 @@
 		case 4:
 			color_type = PNG_COLOR_TYPE_RGBA;
 			if (is_16bit) {
-				for (i = ibuf->x * ibuf->y; i > 0; i--) {
-					premul_to_straight_v4(from_straight, from_float);
-					to16[0] = FTOUSHORT(from_straight[0]);
-					to16[1] = FTOUSHORT(from_straight[1]);
-					to16[2] = FTOUSHORT(from_straight[2]);
-					to16[3] = FTOUSHORT(from_straight[3]);
-					to16 += 4; from_float += 4;
+				if (has_float) {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						premul_to_straight_v4(from_straight, from_float);
+						to16[0] = FTOUSHORT(from_straight[0]);
+						to16[1] = FTOUSHORT(from_straight[1]);
+						to16[2] = FTOUSHORT(from_straight[2]);
+						to16[3] = FTOUSHORT(from_straight[3]);
+						to16 += 4; from_float += 4;
+					}
 				}
+				else {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						to16[0] = UPSAMPLE_8_TO_16(from[0]);
+						to16[1] = UPSAMPLE_8_TO_16(from[1]);
+						to16[2] = UPSAMPLE_8_TO_16(from[2]);
+						to16[3] = UPSAMPLE_8_TO_16(from[3]);
+						to16 += 4; from += 4;
+					}
+				}
 			}
 			else {
 				for (i = ibuf->x * ibuf->y; i > 0; i--) {
@@ -196,13 +213,23 @@
 		case 3:
 			color_type = PNG_COLOR_TYPE_RGB;
 			if (is_16bit) {
-				for (i = ibuf->x * ibuf->y; i > 0; i--) {
-					premul_to_straight_v4(from_straight, from_float);
-					to16[0] = FTOUSHORT(from_straight[0]);
-					to16[1] = FTOUSHORT(from_straight[1]);
-					to16[2] = FTOUSHORT(from_straight[2]);
-					to16 += 3; from_float += 4;
+				if (has_float) {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						premul_to_straight_v4(from_straight, from_float);
+						to16[0] = FTOUSHORT(from_straight[0]);
+						to16[1] = FTOUSHORT(from_straight[1]);
+						to16[2] = FTOUSHORT(from_straight[2]);
+						to16 += 3; from_float += 4;
+					}
 				}
+				else {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						to16[0] = UPSAMPLE_8_TO_16(from[0]);
+						to16[1] = UPSAMPLE_8_TO_16(from[1]);
+						to16[2] = UPSAMPLE_8_TO_16(from[2]);
+						to16 += 3; from += 4;
+					}
+				}
 			}
 			else {
 				for (i = ibuf->x * ibuf->y; i > 0; i--) {
@@ -216,11 +243,19 @@
 		case 1:
 			color_type = PNG_COLOR_TYPE_GRAY;
 			if (is_16bit) {
-				for (i = ibuf->x * ibuf->y; i > 0; i--) {
-					premul_to_straight_v4(from_straight, from_float);
-					to16[0] = FTOUSHORT(from_straight[0]);
-					to16++; from_float += 4;
+				if (has_float) {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						premul_to_straight_v4(from_straight, from_float);
+						to16[0] = FTOUSHORT(from_straight[0]);
+						to16++; from_float += 4;
+					}
 				}
+				else {
+					for (i = ibuf->x * ibuf->y; i > 0; i--) {
+						to16[0] = UPSAMPLE_8_TO_16(from[0]);
+						to16++; from += 4;
+					}
+				}
 			}
 			else {
 				for (i = ibuf->x * ibuf->y; i > 0; i--) {




More information about the Bf-blender-cvs mailing list