CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/libpng17/pngget.c
Views: 1401
1/* pngget.c - retrieval of values from info struct2*3* Last changed in libpng 1.7.0 [(PENDING RELEASE)]4* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson5* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)6* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)7*8* This code is released under the libpng license.9* For conditions of distribution and use, see the disclaimer10* and license in png.h11*12*/1314#include "pngpriv.h"15#define PNG_SRC_FILE PNG_SRC_FILE_pngget1617#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)1819png_uint_32 PNGAPI20png_get_valid(png_const_structrp png_ptr, png_const_inforp info_ptr,21png_uint_32 flag)22{23if (png_ptr != NULL && info_ptr != NULL)24return(info_ptr->valid & flag);2526return(0);27}2829png_alloc_size_t PNGAPI30png_get_rowbytes(png_const_structrp png_ptr, png_const_inforp info_ptr)31{32if (png_ptr != NULL && info_ptr != NULL)33return png_calc_rowbytes(png_ptr,34PNG_FORMAT_CHANNELS(info_ptr->format) * info_ptr->bit_depth,35info_ptr->width);3637return 0;38}3940#ifdef PNG_READ_PNG_SUPPORTED41png_bytepp PNGAPI42png_get_rows(png_const_structrp png_ptr, png_const_inforp info_ptr)43{44if (png_ptr != NULL && info_ptr != NULL)45return(info_ptr->row_pointers);4647return(0);48}49#endif5051#ifdef PNG_EASY_ACCESS_SUPPORTED52/* Easy access to info, added in libpng-0.99 */53png_uint_32 PNGAPI54png_get_image_width(png_const_structrp png_ptr, png_const_inforp info_ptr)55{56if (png_ptr != NULL && info_ptr != NULL)57return info_ptr->width;5859return (0);60}6162png_uint_32 PNGAPI63png_get_image_height(png_const_structrp png_ptr, png_const_inforp info_ptr)64{65if (png_ptr != NULL && info_ptr != NULL)66return info_ptr->height;6768return (0);69}7071png_byte PNGAPI72png_get_bit_depth(png_const_structrp png_ptr, png_const_inforp info_ptr)73{74if (png_ptr != NULL && info_ptr != NULL)75return info_ptr->bit_depth;7677return (0);78}7980png_byte PNGAPI81png_get_color_type(png_const_structrp png_ptr, png_const_inforp info_ptr)82{83if (png_ptr != NULL && info_ptr != NULL)84return png_check_byte(png_ptr,85PNG_COLOR_TYPE_FROM_FORMAT(info_ptr->format));8687return (0);88}8990png_byte PNGAPI91png_get_filter_type(png_const_structrp png_ptr, png_const_inforp info_ptr)92{93if (png_ptr != NULL && info_ptr != NULL)94return info_ptr->filter_type;9596return (0);97}9899png_byte PNGAPI100png_get_interlace_type(png_const_structrp png_ptr, png_const_inforp info_ptr)101{102if (png_ptr != NULL && info_ptr != NULL)103return info_ptr->interlace_type;104105return (0);106}107108png_byte PNGAPI109png_get_compression_type(png_const_structrp png_ptr, png_const_inforp info_ptr)110{111if (png_ptr != NULL && info_ptr != NULL)112return info_ptr->compression_type;113114return (0);115}116117png_uint_32 PNGAPI118png_get_x_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp119info_ptr)120{121#ifdef PNG_pHYs_SUPPORTED122if (png_ptr != NULL && info_ptr != NULL &&123(info_ptr->valid & PNG_INFO_pHYs) != 0)124{125png_debug1(1, "in %s retrieval function",126"png_get_x_pixels_per_meter");127128if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)129return (info_ptr->x_pixels_per_unit);130}131#else132PNG_UNUSED(png_ptr)133PNG_UNUSED(info_ptr)134#endif135136return (0);137}138139png_uint_32 PNGAPI140png_get_y_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp141info_ptr)142{143#ifdef PNG_pHYs_SUPPORTED144if (png_ptr != NULL && info_ptr != NULL &&145(info_ptr->valid & PNG_INFO_pHYs) != 0)146{147png_debug1(1, "in %s retrieval function",148"png_get_y_pixels_per_meter");149150if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER)151return (info_ptr->y_pixels_per_unit);152}153#else154PNG_UNUSED(png_ptr)155PNG_UNUSED(info_ptr)156#endif157158return (0);159}160161png_uint_32 PNGAPI162png_get_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp info_ptr)163{164#ifdef PNG_pHYs_SUPPORTED165if (png_ptr != NULL && info_ptr != NULL &&166(info_ptr->valid & PNG_INFO_pHYs) != 0)167{168png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter");169170if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER &&171info_ptr->x_pixels_per_unit == info_ptr->y_pixels_per_unit)172return (info_ptr->x_pixels_per_unit);173}174#else175PNG_UNUSED(png_ptr)176PNG_UNUSED(info_ptr)177#endif178179return (0);180}181182#ifdef PNG_FLOATING_POINT_SUPPORTED183float PNGAPI184png_get_pixel_aspect_ratio(png_const_structrp png_ptr, png_const_inforp185info_ptr)186{187#ifdef PNG_READ_pHYs_SUPPORTED188if (png_ptr != NULL && info_ptr != NULL &&189(info_ptr->valid & PNG_INFO_pHYs) != 0)190{191png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio");192193if (info_ptr->x_pixels_per_unit != 0)194return ((float)((float)info_ptr->y_pixels_per_unit195/(float)info_ptr->x_pixels_per_unit));196}197#else198PNG_UNUSED(png_ptr)199PNG_UNUSED(info_ptr)200#endif201202return ((float)0.0);203}204#endif205206#ifdef PNG_FIXED_POINT_SUPPORTED207png_fixed_point PNGAPI208png_get_pixel_aspect_ratio_fixed(png_const_structrp png_ptr,209png_const_inforp info_ptr)210{211#ifdef PNG_READ_pHYs_SUPPORTED212if (png_ptr != NULL && info_ptr != NULL &&213(info_ptr->valid & PNG_INFO_pHYs) != 0 &&214info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0 &&215info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX &&216info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX)217{218png_fixed_point res;219220png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio_fixed");221222/* The following casts work because a PNG 4 byte integer only has a valid223* range of 0..2^31-1; otherwise the cast might overflow.224*/225if (png_muldiv(&res, (png_int_32)info_ptr->y_pixels_per_unit, PNG_FP_1,226(png_int_32)info_ptr->x_pixels_per_unit) != 0)227return res;228}229#else230PNG_UNUSED(png_ptr)231PNG_UNUSED(info_ptr)232#endif233234return 0;235}236#endif237238png_int_32 PNGAPI239png_get_x_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr)240{241#ifdef PNG_oFFs_SUPPORTED242if (png_ptr != NULL && info_ptr != NULL &&243(info_ptr->valid & PNG_INFO_oFFs) != 0)244{245png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns");246247if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)248return (info_ptr->x_offset);249}250#else251PNG_UNUSED(png_ptr)252PNG_UNUSED(info_ptr)253#endif254255return (0);256}257258png_int_32 PNGAPI259png_get_y_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr)260{261#ifdef PNG_oFFs_SUPPORTED262if (png_ptr != NULL && info_ptr != NULL &&263(info_ptr->valid & PNG_INFO_oFFs) != 0)264{265png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns");266267if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER)268return (info_ptr->y_offset);269}270#else271PNG_UNUSED(png_ptr)272PNG_UNUSED(info_ptr)273#endif274275return (0);276}277278png_int_32 PNGAPI279png_get_x_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr)280{281#ifdef PNG_oFFs_SUPPORTED282if (png_ptr != NULL && info_ptr != NULL &&283(info_ptr->valid & PNG_INFO_oFFs) != 0)284{285png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels");286287if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)288return (info_ptr->x_offset);289}290#else291PNG_UNUSED(png_ptr)292PNG_UNUSED(info_ptr)293#endif294295return (0);296}297298png_int_32 PNGAPI299png_get_y_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr)300{301#ifdef PNG_oFFs_SUPPORTED302if (png_ptr != NULL && info_ptr != NULL &&303(info_ptr->valid & PNG_INFO_oFFs) != 0)304{305png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels");306307if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL)308return (info_ptr->y_offset);309}310#else311PNG_UNUSED(png_ptr)312PNG_UNUSED(info_ptr)313#endif314315return (0);316}317318#ifdef PNG_INCH_CONVERSIONS_SUPPORTED319static png_uint_32320ppi_from_ppm(png_uint_32 ppm)321{322#if 0 /*NOT USED*/323/* The conversion is *(2.54/100), in binary (32 digits):324* .00000110100000001001110101001001325*/326png_uint_32 t1001, t1101;327ppm >>= 1; /* .1 */328t1001 = ppm + (ppm >> 3); /* .1001 */329t1101 = t1001 + (ppm >> 1); /* .1101 */330ppm >>= 20; /* .000000000000000000001 */331t1101 += t1101 >> 15; /* .1101000000000001101 */332t1001 >>= 11; /* .000000000001001 */333t1001 += t1001 >> 12; /* .000000000001001000000001001 */334ppm += t1001; /* .000000000001001000001001001 */335ppm += t1101; /* .110100000001001110101001001 */336return (ppm + 16) >> 5;/* .00000110100000001001110101001001 */337#else338/* The argument is a PNG unsigned integer, so it is not permitted339* to be bigger than 2^31.340*/341png_fixed_point result;342if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127,3435000) != 0)344return result;345346/* Overflow. */347return 0;348#endif349}350351png_uint_32 PNGAPI352png_get_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)353{354return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr));355}356357png_uint_32 PNGAPI358png_get_x_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)359{360return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr));361}362363png_uint_32 PNGAPI364png_get_y_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr)365{366return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr));367}368369#ifdef PNG_FIXED_POINT_SUPPORTED370static png_fixed_point371png_fixed_inches_from_microns(png_const_structrp png_ptr,372png_int_32 microns)373{374/* Convert from metres * 1,000,000 to inches * 100,000, meters to375* inches is simply *(100/2.54), so we want *(10/2.54) == 500/127.376* Notice that this can overflow - a warning is output and 0 is377* returned.378*/379png_fixed_point result;380381if (png_muldiv(&result, microns, 500, 127) != 0)382return result;383384/* TODO: review this, png_error might be better. */385png_warning(png_ptr, "inch to microns overflow");386return 0;387# ifndef PNG_WARNINGS_SUPPORTED388PNG_UNUSED(png_ptr)389# endif390}391392png_fixed_point PNGAPI393png_get_x_offset_inches_fixed(png_const_structrp png_ptr,394png_const_inforp info_ptr)395{396return png_fixed_inches_from_microns(png_ptr,397png_get_x_offset_microns(png_ptr, info_ptr));398}399400png_fixed_point PNGAPI401png_get_y_offset_inches_fixed(png_const_structrp png_ptr,402png_const_inforp info_ptr)403{404return png_fixed_inches_from_microns(png_ptr,405png_get_y_offset_microns(png_ptr, info_ptr));406}407#endif /* FIXED_POINT */408409#ifdef PNG_FLOATING_POINT_SUPPORTED410float PNGAPI411png_get_x_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr)412{413/* To avoid the overflow do the conversion directly in floating414* point.415*/416return (float)(png_get_x_offset_microns(png_ptr, info_ptr) * .00003937);417}418#endif419420#ifdef PNG_FLOATING_POINT_SUPPORTED421float PNGAPI422png_get_y_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr)423{424/* To avoid the overflow do the conversion directly in floating425* point.426*/427return (float)(png_get_y_offset_microns(png_ptr, info_ptr) * .00003937);428}429#endif430431#ifdef PNG_pHYs_SUPPORTED432png_uint_32 PNGAPI433png_get_pHYs_dpi(png_const_structrp png_ptr, png_const_inforp info_ptr,434png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)435{436png_uint_32 retval = 0;437438if (png_ptr != NULL && info_ptr != NULL &&439(info_ptr->valid & PNG_INFO_pHYs) != 0)440{441png_debug1(1, "in %s retrieval function", "pHYs");442443if (res_x != NULL)444{445*res_x = info_ptr->x_pixels_per_unit;446retval |= PNG_INFO_pHYs;447}448449if (res_y != NULL)450{451*res_y = info_ptr->y_pixels_per_unit;452retval |= PNG_INFO_pHYs;453}454455if (unit_type != NULL)456{457*unit_type = (int)info_ptr->phys_unit_type;458retval |= PNG_INFO_pHYs;459460if (*unit_type == 1)461{462if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50);463if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50);464}465}466}467468return (retval);469}470#endif /* pHYs */471#endif /* INCH_CONVERSIONS */472473/* png_get_channels really belongs in here, too, but it's been around longer */474475#endif /* EASY_ACCESS */476477478png_byte PNGAPI479png_get_channels(png_const_structrp png_ptr, png_const_inforp info_ptr)480{481if (png_ptr != NULL && info_ptr != NULL)482return png_check_byte(png_ptr, PNG_FORMAT_CHANNELS(info_ptr->format));483484return (0);485}486487#ifdef PNG_READ_SUPPORTED488png_const_bytep PNGAPI489png_get_signature(png_const_structrp png_ptr, png_const_inforp info_ptr)490{491if (png_ptr != NULL && info_ptr != NULL)492return(info_ptr->signature);493494return (NULL);495}496#endif497498#ifdef PNG_bKGD_SUPPORTED499png_uint_32 PNGAPI500png_get_bKGD(png_const_structrp png_ptr, png_inforp info_ptr,501png_color_16p *background)502{503if (png_ptr != NULL && info_ptr != NULL &&504(info_ptr->valid & PNG_INFO_bKGD) != 0 &&505background != NULL)506{507png_debug1(1, "in %s retrieval function", "bKGD");508509*background = &(info_ptr->background);510return (PNG_INFO_bKGD);511}512513return (0);514}515#endif516517#ifdef PNG_cHRM_SUPPORTED518/* The XYZ APIs were added in 1.5.5 to take advantage of the code added at the519* same time to correct the rgb grayscale coefficient defaults obtained from the520* cHRM chunk in 1.5.4521*/522# ifdef PNG_FLOATING_POINT_SUPPORTED523png_uint_32 PNGAPI524png_get_cHRM(png_const_structrp png_ptr, png_const_inforp info_ptr,525double *white_x, double *white_y, double *red_x, double *red_y,526double *green_x, double *green_y, double *blue_x, double *blue_y)527{528/* Quiet API change: this code used to only return the end points if a cHRM529* chunk was present, but the end points can also come from iCCP or sRGB530* chunks, so in 1.6.0 the png_get_ APIs return the end points regardless and531* the png_set_ APIs merely check that set end points are mutually532* consistent.533*/534if (png_ptr != NULL && info_ptr != NULL &&535(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)536{537png_debug1(1, "in %s retrieval function", "cHRM");538539if (white_x != NULL)540*white_x = png_float(png_ptr,541info_ptr->colorspace.end_points_xy.whitex, "cHRM white X");542if (white_y != NULL)543*white_y = png_float(png_ptr,544info_ptr->colorspace.end_points_xy.whitey, "cHRM white Y");545if (red_x != NULL)546*red_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redx,547"cHRM red X");548if (red_y != NULL)549*red_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redy,550"cHRM red Y");551if (green_x != NULL)552*green_x = png_float(png_ptr,553info_ptr->colorspace.end_points_xy.greenx, "cHRM green X");554if (green_y != NULL)555*green_y = png_float(png_ptr,556info_ptr->colorspace.end_points_xy.greeny, "cHRM green Y");557if (blue_x != NULL)558*blue_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluex,559"cHRM blue X");560if (blue_y != NULL)561*blue_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluey,562"cHRM blue Y");563return (PNG_INFO_cHRM);564}565566return (0);567}568569png_uint_32 PNGAPI570png_get_cHRM_XYZ(png_const_structrp png_ptr, png_const_inforp info_ptr,571double *red_X, double *red_Y, double *red_Z, double *green_X,572double *green_Y, double *green_Z, double *blue_X, double *blue_Y,573double *blue_Z)574{575if (png_ptr != NULL && info_ptr != NULL &&576(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)577{578png_debug1(1, "in %s retrieval function", "cHRM_XYZ(float)");579580if (red_X != NULL)581*red_X = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_X,582"cHRM red X");583if (red_Y != NULL)584*red_Y = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Y,585"cHRM red Y");586if (red_Z != NULL)587*red_Z = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Z,588"cHRM red Z");589if (green_X != NULL)590*green_X = png_float(png_ptr,591info_ptr->colorspace.end_points_XYZ.green_X, "cHRM green X");592if (green_Y != NULL)593*green_Y = png_float(png_ptr,594info_ptr->colorspace.end_points_XYZ.green_Y, "cHRM green Y");595if (green_Z != NULL)596*green_Z = png_float(png_ptr,597info_ptr->colorspace.end_points_XYZ.green_Z, "cHRM green Z");598if (blue_X != NULL)599*blue_X = png_float(png_ptr,600info_ptr->colorspace.end_points_XYZ.blue_X, "cHRM blue X");601if (blue_Y != NULL)602*blue_Y = png_float(png_ptr,603info_ptr->colorspace.end_points_XYZ.blue_Y, "cHRM blue Y");604if (blue_Z != NULL)605*blue_Z = png_float(png_ptr,606info_ptr->colorspace.end_points_XYZ.blue_Z, "cHRM blue Z");607return (PNG_INFO_cHRM);608}609610return (0);611}612# endif613614# ifdef PNG_FIXED_POINT_SUPPORTED615png_uint_32 PNGAPI616png_get_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,617png_fixed_point *int_red_X, png_fixed_point *int_red_Y,618png_fixed_point *int_red_Z, png_fixed_point *int_green_X,619png_fixed_point *int_green_Y, png_fixed_point *int_green_Z,620png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y,621png_fixed_point *int_blue_Z)622{623if (png_ptr != NULL && info_ptr != NULL &&624(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)625{626png_debug1(1, "in %s retrieval function", "cHRM_XYZ");627628if (int_red_X != NULL)629*int_red_X = info_ptr->colorspace.end_points_XYZ.red_X;630if (int_red_Y != NULL)631*int_red_Y = info_ptr->colorspace.end_points_XYZ.red_Y;632if (int_red_Z != NULL)633*int_red_Z = info_ptr->colorspace.end_points_XYZ.red_Z;634if (int_green_X != NULL)635*int_green_X = info_ptr->colorspace.end_points_XYZ.green_X;636if (int_green_Y != NULL)637*int_green_Y = info_ptr->colorspace.end_points_XYZ.green_Y;638if (int_green_Z != NULL)639*int_green_Z = info_ptr->colorspace.end_points_XYZ.green_Z;640if (int_blue_X != NULL)641*int_blue_X = info_ptr->colorspace.end_points_XYZ.blue_X;642if (int_blue_Y != NULL)643*int_blue_Y = info_ptr->colorspace.end_points_XYZ.blue_Y;644if (int_blue_Z != NULL)645*int_blue_Z = info_ptr->colorspace.end_points_XYZ.blue_Z;646return (PNG_INFO_cHRM);647}648649return (0);650}651652png_uint_32 PNGAPI653png_get_cHRM_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,654png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x,655png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y,656png_fixed_point *blue_x, png_fixed_point *blue_y)657{658png_debug1(1, "in %s retrieval function", "cHRM");659660if (png_ptr != NULL && info_ptr != NULL &&661(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)662{663if (white_x != NULL)664*white_x = info_ptr->colorspace.end_points_xy.whitex;665if (white_y != NULL)666*white_y = info_ptr->colorspace.end_points_xy.whitey;667if (red_x != NULL)668*red_x = info_ptr->colorspace.end_points_xy.redx;669if (red_y != NULL)670*red_y = info_ptr->colorspace.end_points_xy.redy;671if (green_x != NULL)672*green_x = info_ptr->colorspace.end_points_xy.greenx;673if (green_y != NULL)674*green_y = info_ptr->colorspace.end_points_xy.greeny;675if (blue_x != NULL)676*blue_x = info_ptr->colorspace.end_points_xy.bluex;677if (blue_y != NULL)678*blue_y = info_ptr->colorspace.end_points_xy.bluey;679return (PNG_INFO_cHRM);680}681682return (0);683}684# endif685#endif686687#ifdef PNG_gAMA_SUPPORTED688# ifdef PNG_FIXED_POINT_SUPPORTED689png_uint_32 PNGAPI690png_get_gAMA_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,691png_fixed_point *file_gamma)692{693png_debug1(1, "in %s retrieval function", "gAMA");694695if (png_ptr != NULL && info_ptr != NULL &&696(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&697file_gamma != NULL)698{699*file_gamma = info_ptr->colorspace.gamma;700return (PNG_INFO_gAMA);701}702703return (0);704}705# endif706707# ifdef PNG_FLOATING_POINT_SUPPORTED708png_uint_32 PNGAPI709png_get_gAMA(png_const_structrp png_ptr, png_const_inforp info_ptr,710double *file_gamma)711{712png_debug1(1, "in %s retrieval function", "gAMA(float)");713714if (png_ptr != NULL && info_ptr != NULL &&715(info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&716file_gamma != NULL)717{718*file_gamma = png_float(png_ptr, info_ptr->colorspace.gamma,719"png_get_gAMA");720return (PNG_INFO_gAMA);721}722723return (0);724}725# endif726#endif727728#ifdef PNG_sRGB_SUPPORTED729png_uint_32 PNGAPI730png_get_sRGB(png_const_structrp png_ptr, png_const_inforp info_ptr,731int *file_srgb_intent)732{733png_debug1(1, "in %s retrieval function", "sRGB");734735if (png_ptr != NULL && info_ptr != NULL &&736(info_ptr->valid & PNG_INFO_sRGB) != 0 && file_srgb_intent != NULL)737{738*file_srgb_intent = info_ptr->colorspace.rendering_intent;739return (PNG_INFO_sRGB);740}741742return (0);743}744#endif745746#ifdef PNG_iCCP_SUPPORTED747png_uint_32 PNGAPI748png_get_iCCP(png_const_structrp png_ptr, png_inforp info_ptr,749png_charpp name, int *compression_type,750png_bytepp profile, png_uint_32 *proflen)751{752png_debug1(1, "in %s retrieval function", "iCCP");753754if (png_ptr != NULL && info_ptr != NULL &&755(info_ptr->valid & PNG_INFO_iCCP) != 0 &&756name != NULL && compression_type != NULL && profile != NULL &&757proflen != NULL)758{759*name = info_ptr->iccp_name;760*profile = info_ptr->iccp_profile;761*proflen = png_get_uint_32(info_ptr->iccp_profile);762/* This is somewhat irrelevant since the profile data returned has763* actually been uncompressed.764*/765*compression_type = PNG_COMPRESSION_TYPE_BASE;766return (PNG_INFO_iCCP);767}768769return (0);770}771#endif772773#ifdef PNG_sPLT_SUPPORTED774int PNGAPI775png_get_sPLT(png_const_structrp png_ptr, png_inforp info_ptr,776png_sPLT_tpp spalettes)777{778if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL)779{780*spalettes = info_ptr->splt_palettes;781return info_ptr->splt_palettes_num;782}783784return (0);785}786#endif787788#ifdef PNG_hIST_SUPPORTED789png_uint_32 PNGAPI790png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr,791png_uint_16p *hist)792{793png_debug1(1, "in %s retrieval function", "hIST");794795if (png_ptr != NULL && info_ptr != NULL &&796(info_ptr->valid & PNG_INFO_hIST) != 0 && hist != NULL)797{798*hist = info_ptr->hist;799return (PNG_INFO_hIST);800}801802return (0);803}804#endif805806png_uint_32 PNGAPI807png_get_IHDR(png_const_structrp png_ptr, png_const_inforp info_ptr,808png_uint_32 *width, png_uint_32 *height, int *bit_depth,809int *color_type, int *interlace_type, int *compression_type,810int *filter_type)811{812png_debug1(1, "in %s retrieval function", "IHDR");813814if (png_ptr == NULL || info_ptr == NULL)815return (0);816817if (width != NULL)818*width = info_ptr->width;819820if (height != NULL)821*height = info_ptr->height;822823if (bit_depth != NULL)824*bit_depth = info_ptr->bit_depth;825826if (color_type != NULL)827*color_type = PNG_COLOR_TYPE_FROM_FORMAT(info_ptr->format);828829if (compression_type != NULL)830*compression_type = info_ptr->compression_type;831832if (filter_type != NULL)833*filter_type = info_ptr->filter_type;834835if (interlace_type != NULL)836*interlace_type = info_ptr->interlace_type;837838return 1;839}840841#ifdef PNG_oFFs_SUPPORTED842png_uint_32 PNGAPI843png_get_oFFs(png_const_structrp png_ptr, png_const_inforp info_ptr,844png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type)845{846png_debug1(1, "in %s retrieval function", "oFFs");847848if (png_ptr != NULL && info_ptr != NULL &&849(info_ptr->valid & PNG_INFO_oFFs) != 0 &&850offset_x != NULL && offset_y != NULL && unit_type != NULL)851{852*offset_x = info_ptr->x_offset;853*offset_y = info_ptr->y_offset;854*unit_type = (int)info_ptr->offset_unit_type;855return (PNG_INFO_oFFs);856}857858return (0);859}860#endif861862#ifdef PNG_pCAL_SUPPORTED863png_uint_32 PNGAPI864png_get_pCAL(png_const_structrp png_ptr, png_inforp info_ptr,865png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams,866png_charp *units, png_charpp *params)867{868png_debug1(1, "in %s retrieval function", "pCAL");869870if (png_ptr != NULL && info_ptr != NULL &&871(info_ptr->valid & PNG_INFO_pCAL) != 0 &&872purpose != NULL && X0 != NULL && X1 != NULL && type != NULL &&873nparams != NULL && units != NULL && params != NULL)874{875*purpose = info_ptr->pcal_purpose;876*X0 = info_ptr->pcal_X0;877*X1 = info_ptr->pcal_X1;878*type = (int)info_ptr->pcal_type;879*nparams = (int)info_ptr->pcal_nparams;880*units = info_ptr->pcal_units;881*params = info_ptr->pcal_params;882return (PNG_INFO_pCAL);883}884885return (0);886}887#endif888889#ifdef PNG_sCAL_SUPPORTED890# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED891# ifdef PNG_FIXED_POINT_SUPPORTED892png_uint_32 PNGAPI893png_get_sCAL_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,894int *unit, png_fixed_point *width, png_fixed_point *height)895{896if (png_ptr != NULL && info_ptr != NULL &&897(info_ptr->valid & PNG_INFO_sCAL) != 0)898{899*unit = info_ptr->scal_unit;900/*TODO: make this work without FP support; the API is currently eliminated901* if neither floating point APIs nor internal floating point arithmetic902* are enabled.903*/904*width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width");905*height = png_fixed(png_ptr, atof(info_ptr->scal_s_height),906"sCAL height");907return (PNG_INFO_sCAL);908}909910return(0);911}912# endif /* FIXED_POINT */913914# ifdef PNG_FLOATING_POINT_SUPPORTED915png_uint_32 PNGAPI916png_get_sCAL(png_const_structrp png_ptr, png_const_inforp info_ptr,917int *unit, double *width, double *height)918{919if (png_ptr != NULL && info_ptr != NULL &&920(info_ptr->valid & PNG_INFO_sCAL) != 0)921{922*unit = info_ptr->scal_unit;923*width = atof(info_ptr->scal_s_width);924*height = atof(info_ptr->scal_s_height);925return (PNG_INFO_sCAL);926}927928return(0);929}930# endif /* FLOATING POINT */931# endif /* FLOATING_ARITHMETIC */932933png_uint_32 PNGAPI934png_get_sCAL_s(png_const_structrp png_ptr, png_const_inforp info_ptr,935int *unit, png_charpp width, png_charpp height)936{937if (png_ptr != NULL && info_ptr != NULL &&938(info_ptr->valid & PNG_INFO_sCAL) != 0)939{940*unit = info_ptr->scal_unit;941*width = info_ptr->scal_s_width;942*height = info_ptr->scal_s_height;943return (PNG_INFO_sCAL);944}945946return(0);947}948#endif /* sCAL */949950#ifdef PNG_pHYs_SUPPORTED951png_uint_32 PNGAPI952png_get_pHYs(png_const_structrp png_ptr, png_const_inforp info_ptr,953png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type)954{955png_uint_32 retval = 0;956957png_debug1(1, "in %s retrieval function", "pHYs");958959if (png_ptr != NULL && info_ptr != NULL &&960(info_ptr->valid & PNG_INFO_pHYs) != 0)961{962if (res_x != NULL)963{964*res_x = info_ptr->x_pixels_per_unit;965retval |= PNG_INFO_pHYs;966}967968if (res_y != NULL)969{970*res_y = info_ptr->y_pixels_per_unit;971retval |= PNG_INFO_pHYs;972}973974if (unit_type != NULL)975{976*unit_type = (int)info_ptr->phys_unit_type;977retval |= PNG_INFO_pHYs;978}979}980981return (retval);982}983#endif /* pHYs */984985png_uint_32 PNGAPI986png_get_PLTE(png_const_structrp png_ptr, png_inforp info_ptr,987png_colorp *palette, int *num_palette)988{989png_debug1(1, "in %s retrieval function", "PLTE");990991if (png_ptr != NULL && info_ptr != NULL &&992(info_ptr->valid & PNG_INFO_PLTE) != 0 && palette != NULL)993{994*palette = info_ptr->palette;995*num_palette = info_ptr->num_palette;996png_debug1(3, "num_palette = %d", *num_palette);997return (PNG_INFO_PLTE);998}9991000return (0);1001}10021003#ifdef PNG_sBIT_SUPPORTED1004png_uint_32 PNGAPI1005png_get_sBIT(png_const_structrp png_ptr, png_inforp info_ptr,1006png_color_8p *sig_bit)1007{1008png_debug1(1, "in %s retrieval function", "sBIT");10091010if (png_ptr != NULL && info_ptr != NULL &&1011(info_ptr->valid & PNG_INFO_sBIT) != 0 && sig_bit != NULL)1012{1013*sig_bit = &(info_ptr->sig_bit);1014return (PNG_INFO_sBIT);1015}10161017return (0);1018}1019#endif10201021#ifdef PNG_TEXT_SUPPORTED1022int PNGAPI1023png_get_text(png_const_structrp png_ptr, png_inforp info_ptr,1024png_textp *text_ptr, int *num_text)1025{1026if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0)1027{1028png_debug1(1, "in 0x%lx retrieval function",1029(unsigned long)png_ptr->chunk_name);10301031if (text_ptr != NULL)1032*text_ptr = info_ptr->text;10331034if (num_text != NULL)1035*num_text = info_ptr->num_text;10361037return info_ptr->num_text;1038}10391040if (num_text != NULL)1041*num_text = 0;10421043return(0);1044}1045#endif10461047#ifdef PNG_tIME_SUPPORTED1048png_uint_32 PNGAPI1049png_get_tIME(png_const_structrp png_ptr, png_inforp info_ptr,1050png_timep *mod_time)1051{1052png_debug1(1, "in %s retrieval function", "tIME");10531054if (png_ptr != NULL && info_ptr != NULL &&1055(info_ptr->valid & PNG_INFO_tIME) != 0 && mod_time != NULL)1056{1057*mod_time = &(info_ptr->mod_time);1058return (PNG_INFO_tIME);1059}10601061return (0);1062}1063#endif10641065#ifdef PNG_tRNS_SUPPORTED1066png_uint_32 PNGAPI1067png_get_tRNS(png_const_structrp png_ptr, png_inforp info_ptr,1068png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color)1069{1070png_uint_32 retval = 0;1071if (png_ptr != NULL && info_ptr != NULL &&1072(info_ptr->valid & PNG_INFO_tRNS) != 0)1073{1074png_debug1(1, "in %s retrieval function", "tRNS");10751076if ((info_ptr->format & PNG_FORMAT_FLAG_COLORMAP) != 0)1077{1078if (trans_alpha != NULL)1079{1080*trans_alpha = info_ptr->trans_alpha;1081retval |= PNG_INFO_tRNS;1082}10831084if (trans_color != NULL)1085*trans_color = &(info_ptr->trans_color);1086}10871088else /* if (info_ptr->format not colormapped */1089{1090if (trans_color != NULL)1091{1092*trans_color = &(info_ptr->trans_color);1093retval |= PNG_INFO_tRNS;1094}10951096if (trans_alpha != NULL)1097*trans_alpha = NULL;1098}10991100if (num_trans != NULL)1101{1102*num_trans = info_ptr->num_trans;1103retval |= PNG_INFO_tRNS;1104}1105}11061107return (retval);1108}1109#endif11101111#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED1112int PNGAPI1113png_get_unknown_chunks(png_const_structrp png_ptr, png_inforp info_ptr,1114png_unknown_chunkpp unknowns)1115{1116if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL)1117{1118*unknowns = info_ptr->unknown_chunks;1119return info_ptr->unknown_chunks_num;1120}11211122return 0;1123}1124#endif11251126#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED1127png_byte PNGAPI1128png_get_rgb_to_gray_status(png_const_structrp png_ptr)1129{1130if (png_ptr)1131return png_ptr->rgb_to_gray_status;1132return 0;1133}1134#endif11351136#ifdef PNG_USER_CHUNKS_SUPPORTED1137png_voidp PNGAPI1138png_get_user_chunk_ptr(png_const_structrp png_ptr)1139{1140if (png_ptr)1141return png_ptr->user_chunk_ptr;11421143return NULL;1144}1145#endif11461147png_alloc_size_t PNGAPI1148png_get_compression_buffer_size(png_const_structrp png_ptr)1149{1150if (png_ptr == NULL)1151return 0;11521153# if defined(PNG_SEQUENTIAL_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)1154return png_ptr->IDAT_size;11551156# else1157return PNG_IDAT_READ_SIZE; /* progressive reader */1158# endif1159}11601161#ifdef PNG_SET_USER_LIMITS_SUPPORTED1162/* These functions were added to libpng 1.2.6 and were enabled1163* by default in libpng-1.4.0 */1164png_uint_32 PNGAPI1165png_get_user_width_max (png_const_structrp png_ptr)1166{1167return (png_ptr ? png_ptr->user_width_max : 0);1168}11691170png_uint_32 PNGAPI1171png_get_user_height_max (png_const_structrp png_ptr)1172{1173return (png_ptr ? png_ptr->user_height_max : 0);1174}11751176/* This function was added to libpng 1.4.0 */1177png_uint_32 PNGAPI1178png_get_chunk_cache_max (png_const_structrp png_ptr)1179{1180return (png_ptr ? png_ptr->user_chunk_cache_max : 0);1181}11821183/* This function was added to libpng 1.4.1 */1184png_alloc_size_t PNGAPI1185png_get_chunk_malloc_max (png_const_structrp png_ptr)1186{1187return (png_ptr ? png_ptr->user_chunk_malloc_max : 0);1188}1189#endif /* SET_USER_LIMITS */11901191/* These functions were added to libpng 1.4.0 */1192#ifdef PNG_IO_STATE_SUPPORTED1193png_uint_32 PNGAPI1194png_get_io_state (png_const_structrp png_ptr)1195{1196return png_ptr->io_state;1197}11981199png_uint_32 PNGAPI1200png_get_io_chunk_type (png_const_structrp png_ptr)1201{1202return png_ptr->chunk_name;1203}1204#endif /* IO_STATE */1205#endif /* READ || WRITE */120612071208