diff options
Diffstat (limited to 'source/blender/imbuf')
-rw-r--r-- | source/blender/imbuf/IMB_imbuf_types.h | 3 | ||||
-rw-r--r-- | source/blender/imbuf/intern/allocimbuf.c | 1 | ||||
-rw-r--r-- | source/blender/imbuf/intern/png.c | 18 | ||||
-rw-r--r-- | source/blender/imbuf/intern/tiff.c | 39 |
4 files changed, 57 insertions, 4 deletions
diff --git a/source/blender/imbuf/IMB_imbuf_types.h b/source/blender/imbuf/IMB_imbuf_types.h index 52afe2ed854..5717a92db54 100644 --- a/source/blender/imbuf/IMB_imbuf_types.h +++ b/source/blender/imbuf/IMB_imbuf_types.h @@ -93,6 +93,9 @@ typedef struct ImBuf { 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; diff --git a/source/blender/imbuf/intern/allocimbuf.c b/source/blender/imbuf/intern/allocimbuf.c index efa37aa1196..91583f02a49 100644 --- a/source/blender/imbuf/intern/allocimbuf.c +++ b/source/blender/imbuf/intern/allocimbuf.c @@ -348,6 +348,7 @@ ImBuf *IMB_allocImBuf(unsigned int x, unsigned int y, uchar d, unsigned int flag 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) { diff --git a/source/blender/imbuf/intern/png.c b/source/blender/imbuf/intern/png.c index 200ff0af9af..6b6dcdb88af 100644 --- a/source/blender/imbuf/intern/png.c +++ b/source/blender/imbuf/intern/png.c @@ -257,6 +257,10 @@ int imb_savepng(struct ImBuf *ibuf, const char *name, int flags) } + 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 @@ struct ImBuf *imb_loadpng(unsigned char *mem, size_t size, int flags) 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"); } diff --git a/source/blender/imbuf/intern/tiff.c b/source/blender/imbuf/intern/tiff.c index 67d20d56466..36130aa0dbf 100644 --- a/source/blender/imbuf/intern/tiff.c +++ b/source/blender/imbuf/intern/tiff.c @@ -354,6 +354,25 @@ static void scanline_separate_32bit(float *rectf, float *fbuf, int scanline_w, i 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. @@ -369,10 +388,13 @@ static int imb_read_tiff_pixels(ImBuf *ibuf, TIFF *image, int premul) 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 @@ int imb_savetiff(ImBuf *ibuf, const char *name, int flags) 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 @@ int imb_savetiff(ImBuf *ibuf, const char *name, int flags) 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, |