comparison tools/libgfx.c @ 1628:a549d33d543a

Add image aspect ratio information.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 16 May 2018 15:42:24 +0300
parents d0e626e039bf
children f28f36162740
comparison
equal deleted inserted replaced
1627:d0e626e039bf 1628:a549d33d543a
116 img->width = width; 116 img->width = width;
117 img->height = height; 117 img->height = height;
118 img->format = format; 118 img->format = format;
119 img->bpp = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp; 119 img->bpp = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp;
120 img->pitch = width * img->bpp; 120 img->pitch = width * img->bpp;
121 img->size = img->pitch * img->height; 121 img->size = img->pitch * img->height;
122 img->ctransp = -1; 122 img->ctransp = -1;
123 img->aspect = -1;
123 124
124 if ((img->data = dmMalloc(img->size)) == NULL) 125 if ((img->data = dmMalloc(img->size)) == NULL)
125 { 126 {
126 dmFree(img); 127 dmFree(img);
127 return NULL; 128 return NULL;
597 png_structp png_ptr = NULL; 598 png_structp png_ptr = NULL;
598 png_infop info_ptr = NULL; 599 png_infop info_ptr = NULL;
599 png_colorp palette = NULL; 600 png_colorp palette = NULL;
600 png_bytep *row_pointers = NULL; 601 png_bytep *row_pointers = NULL;
601 png_bytep trans = NULL; 602 png_bytep trans = NULL;
602 png_uint_32 width, height; 603 png_uint_32 width, height, res_x, res_y;
603 int i, bit_depth, color_type, ncolors, ntrans; 604 int i, bit_depth, color_type, ncolors, ntrans, unit_type;
604 int res = DMERR_OK; 605 int res = DMERR_OK;
605 DMImage *img; 606 DMImage *img;
606 607
607 // Create PNG structures 608 // Create PNG structures
608 png_ptr = png_create_read_struct( 609 png_ptr = png_create_read_struct(
684 { 685 {
685 res = dmError(DMERR_MALLOC, 686 res = dmError(DMERR_MALLOC,
686 "PNG: Could not allocate image data.\n"); 687 "PNG: Could not allocate image data.\n");
687 goto error; 688 goto error;
688 } 689 }
690
691 // Set image aspect ratio
692 png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type);
693
694 if (res_x > 0 && res_y > 0 &&
695 res_y / res_x != (unsigned) (img->height / img->width))
696 img->aspect = (float) res_y / (float) res_x;
689 697
690 // ... 698 // ...
691 row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep)); 699 row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep));
692 for (i = 0; i < img->height; i++) 700 for (i = 0; i < img->height; i++)
693 row_pointers[i] = img->data + (i * img->pitch); 701 row_pointers[i] = img->data + (i * img->pitch);
1252 res = dmError(DMERR_MALLOC, 1260 res = dmError(DMERR_MALLOC,
1253 "PCX: Could not allocate image structure.\n"); 1261 "PCX: Could not allocate image structure.\n");
1254 goto error; 1262 goto error;
1255 } 1263 }
1256 1264
1265 // Set image aspect ratio
1266 if (hdr.hScreenSize > 0 && hdr.vScreenSize > 0 &&
1267 hdr.vScreenSize / hdr.hScreenSize != img->height / img->width)
1268 img->aspect = (float) hdr.vScreenSize / (float) hdr.hScreenSize;
1269
1257 // Sanity check bytes per line value 1270 // Sanity check bytes per line value
1258 if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8) 1271 if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8)
1259 { 1272 {
1260 res = dmError(DMERR_MALLOC, 1273 res = dmError(DMERR_MALLOC,
1261 "PCX: The bytes per plane line value %d is smaller than width*bpp/8 = %d!\n", 1274 "PCX: The bytes per plane line value %d is smaller than width*bpp/8 = %d!\n",
1858 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1 1871 //iff->bmhd.nplanes <= 8 ? iff->bmhd.nplanes : -1
1859 -1 1872 -1
1860 )) == NULL) 1873 )) == NULL)
1861 return DMERR_MALLOC; 1874 return DMERR_MALLOC;
1862 1875
1876 // Set image aspect ratio
1877 if (iff.bmhd.xasp > 0 && iff.bmhd.yasp > 0)
1878 (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp;
1863 1879
1864 // Decode the body 1880 // Decode the body
1865 if (iff.planar) 1881 if (iff.planar)
1866 { 1882 {
1867 if ((res = dmDecodeILBMBody(fp, &iff, *pimg, &read)) != DMERR_OK) 1883 if ((res = dmDecodeILBMBody(fp, &iff, *pimg, &read)) != DMERR_OK)