本文整理汇总了C++中png_get_bKGD函数的典型用法代码示例。如果您正苦于以下问题:C++ png_get_bKGD函数的具体用法?C++ png_get_bKGD怎么用?C++ png_get_bKGD使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_get_bKGD函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readpng_get_bgcolor
int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
{
png_color_16p pBackground;
/* setjmp() must be called in every function that calls a PNG-reading
* libpng function */
if (setjmp(png_ptr->jmpbuf)) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
return 2;
}
if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
return 1;
/* it is not obvious from the libpng documentation, but this function
* takes a pointer to a pointer, and it always returns valid red, green
* and blue values, regardless of color_type: */
png_get_bKGD(png_ptr, info_ptr, &pBackground);
/* however, it always returns the raw bKGD data, regardless of any
* bit-depth transformations, so check depth and adjust if necessary */
if (bit_depth == 16) {
*red = pBackground->red >> 8;
*green = pBackground->green >> 8;
*blue = pBackground->blue >> 8;
} else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
示例2: _opng_validate_internal
__error__ "PNG_FLAG_FILLER_AFTER" has an unexpected value
#endif
#endif
/*
* Check if it's safe to access libpng's internal structures directly.
* Some fields might have their offsets shifted due to changes in
* libpng's configuration.
*/
static void
_opng_validate_internal(png_structp png_ptr, png_infop info_ptr)
{
#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
png_color_16p background;
#endif
#if defined(PNG_hIST_SUPPORTED)
png_uint_16p hist;
#endif
#if defined(PNG_sBIT_SUPPORTED)
png_color_8p sig_bit;
#endif
png_bytep trans_alpha;
int num_trans;
png_color_16p trans_color;
/* Check info_ptr. */
if (png_get_rows(png_ptr, info_ptr) != info_ptr->row_pointers)
goto error;
#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
if (png_get_bKGD(png_ptr, info_ptr, &background))
if (background != &info_ptr->background)
goto error;
#endif
#if defined(PNG_hIST_SUPPORTED)
if (png_get_hIST(png_ptr, info_ptr, &hist))
if (hist != info_ptr->hist)
goto error;
#endif
#if defined(PNG_sBIT_SUPPORTED)
if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
if (sig_bit != &info_ptr->sig_bit)
goto error;
#endif
if (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color))
if ((trans_alpha != NULL && (trans_alpha != info_ptr->trans_alpha ||
num_trans != info_ptr->num_trans)) ||
(trans_color != NULL && trans_color != &info_ptr->trans_color))
goto error;
/* Also check png_ptr. It's not much, we're doing what we can... */
if (png_get_compression_buffer_size(png_ptr) != png_ptr->zbuf_size)
goto error;
/* Everything looks okay. */
return;
error:
png_error(png_ptr,
"[internal error] Inconsistent internal structures (incorrect libpng?)");
}
示例3: writeSetup2
void writeSetup2(png_structp png_ptr_write, png_infop info_ptr_write,
png_structp png_ptr_read, png_infop info_ptr_read)
{
/* IHDR */
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int colour_type;
int interlace_method;
int compression_method;
int filter_method;
/* PLTE */
png_colorp palette = NULL;
int palette_size = 0;
/* gAMA */
double gamma;
/* tRNS */
png_bytep trans;
int num_trans;
png_color_16p trans_values;
/* bKGD */
png_color_16p background;
png_get_IHDR(png_ptr_read, info_ptr_read, &width, &height,
&bit_depth, &colour_type, &interlace_method,
&compression_method, &filter_method);
png_set_IHDR(png_ptr_write, info_ptr_write, width, height,
bit_depth, colour_type, interlace_method,
compression_method, filter_method);
if(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_PLTE))
{
png_get_PLTE(png_ptr_read, info_ptr_read, &palette, &palette_size);
png_set_PLTE(png_ptr_write, info_ptr_write, palette, palette_size);
}
if(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_gAMA))
{
png_get_gAMA(png_ptr_read, info_ptr_read, &gamma);
png_set_gAMA(png_ptr_write, info_ptr_write, gamma);
}
if(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_tRNS))
{
png_get_tRNS(png_ptr_read, info_ptr_read, &trans, &num_trans, &trans_values);
png_set_tRNS(png_ptr_write, info_ptr_write, trans, num_trans, trans_values);
}
if(png_get_valid(png_ptr_read, info_ptr_read, PNG_INFO_bKGD))
{
png_get_bKGD(png_ptr_read, info_ptr_read, &background);
png_set_bKGD(png_ptr_write, info_ptr_write, background);
}
}
示例4: read_png
static png_bytep *
read_png(png_structp png_ptr, png_infop info_ptr, at_input_opts_type * opts)
{
int row;
png_color_16p original_bg;
png_color_16 my_bg;
png_read_info(png_ptr, info_ptr);
png_set_strip_16(png_ptr);
png_set_packing(png_ptr);
if ((png_get_bit_depth(png_ptr, info_ptr) < 8) ||
(png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE) ||
(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
png_set_expand(png_ptr);
if (png_get_bKGD(png_ptr, info_ptr, &original_bg)) {
/* Fill transparent region with ... */
my_bg.index = 0;
if (opts && opts->background_color) {
my_bg.red = 256 * opts->background_color->r;
my_bg.green = 256 * opts->background_color->g;
my_bg.blue = 256 * opts->background_color->b;
my_bg.gray = 256* ((opts->background_color->r
+ opts->background_color->g
+ opts->background_color->b) / 3);
} else
/* else, use white */
my_bg.red = my_bg.green = my_bg.blue = my_bg.gray = 0xFFFF;
png_set_background(png_ptr, &my_bg,
PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
} else
png_set_strip_alpha(png_ptr);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
// PGC to replace commented lines
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_read_end(png_ptr, info_ptr);
return png_get_rows(png_ptr, info_ptr);
// info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
// info_ptr->height * sizeof(png_bytep));
//#ifdef PNG_FREE_ME_SUPPORTED
// info_ptr->free_me |= PNG_FREE_ROWS;
//#endif
// for (row = 0; row < (int)info_ptr->height; row++)
// info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
// png_get_rowbytes(png_ptr, info_ptr));
//
// png_read_image(png_ptr, info_ptr->row_pointers);
// info_ptr->valid |= PNG_INFO_IDAT;
// png_read_end(png_ptr, info_ptr);
// return png_get_rows(png_ptr, info_ptr);
}
示例5: png_set_palette_to_rgb
// Do transformations to normalize the input to 8-bpp RGBA
void LLPngWrapper::normalizeImage()
{
// 1. Expand any palettes
// 2. Convert grayscales to RGB
// 3. Create alpha layer from transparency
// 4. Ensure 8-bpp for all images
// 5. Apply background matte if any
// 6. Set (or guess) gamma
if (mColorType == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(mReadPngPtr);
}
if (mColorType == PNG_COLOR_TYPE_GRAY && mBitDepth < 8)
{
png_set_gray_1_2_4_to_8(mReadPngPtr);
}
if (mColorType == PNG_COLOR_TYPE_GRAY
|| mColorType == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(mReadPngPtr);
}
if (png_get_valid(mReadPngPtr, mReadInfoPtr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(mReadPngPtr);
}
if (mBitDepth < 8)
{
png_set_packing(mReadPngPtr);
}
else if (mBitDepth == 16)
{
png_set_strip_16(mReadPngPtr);
}
mHasBKGD = png_get_bKGD(mReadPngPtr, mReadInfoPtr, &mBackgroundColor);
if (mHasBKGD)
{
png_set_background(mReadPngPtr, mBackgroundColor,
PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
}
#if LL_DARWIN
const F64 SCREEN_GAMMA = 1.8;
#else
const F64 SCREEN_GAMMA = 2.2;
#endif
if (png_get_gAMA(mReadPngPtr, mReadInfoPtr, &mGamma))
{
png_set_gamma(mReadPngPtr, SCREEN_GAMMA, mGamma);
}
else
{
png_set_gamma(mReadPngPtr, SCREEN_GAMMA, 1/SCREEN_GAMMA);
}
}
示例6: png_read_update_info
// Read out the image meta-data
void LLPngWrapper::updateMetaData()
{
png_read_update_info(mReadPngPtr, mReadInfoPtr);
mWidth = png_get_image_width(mReadPngPtr, mReadInfoPtr);
mHeight = png_get_image_height(mReadPngPtr, mReadInfoPtr);
mBitDepth = png_get_bit_depth(mReadPngPtr, mReadInfoPtr);
mColorType = png_get_color_type(mReadPngPtr, mReadInfoPtr);
mChannels = png_get_channels(mReadPngPtr, mReadInfoPtr);
mHasBKGD = png_get_bKGD(mReadPngPtr, mReadInfoPtr, &mBackgroundColor);
}
示例7: opng_analyze_sample_usage
/*
* Analyze the usage of samples.
* The output value usage_map[n] indicates whether the sample n
* is used. The usage_map[] array must have 256 entries.
* The function requires a valid bit depth between 1 and 8.
*/
static void opng_analyze_sample_usage(png_structp png_ptr, png_infop info_ptr, png_bytep usage_map)
{
png_bytep sample_ptr;
int init_shift, init_mask, shift, mask;
png_uint_32 i, j;
png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
png_uint_32 width = png_get_image_width(png_ptr, info_ptr);
int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
png_bytepp row_ptr = png_get_rows(png_ptr, info_ptr);
/* Initialize the output entries with 0. */
memset(usage_map, 0, 256);
/* Iterate through all sample values. */
if (bit_depth == 8)
{
for (i = 0; i < height; ++i, ++row_ptr)
{
for (j = 0, sample_ptr = *row_ptr; j < width; ++j, ++sample_ptr)
usage_map[*sample_ptr] = 1;
}
}
else
{
OPNG_ASSERT(bit_depth < 8);
init_shift = 8 - bit_depth;
init_mask = (1 << 8) - (1 << init_shift);
for (i = 0; i < height; ++i, ++row_ptr)
{
for (j = 0, sample_ptr = *row_ptr; j < width; ++sample_ptr)
{
mask = init_mask;
shift = init_shift;
do
{
usage_map[(*sample_ptr & mask) >> shift] = 1;
mask >>= bit_depth;
shift -= bit_depth;
++j;
} while (mask > 0 && j < width);
}
}
}
#ifdef PNG_bKGD_SUPPORTED
png_color_16p background;
/* bKGD also counts as a used sample. */
if (png_get_bKGD(png_ptr, info_ptr, &background))
usage_map[background->index] = 1;
#endif
}
示例8: opng_reduce_bits
//.........这里部分代码省略.........
OPNG_ASSERT(src_sample_size > dest_sample_size);
png_bytepp row_ptr = png_get_rows(png_ptr, info_ptr);
for (png_uint_32 i = 0; i < height; ++i, ++row_ptr)
{
src_ptr = dest_ptr = *row_ptr;
for (png_uint_32 j = 0; j < width; ++j)
{
for (k = 0; k < dest_sample_size; ++k)
dest_ptr[k] = src_ptr[tran_tbl[k]];
src_ptr += src_sample_size;
dest_ptr += dest_sample_size;
}
}
png_color_16p trans_color;
/* Update the ancillary information. */
if (png_get_tRNS(png_ptr, info_ptr, 0, 0, &trans_color))
{
if (reductions & OPNG_REDUCE_16_TO_8)
{
if (trans_color->red % 257 == 0 &&
trans_color->green % 257 == 0 &&
trans_color->blue % 257 == 0 &&
trans_color->gray % 257 == 0)
{
trans_color->red &= 255;
trans_color->green &= 255;
trans_color->blue &= 255;
trans_color->gray &= 255;
}
else
{
/* 16-bit tRNS in 8-bit samples: all pixels are 100% opaque. */
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, -1);
png_set_invalid(png_ptr, info_ptr, PNG_INFO_tRNS);
}
}
if (reductions & OPNG_REDUCE_RGB_TO_GRAY)
{
if (trans_color->red == trans_color->green ||
trans_color->red == trans_color->blue)
trans_color->gray = trans_color->red;
else
{
/* Non-gray tRNS in grayscale image: all pixels are 100% opaque. */
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, -1);
png_set_invalid(png_ptr, info_ptr, PNG_INFO_tRNS);
}
}
}
#ifdef PNG_bKGD_SUPPORTED
png_color_16p background;
if (png_get_bKGD(png_ptr, info_ptr, &background))
{
if (reductions & OPNG_REDUCE_16_TO_8)
{
background->red &= 255;
background->green &= 255;
background->blue &= 255;
background->gray &= 255;
}
if (reductions & OPNG_REDUCE_RGB_TO_GRAY)
background->gray = background->red;
}
#endif
#ifdef PNG_sBIT_SUPPORTED
png_color_8p sig_bits;
if (png_get_sBIT(png_ptr, info_ptr, &sig_bits))
{
if (reductions & OPNG_REDUCE_16_TO_8)
{
if (sig_bits->red > 8)
sig_bits->red = 8;
if (sig_bits->green > 8)
sig_bits->green = 8;
if (sig_bits->blue > 8)
sig_bits->blue = 8;
if (sig_bits->gray > 8)
sig_bits->gray = 8;
if (sig_bits->alpha > 8)
sig_bits->alpha = 8;
}
if (reductions & OPNG_REDUCE_RGB_TO_GRAY)
{
png_byte max_sig_bits = sig_bits->red;
if (max_sig_bits < sig_bits->green)
max_sig_bits = sig_bits->green;
if (max_sig_bits < sig_bits->blue)
max_sig_bits = sig_bits->blue;
sig_bits->gray = max_sig_bits;
}
}
#endif
/* Update the image information. */
png_set_IHDR(png_ptr, info_ptr, width, height,
dest_bit_depth, dest_color_type,
interlace_type, compression_type, filter_type);
return reductions;
}
示例9: opng_reduce_to_palette
//.........这里部分代码省略.........
{
OPNG_ASSERT(num_palette < 0);
i = height; /* forced exit from outer loop */
break;
}
}
}
}
else /* grayscale */
{
for (j = 0; j < width; ++j, sample_ptr += channels)
{
gray = sample_ptr[0];
alpha = alpha_row[j];
/* Check the cache first. */
if (gray != prev_gray || alpha != prev_alpha)
{
prev_gray = gray;
prev_alpha = alpha;
if (opng_insert_palette_entry(palette, &num_palette,
trans_alpha, &num_trans, 256,
gray, gray, gray, alpha, &index) < 0) /* overflow */
{
OPNG_ASSERT(num_palette < 0);
i = height; /* forced exit from outer loop */
break;
}
}
}
}
}
#ifdef PNG_bKGD_SUPPORTED
png_color_16p background;
if ((num_palette >= 0) && png_get_bKGD(png_ptr, info_ptr, &background))
{
/* bKGD has an alpha-agnostic palette entry. */
if (color_type & PNG_COLOR_MASK_COLOR)
{
red = background->red;
green = background->green;
blue = background->blue;
}
else
red = green = blue = background->gray;
opng_insert_palette_entry(palette, &num_palette,
trans_alpha, &num_trans, 256,
red, green, blue, 256, &index);
if (index >= 0)
background->index = (png_byte)index;
}
#endif
/* Continue only if the uncompressed indexed image (pixels + PLTE + tRNS)
* is smaller than the uncompressed RGB(A) image.
* Casual overhead (headers, CRCs, etc.) is ignored.
*
* Compare:
* num_pixels * (src_bit_depth * channels - dest_bit_depth) / 8
* vs.
* sizeof(PLTE) + sizeof(tRNS)
*/
/* 5/3 times sizeof(PLTE) + sizeof(tRNS) as:
1. Palette is uncompressed additional IDAT data is
2. Headers */
if (num_palette >= 0)
{
示例10: fopen
//.........这里部分代码省略.........
return NULL;
}
/* normalize to 8bpp with alpha channel */
if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
png_set_expand(png);
if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
png_set_expand(png);
if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
png_set_expand(png);
if (depth == 16)
png_set_strip_16(png);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
/* set gamma correction */
if ((context->attribs->flags & RC_GammaCorrection)
&& context->depth != 8) {
sgamma = (context->attribs->rgamma + context->attribs->ggamma + context->attribs->bgamma) / 3;
} else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
sgamma = atof(tmp);
if (sgamma < 1.0E-3)
sgamma = 1;
} else {
/* blah */
sgamma = 2.2;
}
if (png_get_gAMA(png, pinfo, &gamma))
png_set_gamma(png, sgamma, gamma);
else
png_set_gamma(png, sgamma, 0.45);
/* do the transforms */
png_read_update_info(png, pinfo);
/* set background color */
if (png_get_bKGD(png, pinfo, &bkcolor)) {
image->background.red = bkcolor->red >> 8;
image->background.green = bkcolor->green >> 8;
image->background.blue = bkcolor->blue >> 8;
}
png_rows = calloc(height, sizeof(char *));
if (!png_rows) {
RErrorCode = RERR_NOMEMORY;
fclose(f);
RReleaseImage(image);
png_destroy_read_struct(&png, &pinfo, &einfo);
return NULL;
}
for (y = 0; y < height; y++) {
png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
if (!png_rows[y]) {
RErrorCode = RERR_NOMEMORY;
fclose(f);
RReleaseImage(image);
png_destroy_read_struct(&png, &pinfo, &einfo);
while (y-- > 0)
if (png_rows[y])
free(png_rows[y]);
free(png_rows);
return NULL;
}
}
/* read data */
png_read_image(png, png_rows);
png_read_end(png, einfo);
png_destroy_read_struct(&png, &pinfo, &einfo);
fclose(f);
ptr = image->data;
/* convert to RImage */
if (alpha) {
for (y = 0; y < height; y++) {
for (x = 0, i = width * 4; x < i; x++, ptr++) {
*ptr = *(png_rows[y] + x);
}
}
} else {
for (y = 0; y < height; y++) {
for (x = 0, i = width * 3; x < i; x++, ptr++) {
*ptr = *(png_rows[y] + x);
}
}
}
for (y = 0; y < height; y++)
if (png_rows[y])
free(png_rows[y]);
free(png_rows);
return image;
}
示例11: opng_analyze_bits
/*
* Analyze the redundancy of bits inside the image.
* The parameter reductions indicates the intended reductions.
* The function returns the possible reductions.
*/
static png_uint_32 opng_analyze_bits(png_structp png_ptr, png_infop info_ptr, png_uint_32 reductions)
{
png_uint_32 height, width;
int bit_depth, color_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
if (bit_depth < 8 || !height || !width)
return OPNG_REDUCE_NONE; /* not applicable */
if (color_type & PNG_COLOR_MASK_PALETTE)
return OPNG_REDUCE_NONE; /* let opng_reduce_palette() handle it */
int byte_depth = bit_depth / 8;
int channels = png_get_channels(png_ptr, info_ptr);
int sample_size = channels * byte_depth;
int offset_alpha = (channels - 1) * byte_depth;
/* Select the applicable reductions. */
reductions &= (OPNG_REDUCE_16_TO_8 |
OPNG_REDUCE_RGB_TO_GRAY | OPNG_REDUCE_STRIP_ALPHA);
if (bit_depth <= 8)
reductions &= ~OPNG_REDUCE_16_TO_8;
if (!(color_type & PNG_COLOR_MASK_COLOR))
reductions &= ~OPNG_REDUCE_RGB_TO_GRAY;
if (!(color_type & PNG_COLOR_MASK_ALPHA))
reductions &= ~OPNG_REDUCE_STRIP_ALPHA;
/* Check if the ancillary information allows these reductions. */
#ifdef PNG_bKGD_SUPPORTED
png_color_16p background;
if (png_get_bKGD(png_ptr, info_ptr, &background))
{
if (reductions & OPNG_REDUCE_16_TO_8)
{
if (background->red % 257 != 0 ||
background->green % 257 != 0 ||
background->blue % 257 != 0 ||
background->gray % 257 != 0)
reductions &= ~OPNG_REDUCE_16_TO_8;
}
if (reductions & OPNG_REDUCE_RGB_TO_GRAY)
{
if (background->red != background->green ||
background->red != background->blue)
reductions &= ~OPNG_REDUCE_RGB_TO_GRAY;
}
}
#endif
/* Check for each possible reduction, row by row. */
png_bytepp row_ptr = png_get_rows(png_ptr, info_ptr);
for (png_uint_32 i = 0; i < height; ++i, ++row_ptr)
{
png_bytep component_ptr;
png_uint_32 j;
if (reductions == OPNG_REDUCE_NONE)
return OPNG_REDUCE_NONE; /* no need to go any further */
/* Check if it is possible to reduce the bit depth to 8. */
if (reductions & OPNG_REDUCE_16_TO_8)
{
component_ptr = *row_ptr;
for (j = 0; j < channels * width; ++j, component_ptr += 2)
{
if (component_ptr[0] != component_ptr[1])
{
reductions &= ~OPNG_REDUCE_16_TO_8;
break;
}
}
}
if (bit_depth == 8)
{
/* Check if it is possible to reduce rgb --> gray. */
if (reductions & OPNG_REDUCE_RGB_TO_GRAY)
{
component_ptr = *row_ptr;
for (j = 0; j < width; ++j, component_ptr += sample_size)
{
if (component_ptr[0] != component_ptr[1] ||
component_ptr[0] != component_ptr[2])
{
reductions &= ~OPNG_REDUCE_RGB_TO_GRAY;
break;
}
}
}
/* Check if it is possible to strip the alpha channel. */
if (reductions & OPNG_REDUCE_STRIP_ALPHA)
{
component_ptr = *row_ptr + offset_alpha;
for (j = 0; j < width; ++j, component_ptr += sample_size)
{
if (component_ptr[0] != 255)
{
//.........这里部分代码省略.........
示例12: pngu_info
//.........这里部分代码省略.........
}
ctx->info_ptr = png_create_info_struct (ctx->png_ptr);
if (!(ctx->info_ptr))
{
if (ctx->source == PNGU_SOURCE_DEVICE)
fclose (ctx->fd);
png_destroy_read_struct (&(ctx->png_ptr), (png_infopp)NULL, (png_infopp)NULL);
return PNGU_LIB_ERROR;
}
if (ctx->source == PNGU_SOURCE_BUFFER)
{
// Installation of our custom data provider function
ctx->cursor = 0;
png_set_read_fn (ctx->png_ptr, ctx, pngu_read_data_from_buffer);
}
else if (ctx->source == PNGU_SOURCE_DEVICE)
{
// Default data provider uses function fread, so it needs to use our FILE*
png_init_io (ctx->png_ptr, ctx->fd);
png_set_sig_bytes (ctx->png_ptr, 8); // We have read 8 bytes already to check PNG authenticity
}
// Read png header
png_read_info (ctx->png_ptr, ctx->info_ptr);
// Query image properties if they have not been queried before
if (!ctx->propRead)
{
int ctxNumTrans;
png_get_IHDR(ctx->png_ptr, ctx->info_ptr, &width, &height,
(int *) &(ctx->prop.imgBitDepth),
(int *) &(ctx->prop.imgColorType),
NULL, NULL, NULL);
ctx->prop.imgWidth = width;
ctx->prop.imgHeight = height;
switch (ctx->prop.imgColorType)
{
case PNG_COLOR_TYPE_GRAY:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_GRAY;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_GRAY_ALPHA;
break;
case PNG_COLOR_TYPE_PALETTE:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_PALETTE;
break;
case PNG_COLOR_TYPE_RGB:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_RGB_ALPHA;
break;
default:
ctx->prop.imgColorType = PNGU_COLOR_TYPE_UNKNOWN;
break;
}
// Constant used to scale 16 bit values to 8 bit values
scale = 0;
if (ctx->prop.imgBitDepth == 16)
scale = 8;
// Query background color, if any.
ctx->prop.validBckgrnd = 0;
switch(ctx->prop.imgColorType)
{
case PNGU_COLOR_TYPE_RGB:
case PNGU_COLOR_TYPE_RGB_ALPHA:
{
if(png_get_bKGD (ctx->png_ptr, ctx->info_ptr, &background)){
ctx->prop.validBckgrnd = 1;
ctx->prop.bckgrnd.r = background->red >> scale;
ctx->prop.bckgrnd.g = background->green >> scale;
ctx->prop.bckgrnd.b = background->blue >> scale;
}
// Query list of transparent colors, if any.
ctx->prop.numTrans = 0;
ctx->prop.trans = NULL;
if(png_get_tRNS (ctx->png_ptr, ctx->info_ptr, &trans, (int *) &(ctx->prop.numTrans), &trans_values)){
ctxNumTrans = ctx->prop.numTrans;
if(ctxNumTrans){
ctx->prop.trans = malloc (sizeof (PNGUCOLOR) * ctxNumTrans);
if (ctx->prop.trans)
for (i = 0; i < ctxNumTrans; i++)
{
ctx->prop.trans[i].r = trans_values[i].red >> scale;
ctx->prop.trans[i].g = trans_values[i].green >> scale;
ctx->prop.trans[i].b = trans_values[i].blue >> scale;
}
else
ctx->prop.numTrans = 0;
}
}
示例13: ReadPNG
//.........这里部分代码省略.........
/* read all PNG data up to the image data */
png_read_info( png_ptr, info_ptr );
/* extract the data we need to form the HBITMAP from the PNG header */
png_get_IHDR( png_ptr, info_ptr, &Width, &Height, &BitDepth, &ColorType,
&InterlaceType, NULL, NULL);
img->width = (unsigned int) Width;
img->height = (unsigned int) Height;
img->bits_per_pixel = (unsigned char)32;
img->scan_width = Width * 4;
/* convert 16-bit images to 8-bit images */
if (BitDepth == 16)
png_set_strip_16(png_ptr);
/* These are not really required per Rice format spec,
* but is done just in case someone uses them.
*/
/* convert palette color to rgb color */
if (ColorType == PNG_COLOR_TYPE_PALETTE) {
png_set_palette_to_rgb(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB;
}
/* expand 1,2,4 bit gray scale to 8 bit gray scale */
if (ColorType == PNG_COLOR_TYPE_GRAY && BitDepth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
/* convert gray scale or gray scale + alpha to rgb color */
if (ColorType == PNG_COLOR_TYPE_GRAY ||
ColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
png_set_gray_to_rgb(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB;
}
/* add alpha channel if any */
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png_ptr);
ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
}
/* convert rgb to rgba */
if (ColorType == PNG_COLOR_TYPE_RGB) {
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
ColorType = PNG_COLOR_TYPE_RGB_ALPHA;
}
png_set_bgr(png_ptr);
/* set the background color if one is found */
if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD) )
png_get_bKGD(png_ptr, info_ptr, &ImageBackground);
/* get the transparent color if one is there */
if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) )
png_get_tRNS( png_ptr, info_ptr, &trns, &NumTrans, &TransColors );
img->palette_size = (unsigned short)0;
img->bytes_per_palette_entry = 4U;
tmp = AllocateBMGImage( img );
if ( tmp != BMG_OK )
longjmp( err_jmp, (int)tmp );
png_read_update_info( png_ptr, info_ptr );
/* create buffer to read data to */
rows = (unsigned char **)malloc(Height*sizeof(unsigned char *));
if ( !rows )
longjmp( err_jmp, (int)errMemoryAllocation );
k = png_get_rowbytes( png_ptr, info_ptr );
rows[0] = (unsigned char *)malloc( Height*k*sizeof(char));
if ( !rows[0] )
longjmp( err_jmp, (int)errMemoryAllocation );
for ( i = 1; i < (int)Height; i++ )
rows[i] = rows[i-1] + k;
/* read the entire image into rows */
png_read_image( png_ptr, rows );
bits = img->bits + (Height - 1) * img->scan_width;
for ( i = 0; i < (int)Height; i++ )
{
memcpy(bits, rows[i], 4*Width);
bits -= img->scan_width;
}
free( rows[0] );
free( rows );
png_read_end( png_ptr, info_ptr );
png_destroy_read_struct((png_structp *) &png_ptr, (png_infop *) &info_ptr, (png_infop *) &end_info);
fclose( file );
return BMG_OK;
}
示例14: read_PNG
//.........这里部分代码省略.........
/* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
* byte into separate bytes (useful for paletted and grayscale images).
*/
png_set_packing(png_ptr);
/* Change the order of packed pixels to least significant bit first
* (not useful if you are using png_set_packing). */
png_set_packswap(png_ptr);
/* Expand paletted colors into true RGB triplets */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
/* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
/* Expand paletted or RGB images with transparency to full alpha channels
* so the data will be available as RGBA quartets.
*/
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
/* Set the background color to draw transparent and alpha images over.
* It is possible to set the red, green, and blue components directly
* for paletted images instead of supplying a palette index. Note that
* even if the PNG file supplies a background, you are not required to
* use it - you should use the (solid) application background if it has one.
*/
png_color_16 my_background, *image_background;
if (png_get_bKGD(png_ptr, info_ptr, &image_background))
png_set_background(png_ptr, image_background,
PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
else
png_set_background(png_ptr, &my_background,
PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
screen_gamma = 1.0;
int intent;
if (png_get_sRGB(png_ptr, info_ptr, &intent))
png_set_gamma(png_ptr, screen_gamma, 0.45455);
else
{
double image_gamma;
if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
png_set_gamma(png_ptr, screen_gamma, image_gamma);
else
png_set_gamma(png_ptr, screen_gamma, 0.45455);
}
/* Invert monochrome files to have 0 as white and 1 as black */
png_set_invert_mono(png_ptr);
/* If you want to shift the pixel values from the range [0,255] or
* [0,65535] to the original [0,7] or [0,31], or whatever range the
* colors were originally in:
*/
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
{
png_color_8p sig_bit_p;
示例15: Load
//.........这里部分代码省略.........
// array of alpha (transparency) entries for palette
png_bytep trans_alpha = NULL;
// number of transparent entries
int num_trans = 0;
// graylevel or color sample values of the single transparent color for non-paletted images
png_color_16p trans_color = NULL;
png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color);
if((color_type == PNG_COLOR_TYPE_GRAY) && trans_color) {
// single transparent color
if (trans_color->gray < palette_entries) {
BYTE table[256];
memset(table, 0xFF, palette_entries);
table[trans_color->gray] = 0;
FreeImage_SetTransparencyTable(dib, table, palette_entries);
}
} else if((color_type == PNG_COLOR_TYPE_PALETTE) && trans_alpha) {
// transparency table
FreeImage_SetTransparencyTable(dib, (BYTE *)trans_alpha, num_trans);
}
}
// store the background color
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD)) {
// Get the background color to draw transparent and alpha images over.
// Note that even if the PNG file supplies a background, you are not required to
// use it - you should use the (solid) application background if it has one.
png_color_16p image_background = NULL;
RGBQUAD rgbBkColor;
if (png_get_bKGD(png_ptr, info_ptr, &image_background)) {
rgbBkColor.rgbRed = (BYTE)image_background->red;
rgbBkColor.rgbGreen = (BYTE)image_background->green;
rgbBkColor.rgbBlue = (BYTE)image_background->blue;
rgbBkColor.rgbReserved = 0;
FreeImage_SetBackgroundColor(dib, &rgbBkColor);
}
}
// get physical resolution
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
png_uint_32 res_x, res_y;
// we'll overload this var and use 0 to mean no phys data,
// since if it's not in meters we can't use it anyway
int res_unit_type = PNG_RESOLUTION_UNKNOWN;
png_get_pHYs(png_ptr,info_ptr, &res_x, &res_y, &res_unit_type);
if (res_unit_type == PNG_RESOLUTION_METER) {
FreeImage_SetDotsPerMeterX(dib, res_x);
FreeImage_SetDotsPerMeterY(dib, res_y);
}
}
// get possible ICC profile
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
png_charp profile_name = NULL;
png_bytep profile_data = NULL;