[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [36669] trunk/blender/source/blender: support for reading/writing image resolution (dpi), for PNG and TIFF,

Campbell Barton ideasman42 at gmail.com
Fri May 13 16:27:13 CEST 2011


Revision: 36669
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=36669
Author:   campbellbarton
Date:     2011-05-13 14:27:12 +0000 (Fri, 13 May 2011)
Log Message:
-----------
support for reading/writing image resolution (dpi), for PNG and TIFF,
only RNA access currently 'image.resolution'.

Modified Paths:
--------------
    trunk/blender/source/blender/imbuf/IMB_imbuf_types.h
    trunk/blender/source/blender/imbuf/intern/allocimbuf.c
    trunk/blender/source/blender/imbuf/intern/png.c
    trunk/blender/source/blender/imbuf/intern/tiff.c
    trunk/blender/source/blender/makesrna/intern/rna_image.c

Modified: trunk/blender/source/blender/imbuf/IMB_imbuf_types.h
===================================================================
--- trunk/blender/source/blender/imbuf/IMB_imbuf_types.h	2011-05-13 13:17:30 UTC (rev 36668)
+++ trunk/blender/source/blender/imbuf/IMB_imbuf_types.h	2011-05-13 14:27:12 UTC (rev 36669)
@@ -93,6 +93,9 @@
 	char profile_filename[256];	/* to be implemented properly, specific filename for custom profiles */
 #endif
 
+	/* resolution - pixels per meter */
+	double ppm[2];
+
 	/* tiled pixel storage */
 	int tilex, tiley;
 	int xtiles, ytiles;

Modified: trunk/blender/source/blender/imbuf/intern/allocimbuf.c
===================================================================
--- trunk/blender/source/blender/imbuf/intern/allocimbuf.c	2011-05-13 13:17:30 UTC (rev 36668)
+++ trunk/blender/source/blender/imbuf/intern/allocimbuf.c	2011-05-13 14:27:12 UTC (rev 36669)
@@ -348,6 +348,7 @@
 		ibuf->depth= d;
 		ibuf->ftype= TGA;
 		ibuf->channels= 4;	/* float option, is set to other values when buffers get assigned */
+		ibuf->ppm[0]= ibuf->ppm[1]= 150.0 / 0.0254; /* 150dpi -> pixels-per-meter */
 		
 		if(flags & IB_rect) {
 			if(imb_addrectImBuf(ibuf)==FALSE) {

Modified: trunk/blender/source/blender/imbuf/intern/png.c
===================================================================
--- trunk/blender/source/blender/imbuf/intern/png.c	2011-05-13 13:17:30 UTC (rev 36668)
+++ trunk/blender/source/blender/imbuf/intern/png.c	2011-05-13 14:27:12 UTC (rev 36669)
@@ -257,6 +257,10 @@
 
 	}
 
+	if(ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
+		png_set_pHYs(png_ptr, info_ptr, (unsigned int)(ibuf->ppm[0] + 0.5), (unsigned int)(ibuf->ppm[1] + 0.5), PNG_RESOLUTION_METER);
+	}
+
 	// write the file header information
 	png_write_info(png_ptr, info_ptr);
 
@@ -384,7 +388,19 @@
 	if (ibuf) {
 		ibuf->ftype = PNG;
 		ibuf->profile = IB_PROFILE_SRGB;
-	} else {
+
+		if (png_get_valid (png_ptr, info_ptr, PNG_INFO_pHYs)) {
+			int unit_type;
+			unsigned int xres, yres;
+
+			if(png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type))
+			if(unit_type == PNG_RESOLUTION_METER) {
+				ibuf->ppm[0]= xres;
+				ibuf->ppm[1]= yres;
+			}
+		}
+	}
+	else {
 		printf("Couldn't allocate memory for PNG image\n");
 	}
 

Modified: trunk/blender/source/blender/imbuf/intern/tiff.c
===================================================================
--- trunk/blender/source/blender/imbuf/intern/tiff.c	2011-05-13 13:17:30 UTC (rev 36668)
+++ trunk/blender/source/blender/imbuf/intern/tiff.c	2011-05-13 14:27:12 UTC (rev 36669)
@@ -354,7 +354,26 @@
 		rectf[i*4 + chan] = fbuf[i];
 }
 
+static void imb_read_tiff_resolution(ImBuf *ibuf, TIFF *image)
+{
+	uint16 unit;
+	float xres;
+	float yres;
 
+	TIFFGetFieldDefaulted(image, TIFFTAG_RESOLUTIONUNIT, &unit);
+	TIFFGetFieldDefaulted(image, TIFFTAG_XRESOLUTION, &xres);
+	TIFFGetFieldDefaulted(image, TIFFTAG_YRESOLUTION, &yres);
+
+	if(unit == RESUNIT_CENTIMETER) {
+		ibuf->ppm[0]= (double)xres * 100.0;
+		ibuf->ppm[1]= (double)yres * 100.0;
+	}
+	else {
+		ibuf->ppm[0]= (double)xres / 0.0254;
+		ibuf->ppm[1]= (double)yres / 0.0254;
+	}
+}
+
 /* 
  * Use the libTIFF scanline API to read a TIFF image.
  * This method is most flexible and can handle multiple different bit depths 
@@ -369,10 +388,13 @@
 	int ib_flag=0, row, chan;
 	float *fbuf=NULL;
 	unsigned short *sbuf=NULL;
-	
+
 	TIFFGetField(image, TIFFTAG_BITSPERSAMPLE, &bitspersample);
 	TIFFGetField(image, TIFFTAG_SAMPLESPERPIXEL, &spp);		/* number of 'channels' */
 	TIFFGetField(image, TIFFTAG_PLANARCONFIG, &config);
+
+	imb_read_tiff_resolution(ibuf, image);
+
 	scanline = TIFFScanlineSize(image);
 	
 	if (bitspersample == 32) {
@@ -658,6 +680,7 @@
 	unsigned char *from = NULL, *to = NULL;
 	unsigned short *pixels16 = NULL, *to16 = NULL;
 	float *fromf = NULL;
+	float xres, yres;
 	int x, y, from_i, to_i, i;
 	int extraSampleTypes[1] = { EXTRASAMPLE_ASSOCALPHA };
 	
@@ -783,8 +806,18 @@
 	TIFFSetField(image, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
 	TIFFSetField(image, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
 	TIFFSetField(image, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
-	TIFFSetField(image, TIFFTAG_XRESOLUTION,     150.0);
-	TIFFSetField(image, TIFFTAG_YRESOLUTION,     150.0);
+
+
+	if(ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
+		xres= (float)(ibuf->ppm[0] * 0.0254);
+		yres= (float)(ibuf->ppm[1] * 0.0254);
+	}
+	else {
+		xres= yres= 150.0f;
+	}
+
+	TIFFSetField(image, TIFFTAG_XRESOLUTION,     xres);
+	TIFFSetField(image, TIFFTAG_YRESOLUTION,     yres);
 	TIFFSetField(image, TIFFTAG_RESOLUTIONUNIT,  RESUNIT_INCH);
 	if(TIFFWriteEncodedStrip(image, 0,
 			(bitspersample == 16)? (unsigned char*)pixels16: pixels,

Modified: trunk/blender/source/blender/makesrna/intern/rna_image.c
===================================================================
--- trunk/blender/source/blender/makesrna/intern/rna_image.c	2011-05-13 13:17:30 UTC (rev 36668)
+++ trunk/blender/source/blender/makesrna/intern/rna_image.c	2011-05-13 14:27:12 UTC (rev 36669)
@@ -221,7 +221,40 @@
 	BKE_image_release_ibuf(im, lock);
 }
 
+static void rna_Image_resolution_get(PointerRNA *ptr, float *values)
+{
+	Image *im= (Image*)ptr->data;
+	ImBuf *ibuf;
+	void *lock;
 
+	ibuf = BKE_image_acquire_ibuf(im, NULL , &lock);
+	if (ibuf) {
+		values[0]= ibuf->ppm[0];
+		values[1]= ibuf->ppm[1];
+	}
+	else {
+		values[0]= 0;
+		values[1]= 0;
+	}
+
+	BKE_image_release_ibuf(im, lock);
+}
+
+static void rna_Image_resolution_set(PointerRNA *ptr, const float *values)
+{
+	Image *im= (Image*)ptr->data;
+	ImBuf *ibuf;
+	void *lock;
+
+	ibuf = BKE_image_acquire_ibuf(im, NULL , &lock);
+	if (ibuf) {
+		ibuf->ppm[0]= values[0];
+		ibuf->ppm[1]= values[1];
+	}
+
+	BKE_image_release_ibuf(im, lock);
+}
+
 static int rna_Image_depth_get(PointerRNA *ptr)
 {
 	Image *im= (Image*)ptr->data;
@@ -557,6 +590,9 @@
 	RNA_def_property_int_funcs(prop, "rna_Image_size_get" , NULL, NULL);
 	RNA_def_property_clear_flag(prop, PROP_EDITABLE);
 
+	prop= RNA_def_float_vector(srna, "resolution" , 2 , NULL , 0, 0, "Resolution" , "X/Y pixels per meter" , 0 , 0);
+	RNA_def_property_float_funcs(prop, "rna_Image_resolution_get" , "rna_Image_resolution_set", NULL);
+
 	prop= RNA_def_property(srna, "pixels", PROP_FLOAT, PROP_NONE);
 	RNA_def_property_flag(prop, PROP_DYNAMIC);
 	RNA_def_property_multi_array(prop, 1, NULL);




More information about the Bf-blender-cvs mailing list