本文整理汇总了C++中png_set_gamma函数的典型用法代码示例。如果您正苦于以下问题:C++ png_set_gamma函数的具体用法?C++ png_set_gamma怎么用?C++ png_set_gamma使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了png_set_gamma函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: loadPngFile
bool
loadPngFile(
IMAGE_T* image,
FILE *file)
{
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL,
NULL,
NULL);
if (png_ptr == NULL)
{
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr, 0, 0);
return false;
}
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return false;
}
//---------------------------------------------------------------------
png_init_io(png_ptr, file);
png_read_info(png_ptr, info_ptr);
//---------------------------------------------------------------------
png_byte colour_type = png_get_color_type(png_ptr, info_ptr);
png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
VC_IMAGE_TYPE_T type = VC_IMAGE_RGB888;
if (colour_type & PNG_COLOR_MASK_ALPHA)
{
type = VC_IMAGE_RGBA32;
}
initImage(image,
type,
png_get_image_width(png_ptr, info_ptr),
png_get_image_height(png_ptr, info_ptr),
false);
//---------------------------------------------------------------------
double gamma = 0.0;
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
{
png_set_gamma(png_ptr, 2.2, gamma);
}
//---------------------------------------------------------------------
if (colour_type == PNG_COLOR_TYPE_PALETTE)
{
png_set_palette_to_rgb(png_ptr);
}
if ((colour_type == PNG_COLOR_TYPE_GRAY) && (bit_depth < 8))
{
png_set_expand_gray_1_2_4_to_8(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
{
png_set_tRNS_to_alpha(png_ptr);
}
if (bit_depth == 16)
{
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
png_set_scale_16(png_ptr);
#else
png_set_strip_16(png_ptr);
#endif
}
if (colour_type == PNG_COLOR_TYPE_GRAY ||
colour_type == PNG_COLOR_TYPE_GRAY_ALPHA)
{
png_set_gray_to_rgb(png_ptr);
}
//---------------------------------------------------------------------
png_read_update_info(png_ptr, info_ptr);
//---------------------------------------------------------------------
//.........这里部分代码省略.........
示例2: png_include_image
int
png_include_image (pdf_ximage *ximage, FILE *png_file)
{
pdf_obj *stream;
pdf_obj *stream_dict;
pdf_obj *colorspace, *mask, *intent;
png_bytep stream_data_ptr;
int trans_type;
ximage_info info;
/* Libpng stuff */
png_structp png_ptr;
png_infop png_info_ptr;
png_byte bpc, color_type;
png_uint_32 width, height, rowbytes;
pdf_ximage_init_image_info(&info);
stream = NULL;
stream_dict = NULL;
colorspace = mask = intent = NULL;
rewind (png_file);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, warn);
if (png_ptr == NULL ||
(png_info_ptr = png_create_info_struct (png_ptr)) == NULL) {
WARN("%s: Creating Libpng read/info struct failed.", PNG_DEBUG_STR);
if (png_ptr)
png_destroy_read_struct(&png_ptr, NULL, NULL);
return -1;
}
#if PNG_LIBPNG_VER >= 10603
/* ignore possibly incorrect CMF bytes */
png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);
#endif
/* Inititializing file IO. */
png_init_io (png_ptr, png_file);
/* Read PNG info-header and get some info. */
png_read_info(png_ptr, png_info_ptr);
color_type = png_get_color_type (png_ptr, png_info_ptr);
width = png_get_image_width (png_ptr, png_info_ptr);
height = png_get_image_height(png_ptr, png_info_ptr);
bpc = png_get_bit_depth (png_ptr, png_info_ptr);
/* Ask libpng to convert down to 8-bpc. */
if (bpc > 8) {
if (pdf_get_version() < 5) {
WARN("%s: 16-bpc PNG requires PDF version 1.5.", PNG_DEBUG_STR);
png_set_strip_16(png_ptr);
bpc = 8;
}
}
/* Ask libpng to gamma-correct.
* It is wrong to assume screen gamma value 2.2 but...
* We do gamma correction here only when uncalibrated color space is used.
*/
if (!png_get_valid(png_ptr, png_info_ptr, PNG_INFO_iCCP) &&
!png_get_valid(png_ptr, png_info_ptr, PNG_INFO_sRGB) &&
!png_get_valid(png_ptr, png_info_ptr, PNG_INFO_cHRM) &&
png_get_valid(png_ptr, png_info_ptr, PNG_INFO_gAMA)) {
double G = 1.0;
png_get_gAMA (png_ptr, png_info_ptr, &G);
png_set_gamma(png_ptr, 2.2, G);
}
trans_type = check_transparency(png_ptr, png_info_ptr);
/* check_transparency() does not do updata_info() */
png_read_update_info(png_ptr, png_info_ptr);
rowbytes = png_get_rowbytes(png_ptr, png_info_ptr);
/* Values listed below will not be modified in the remaining process. */
info.width = width;
info.height = height;
info.bits_per_component = bpc;
if (compat_mode)
info.xdensity = info.ydensity = 72.0 / 100.0;
else
{
png_uint_32 xppm = png_get_x_pixels_per_meter(png_ptr, png_info_ptr);
png_uint_32 yppm = png_get_y_pixels_per_meter(png_ptr, png_info_ptr);
if (xppm > 0)
info.xdensity = 72.0 / 0.0254 / xppm;
if (yppm > 0)
info.ydensity = 72.0 / 0.0254 / yppm;
}
stream = pdf_new_stream (STREAM_COMPRESS);
stream_dict = pdf_stream_dict(stream);
stream_data_ptr = (png_bytep) NEW(rowbytes*height, png_byte);
read_image_data(png_ptr, stream_data_ptr, height, rowbytes);
/* Non-NULL intent means there is valid sRGB chunk. */
intent = get_rendering_intent(png_ptr, png_info_ptr);
if (intent)
pdf_add_dict(stream_dict, pdf_new_name("Intent"), intent);
//.........这里部分代码省略.........
示例3: SplashDecodePng
int
SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr)
{
int stride;
ImageFormat srcFormat;
png_uint_32 i, rowbytes;
png_bytepp row_pointers = NULL;
png_bytep image_data = NULL;
int success = 0;
double gamma;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_uint_32 width, height;
int bit_depth, color_type;
ImageRect srcRect, dstRect;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
goto done;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
goto done;
}
if (setjmp(png_ptr->jmpbuf)) {
goto done;
}
png_ptr->io_ptr = io_ptr;
png_ptr->read_data_fn = read_func;
png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */
png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
* transparency chunks to full alpha channel; strip 16-bit-per-sample
* images to 8 bits per sample; and convert grayscale to RGB[A]
* this may be sub-optimal but this simplifies implementation */
png_set_expand(png_ptr);
png_set_tRNS_to_alpha(png_ptr);
png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
if (png_get_gAMA(png_ptr, info_ptr, &gamma))
png_set_gamma(png_ptr, 2.2, gamma);
png_read_update_info(png_ptr, info_ptr);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if (!SAFE_TO_ALLOC(rowbytes, height)) {
goto done;
}
if ((image_data = (unsigned char *) malloc(rowbytes * height)) == NULL) {
goto done;
}
if (!SAFE_TO_ALLOC(height, sizeof(png_bytep))) {
goto done;
}
if ((row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep)))
== NULL) {
goto done;
}
for (i = 0; i < height; ++i)
row_pointers[i] = image_data + i * rowbytes;
png_read_image(png_ptr, row_pointers);
SplashCleanup(splash);
splash->width = width;
splash->height = height;
if (!SAFE_TO_ALLOC(splash->width, splash->imageFormat.depthBytes)) {
goto done;
}
stride = splash->width * splash->imageFormat.depthBytes;
if (!SAFE_TO_ALLOC(splash->height, stride)) {
goto done;
}
splash->frameCount = 1;
splash->frames = (SplashImage *)
malloc(sizeof(SplashImage) * splash->frameCount);
if (splash->frames == NULL) {
//.........这里部分代码省略.........
示例4: 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);
示例5: png2pnm
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha)
{
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
png_byte buf[8];
png_byte *png_pixels = NULL;
png_byte **row_pointers = NULL;
png_byte *pix_ptr = NULL;
png_uint_32 row_bytes;
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int channels;
int color_type;
int alpha_present;
int row, col;
int ret;
int i;
long dep_16;
/* read and check signature in PNG file */
ret = fread (buf, 1, 8, png_file);
if (ret != 8)
return FALSE;
ret = !png_sig_cmp (buf, 0, 8);
if (!ret)
return FALSE;
/* create png and info structures */
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
return FALSE; /* out of memory */
info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr)
{
png_destroy_read_struct (&png_ptr, NULL, NULL);
return FALSE; /* out of memory */
}
if (setjmp (png_jmpbuf(png_ptr)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return FALSE;
}
/* set up the input control for C streams */
png_init_io (png_ptr, png_file);
png_set_sig_bytes (png_ptr, 8); /* we already read the 8 signature bytes */
/* read the file information */
png_read_info (png_ptr, info_ptr);
/* get size and bit-depth of the PNG-image */
png_get_IHDR (png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* set-up the transformations */
/* transform paletted images into full-color rgb */
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand (png_ptr);
/* expand images to bit-depth 8 (only applicable for grayscale images) */
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand (png_ptr);
/* transform transparency maps into full alpha-channel */
if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand (png_ptr);
#ifdef NJET
/* downgrade 16-bit images to 8 bit */
if (bit_depth == 16)
png_set_strip_16 (png_ptr);
/* transform grayscale images into full-color */
if (color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb (png_ptr);
/* only if file has a file gamma, we do a correction */
if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
png_set_gamma (png_ptr, (double) 2.2, file_gamma);
#endif
/* all transformations have been registered; now update info_ptr data,
* get rowbytes and channels, and allocate image memory */
png_read_update_info (png_ptr, info_ptr);
/* get the new color-type and bit-depth (after expansion/stripping) */
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* check for 16-bit files */
if (bit_depth == 16)
{
raw = FALSE;
//.........这里部分代码省略.........
示例6: ReadPNG
//.........这里部分代码省略.........
png_get_hIST(png_ptr, info_ptr, &hist);
png_set_quantize(png_ptr, palette, num_palette,
get_pref_int(eCOLORS_PER_INLINED_IMAGE), hist, 1);
}
}
/* PNG files pack pixels of bit depths 1, 2, and 4 into bytes as
small as they can. This expands pixels to 1 pixel per byte, and
if a transparency value is supplied, an alpha channel is
built.*/
if (bit_depth < 8)
png_set_packing(png_ptr);
/* have libpng handle the gamma conversion */
if (get_pref_boolean(eUSE_SCREEN_GAMMA)) { /*SWP*/
if (bit_depth != 16) { /* temporary .. glennrp */
screen_gamma=(double)(get_pref_float(eSCREEN_GAMMA));
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"screen gamma=%f\n",screen_gamma);
}
#endif
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
#ifndef DISABLE_TRACE
if (srcTrace) {
printf("setting gamma=%f\n", gamma);
}
#endif
png_set_gamma(png_ptr, screen_gamma, gamma);
}
else {
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"setting gamma=%f\n",0.45);
}
#endif
png_set_gamma(png_ptr, screen_gamma, (double)0.45);
}
}
}
if (interlace_type)
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &raw_width, &raw_height, &bit_depth,
&color_type, &interlace_type, &compression_type,
&filter_type);
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
have_palette = png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"\n\nAFTER\nwidth = %d\n", *width);
fprintf(stderr,"height = %d\n", *height);
fprintf(stderr,"bit depth = %d\n", bit_depth);
fprintf(stderr,"color type = %d\n", color_type);
fprintf(stderr,"compression type = %d\n", compression_type);
fprintf(stderr,"filter type = %d\n", filter_type);
fprintf(stderr,"interlace type = %d\n", interlace_type);
示例7: setup_qt
static
void setup_qt(QImage& image, png_structp png_ptr, png_infop info_ptr, float screen_gamma=0.0)
{
if (screen_gamma != 0.0 && png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
double file_gamma;
png_get_gAMA(png_ptr, info_ptr, &file_gamma);
png_set_gamma(png_ptr, screen_gamma, file_gamma);
}
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
png_bytep trans_alpha = 0;
png_color_16p trans_color_p = 0;
int num_trans;
png_colorp palette = 0;
int num_palette;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
png_set_interlace_handling(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY) {
// Black & White or 8-bit grayscale
if (bit_depth == 1 && png_get_channels(png_ptr, info_ptr) == 1) {
png_set_invert_mono(png_ptr);
png_read_update_info(png_ptr, info_ptr);
if (image.size() != QSize(width, height) || image.format() != QImage::Format_Mono) {
image = QImage(width, height, QImage::Format_Mono);
if (image.isNull())
return;
}
image.setColorCount(2);
image.setColor(1, qRgb(0,0,0));
image.setColor(0, qRgb(255,255,255));
} else if (bit_depth == 16 && png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_set_expand(png_ptr);
png_set_strip_16(png_ptr);
png_set_gray_to_rgb(png_ptr);
if (image.size() != QSize(width, height) || image.format() != QImage::Format_ARGB32) {
image = QImage(width, height, QImage::Format_ARGB32);
if (image.isNull())
return;
}
if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
png_set_swap_alpha(png_ptr);
png_read_update_info(png_ptr, info_ptr);
} else {
if (bit_depth == 16)
png_set_strip_16(png_ptr);
else if (bit_depth < 8)
png_set_packing(png_ptr);
int ncols = bit_depth < 8 ? 1 << bit_depth : 256;
png_read_update_info(png_ptr, info_ptr);
if (image.size() != QSize(width, height) || image.format() != QImage::Format_Indexed8) {
image = QImage(width, height, QImage::Format_Indexed8);
if (image.isNull())
return;
}
image.setColorCount(ncols);
for (int i=0; i<ncols; i++) {
int c = i*255/(ncols-1);
image.setColor(i, qRgba(c,c,c,0xff));
}
if (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color_p) && trans_color_p) {
const int g = trans_color_p->gray;
if (g < ncols) {
image.setColor(g, 0);
}
}
}
} else if (color_type == PNG_COLOR_TYPE_PALETTE
&& png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)
&& num_palette <= 256)
{
// 1-bit and 8-bit color
if (bit_depth != 1)
png_set_packing(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 0, 0, 0);
QImage::Format format = bit_depth == 1 ? QImage::Format_Mono : QImage::Format_Indexed8;
if (image.size() != QSize(width, height) || image.format() != format) {
image = QImage(width, height, format);
if (image.isNull())
return;
}
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
image.setColorCount(num_palette);
int i = 0;
if (png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color_p) && trans_alpha) {
while (i < num_trans) {
image.setColor(i, qRgba(
palette[i].red,
palette[i].green,
palette[i].blue,
trans_alpha[i]
)
);
i++;
}
//.........这里部分代码省略.........
示例8: png_create_info_struct
CDibSection* InformApp::GetImage(const char* path)
{
// Check if it's a PNG file
CStdioFile imageFile;
if (!imageFile.Open(path,CFile::modeRead|CFile::typeBinary))
return 0;
png_byte fileHeader[8];
imageFile.Read(fileHeader,8);
bool isPNG = (png_sig_cmp(fileHeader,0,8) == 0);
if (isPNG)
{
// Prepare to read the PNG file
png_structp png_ptr = png_create_read_struct
(PNG_LIBPNG_VER_STRING,(png_voidp)NULL,NULL,NULL);
if (png_ptr == NULL)
return 0;
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL)
{
png_destroy_read_struct(&png_ptr,(png_infopp)NULL,(png_infopp)NULL);
return 0;
}
png_infop end_info = png_create_info_struct(png_ptr);
if (end_info == NULL)
{
png_destroy_read_struct(&png_ptr,&info_ptr,(png_infopp)NULL);
return 0;
}
// Set up the point to return to in case of error
png_bytep* rowPointers = NULL;
if (setjmp(png_jmpbuf(png_ptr)))
{
if (rowPointers)
delete[] rowPointers;
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
return 0;
}
png_init_io(png_ptr,imageFile.m_pStream);
png_set_sig_bytes(png_ptr,8);
png_read_info(png_ptr,info_ptr);
// Get details of the image
png_uint_32 width = png_get_image_width(png_ptr,info_ptr);
png_uint_32 height = png_get_image_height(png_ptr,info_ptr);
int bit_depth = png_get_bit_depth(png_ptr,info_ptr);
int color_type = png_get_color_type(png_ptr,info_ptr);
// Set up transforms to the required format
if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
png_set_expand_gray_1_2_4_to_8(png_ptr);
if (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
double gamma;
if (png_get_gAMA(png_ptr,info_ptr,&gamma))
png_set_gamma(png_ptr,2.2,gamma);
if (bit_depth == 16)
png_set_strip_16(png_ptr);
if (bit_depth < 8)
png_set_packing(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
png_set_bgr(png_ptr);
png_set_filler(png_ptr,0xFF,PNG_FILLER_AFTER);
// Create a bitmap
HDC dc = ::GetDC(NULL);
CDibSection* dib = new CDibSection();
BOOL created = dib->CreateBitmap(dc,width,height);
::ReleaseDC(NULL,dc);
if (!created)
{
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
return NULL;
}
// Read in the image
rowPointers = new png_bytep[height];
for (int i = 0; i < (int)height; i++)
rowPointers[i] = ((png_bytep)dib->GetBits())+(width*i*4);
png_read_image(png_ptr,rowPointers);
png_read_end(png_ptr,end_info);
delete[] rowPointers;
png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
return dib;
}
else
{
imageFile.SeekToBegin();
struct jpeg_decompress_struct info;
struct JPEGErrorInfo error;
// Initialize the error handling
info.err = jpeg_std_error(&(error.base));
//.........这里部分代码省略.........
示例9: png_get_image_width
void PNGImageDecoder::headerAvailable()
{
png_structp png = m_reader->pngPtr();
png_infop info = m_reader->infoPtr();
png_uint_32 width = png_get_image_width(png, info);
png_uint_32 height = png_get_image_height(png, info);
// Protect against large PNGs. See http://bugzil.la/251381 for more details.
const unsigned long maxPNGSize = 1000000UL;
if (width > maxPNGSize || height > maxPNGSize) {
longjmp(JMPBUF(png), 1);
return;
}
// Set the image size now that the image header is available.
if (!setSize(width, height)) {
longjmp(JMPBUF(png), 1);
return;
}
int bitDepth, colorType, interlaceType, compressionType, filterType, channels;
png_get_IHDR(png, info, &width, &height, &bitDepth, &colorType, &interlaceType, &compressionType, &filterType);
// The options we set here match what Mozilla does.
// Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
if (colorType == PNG_COLOR_TYPE_PALETTE || (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8))
png_set_expand(png);
png_bytep trns = 0;
int trnsCount = 0;
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
png_get_tRNS(png, info, &trns, &trnsCount, 0);
png_set_expand(png);
}
if (bitDepth == 16)
png_set_strip_16(png);
if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png);
#if USE(QCMSLIB)
if ((colorType & PNG_COLOR_MASK_COLOR) && !m_ignoreGammaAndColorProfile) {
// We only support color profiles for color PALETTE and RGB[A] PNG. Supporting
// color profiles for gray-scale images is slightly tricky, at least using the
// CoreGraphics ICC library, because we expand gray-scale images to RGB but we
// do not similarly transform the color profile. We'd either need to transform
// the color profile or we'd need to decode into a gray-scale image buffer and
// hand that to CoreGraphics.
bool sRGB = false;
ColorProfile colorProfile;
getColorProfile(png, info, colorProfile, sRGB);
bool imageHasAlpha = (colorType & PNG_COLOR_MASK_ALPHA) || trnsCount;
m_reader->createColorTransform(colorProfile, imageHasAlpha, sRGB);
m_hasColorProfile = !!m_reader->colorTransform();
}
#endif
if (!m_hasColorProfile) {
// Deal with gamma and keep it under our control.
const double inverseGamma = 0.45455;
const double defaultGamma = 2.2;
double gamma;
if (!m_ignoreGammaAndColorProfile && png_get_gAMA(png, info, &gamma)) {
const double maxGamma = 21474.83;
if ((gamma <= 0.0) || (gamma > maxGamma)) {
gamma = inverseGamma;
png_set_gAMA(png, info, gamma);
}
png_set_gamma(png, defaultGamma, gamma);
} else {
png_set_gamma(png, defaultGamma, inverseGamma);
}
}
// Tell libpng to send us rows for interlaced pngs.
if (interlaceType == PNG_INTERLACE_ADAM7)
png_set_interlace_handling(png);
// Update our info now.
png_read_update_info(png, info);
channels = png_get_channels(png, info);
ASSERT(channels == 3 || channels == 4);
m_reader->setHasAlpha(channels == 4);
if (m_reader->decodingSizeOnly()) {
// If we only needed the size, halt the reader.
#if PNG_LIBPNG_VER_MAJOR > 1 || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 5)
// '0' argument to png_process_data_pause means: Do not cache unprocessed data.
m_reader->setReadOffset(m_reader->currentBufferSize() - png_process_data_pause(png, 0));
#else
m_reader->setReadOffset(m_reader->currentBufferSize() - png->buffer_size);
png->buffer_size = 0;
#endif
}
}
示例10: setup_qt
static
void setup_qt( QImage& image, png_structp png_ptr, png_infop info_ptr )
{
if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) ) {
double file_gamma;
png_get_gAMA(png_ptr, info_ptr, &file_gamma);
#if defined(_OS_MAC_)
// a good guess for Mac systems
png_set_gamma( png_ptr, 1.7, file_gamma );
#else
// a good guess for PC monitors in a bright office or a dim room
png_set_gamma( png_ptr, 2.2, file_gamma );
#endif
}
png_uint_32 width;
png_uint_32 height;
int bit_depth;
int color_type;
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
0, 0, 0);
if ( color_type == PNG_COLOR_TYPE_GRAY ) {
// Black & White or 8-bit greyscale
if ( bit_depth == 1 && info_ptr->channels == 1 ) {
png_set_invert_mono( png_ptr );
png_read_update_info( png_ptr, info_ptr );
image.create( width, height, 1, 2, QImage::BigEndian );
image.setColor( 1, qRgb(0,0,0) );
image.setColor( 0, qRgb(255,255,255) );
} else {
if ( bit_depth == 16 )
png_set_strip_16(png_ptr);
else if ( bit_depth < 8 )
png_set_packing(png_ptr);
int ncols = bit_depth < 8 ? 1 << bit_depth : 256;
png_read_update_info(png_ptr, info_ptr);
image.create(width, height, 8, ncols);
for (int i=0; i<ncols; i++) {
int c = i*255/(ncols-1);
image.setColor( i, qRgba(c,c,c,0xff) );
}
if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) {
int g = info_ptr->trans_values.gray;
if ( bit_depth > 8 ) {
// transparency support disabled for now
} else {
image.setAlphaBuffer( TRUE );
image.setColor(g, RGB_MASK & image.color(g));
}
}
}
} else if ( color_type == PNG_COLOR_TYPE_PALETTE
&& png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)
&& info_ptr->num_palette <= 256 )
{
// 1-bit and 8-bit color
if ( bit_depth != 1 )
png_set_packing( png_ptr );
png_read_update_info( png_ptr, info_ptr );
png_get_IHDR(png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type, 0, 0, 0);
image.create(width, height, bit_depth, info_ptr->num_palette,
QImage::BigEndian);
int i = 0;
if ( png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ) {
image.setAlphaBuffer( TRUE );
while ( i < info_ptr->num_trans ) {
image.setColor(i, qRgba(
info_ptr->palette[i].red,
info_ptr->palette[i].green,
info_ptr->palette[i].blue,
info_ptr->trans[i]
)
);
i++;
}
}
while ( i < info_ptr->num_palette ) {
image.setColor(i, qRgba(
info_ptr->palette[i].red,
info_ptr->palette[i].green,
info_ptr->palette[i].blue,
0xff
)
);
i++;
}
} else {
// 32-bit
if ( bit_depth == 16 )
png_set_strip_16(png_ptr);
png_set_expand(png_ptr);
if ( color_type == PNG_COLOR_TYPE_GRAY_ALPHA )
png_set_gray_to_rgb(png_ptr);
image.create(width, height, 32);
//.........这里部分代码省略.........
示例11: Load
//.........这里部分代码省略.........
if (pixel_depth == 2) {
png_set_expand_gray_1_2_4_to_8(png_ptr);
pixel_depth = 8;
}
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
// expand 8-bit greyscale + 8-bit alpha to 32-bit
png_set_gray_to_rgb(png_ptr);
#if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
// flip the RGBA pixels to BGRA
png_set_bgr(png_ptr);
#endif
pixel_depth = 32;
break;
default:
throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
}
// unlike the example in the libpng documentation, we have *no* idea where
// this file may have come from--so if it doesn't have a file gamma, don't
// do any correction ("do no harm")
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA)) {
double gamma = 0;
double screen_gamma = 2.2;
if (png_get_gAMA(png_ptr, info_ptr, &gamma) && ( flags & PNG_IGNOREGAMMA ) != PNG_IGNOREGAMMA) {
png_set_gamma(png_ptr, screen_gamma, gamma);
}
}
// all transformations have been registered; now update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// color type may have changed, due to our transformations
color_type = png_get_color_type(png_ptr,info_ptr);
// create a DIB and write the bitmap header
// set up the DIB palette, if needed
switch (color_type) {
case PNG_COLOR_TYPE_RGB:
png_set_invert_alpha(png_ptr);
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
if(image_type == FIT_BITMAP) {
dib = FreeImage_AllocateHeader(header_only, width, height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
} else {
dib = FreeImage_AllocateHeaderT(header_only, image_type, width, height, pixel_depth);
}
break;
示例12: png_create_read_struct
//.........这里部分代码省略.........
&BitDepth, &ColorType, NULL, NULL, NULL);
Width=w;
Height=h;
}
// Convert palette color to true color
if (ColorType==PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
// Convert low bit colors to 8 bit colors
if (BitDepth < 8)
{
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_expand_gray_1_2_4_to_8(png_ptr);
else
png_set_packing(png_ptr);
}
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(png_ptr);
// Convert high bit colors to 8 bit colors
if (BitDepth == 16)
png_set_strip_16(png_ptr);
// Convert gray color to true color
if (ColorType==PNG_COLOR_TYPE_GRAY || ColorType==PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
int intent;
const double screen_gamma = 2.2;
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);
}
// Update the changes in between, as we need to get the new color type
// for proper processing of the RGBA type
png_read_update_info(png_ptr, info_ptr);
{
// Use temporary variables to avoid passing casted pointers
png_uint_32 w,h;
// Extract info
png_get_IHDR(png_ptr, info_ptr,
&w, &h,
&BitDepth, &ColorType, NULL, NULL, NULL);
Width=w;
Height=h;
}
// Convert RGBA to BGRA
if (ColorType==PNG_COLOR_TYPE_RGB_ALPHA)
{
png_set_bgr(png_ptr);
}
// Create the image structure to be filled by png data
Picture* pic = GfxEngine::instance().createPicture( Size( Width, Height ) );
GfxEngine::instance().loadPicture( *pic );
示例13: ReadPngStream
int ReadPngStream(FILE *file,
std::vector<unsigned char> * ptr,
int * w,
int * h,
int * depth) {
// first check the eight byte PNG signature
png_byte pbSig[8];
size_t readcnt = fread(pbSig, 1, 8, file);
(void) readcnt;
if (png_sig_cmp(pbSig, 0, 8))
{
return 0;
}
// create the two png(-info) structures
png_structp png_ptr = nullptr;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
(png_error_ptr)nullptr, (png_error_ptr)nullptr);
if (!png_ptr)
{
return 0;
}
png_infop info_ptr = nullptr;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, nullptr, nullptr);
return 0;
}
// initialize the png structure
png_init_io(png_ptr, file);
png_set_sig_bytes(png_ptr, 8);
// read all PNG info up to image data
png_read_info(png_ptr, info_ptr);
// get width, height, bit-depth and color-type
png_uint_32 wPNG, hPNG;
int iBitDepth;
int iColorType;
png_get_IHDR(png_ptr, info_ptr, &wPNG, &hPNG, &iBitDepth,
&iColorType, nullptr, nullptr, nullptr);
// expand images of all color-type to 8-bit
if (iColorType == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
if (iBitDepth < 8)
png_set_expand(png_ptr);
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);
if (iBitDepth == 16) // convert 16-bit to 8-bit on the fly
png_set_strip_16(png_ptr);
double dGamma;
// if required set gamma conversion
if (png_get_gAMA(png_ptr, info_ptr, &dGamma))
png_set_gamma(png_ptr, (double) 2.2, dGamma);
// after the transformations are registered, update info_ptr data
png_read_update_info(png_ptr, info_ptr);
// get again width, height and the new bit-depth and color-type
png_get_IHDR(png_ptr, info_ptr, &wPNG, &hPNG, &iBitDepth,
&iColorType, nullptr, nullptr, nullptr);
// Get number of byte along a tow
png_uint_32 ulRowBytes;
ulRowBytes = png_get_rowbytes(png_ptr, info_ptr);
// and allocate memory for an array of row-pointers
png_byte **ppbRowPointers = nullptr;
if ((ppbRowPointers = (png_bytepp) malloc(hPNG
* sizeof(png_bytep))) == nullptr)
{
std::cerr << "PNG: out of memory" << std::endl;
return 0;
}
*w = wPNG;
*h = hPNG;
*depth = png_get_channels(png_ptr, info_ptr);
// now we can allocate memory to store the image
ptr->resize((*h)*(*w)*(*depth));
// set the individual row-pointers to point at the correct offsets
for (png_uint_32 i = 0; i < hPNG; i++)
ppbRowPointers[i] = &((*ptr)[0]) + i * ulRowBytes;
// now we can go ahead and just read the whole image
png_read_image(png_ptr, ppbRowPointers);
// read the additional chunks in the PNG file (not really needed)
png_read_end(png_ptr, nullptr);
//.........这里部分代码省略.........
示例14: Warn
//.........这里部分代码省略.........
} else
alpha = false;
} else
alpha = false;
break;
case PNG_COLOR_TYPE_RGBA:
alpha = true;
case PNG_COLOR_TYPE_RGB:
imagetype = RGBImage;
if (!(color_type & PNG_COLOR_MASK_ALPHA)) {
alpha = false;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
png_color_16p trans_values;
png_get_tRNS(png_ptr, info_ptr, 0, 0, &trans_values);
hascolorkey = true;
keycolor.red = uint8((trans_values->red) & 0xff);
keycolor.green = uint8((trans_values->green) & 0xff);
keycolor.blue = uint8((trans_values->blue) & 0xff);
}
}
break;
}
// setup gamma
//@ TODO: add code to retrieve the gamma here
double screen_gamma = 0; //X_SharedPtr(iSystem)->getUserGamma();
// and comment this
screen_gamma = 2.2;
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);
}
/* todo Unstrip this */
if (bit_depth > 8) {
// tell libpng to strip 16 bit/color files down to 8 bits/color
png_set_strip_16(png_ptr);
bit_depth = 8;
} else if (bit_depth < 8)
// Expand pictures with less than 8bpp to 8bpp
png_set_packing(png_ptr);
// update
png_read_update_info(png_ptr, info_ptr);
png_uint_32 bytes_per_row = (png_uint_32)png_get_rowbytes(png_ptr, info_ptr);
//png_read_image
PixelFormat fmt;
uint32 bpp = 1;
// we dont support anything else
// other than RGBImage right now
// so!
if (imagetype == RGBImage) {
fmt = PixelFormat::RGBA8;
bpp = 4;
} else if (imagetype == GrayAlpha && bytes_per_row == 1)
示例15: readPng
void readPng(GImage* pImage, const unsigned char* pData, size_t nDataSize)
{
// Check for the PNG signature
if(nDataSize < 8 || png_sig_cmp((png_bytep)pData, 0, 8) != 0)
throw Ex("not a png file");
// Read all PNG data up until the image data chunk.
GPNGReader reader(pData);
png_set_read_fn(reader.m_pReadStruct, (png_voidp)&reader, (png_rw_ptr)readFunc);
png_read_info(reader.m_pReadStruct, reader.m_pInfoStruct);
// Get the image data
int depth, color;
png_uint_32 width, height;
png_get_IHDR(reader.m_pReadStruct, reader.m_pInfoStruct, &width, &height, &depth, &color, NULL, NULL, NULL);
if(depth != 8)
throw Ex("unexpected depth");
pImage->resize(width, height);
// Set gamma correction
double dGamma;
if (png_get_gAMA(reader.m_pReadStruct, reader.m_pInfoStruct, &dGamma))
png_set_gamma(reader.m_pReadStruct, 2.2, dGamma);
else
png_set_gamma(reader.m_pReadStruct, 2.2, 1.0 / 2.2); // 1.0 = viewing gamma, 2.2 = screen gamma
// Update the 'info' struct with the gamma information
png_read_update_info(reader.m_pReadStruct, reader.m_pInfoStruct);
// Tell it to expand palettes to full channels
png_set_expand(reader.m_pReadStruct);
png_set_gray_to_rgb(reader.m_pReadStruct);
// Allocate the row pointers
unsigned long rowbytes = png_get_rowbytes(reader.m_pReadStruct, reader.m_pInfoStruct);
unsigned long channels = rowbytes / width;
png_bytep pRawData = (png_bytep)new unsigned char[rowbytes * height];
unsigned int i;
{
png_bytep* pRows = (png_bytep*)new unsigned char[sizeof(png_bytep) * height];
for(i = 0; i < height; i++)
pRows[i] = pRawData + i * rowbytes;
png_read_image(reader.m_pReadStruct, pRows);
delete[] pRows;
}
// Copy to the GImage
unsigned long nPixels = width * height;
unsigned int* pRGBQuads = pImage->m_pPixels;
unsigned char *pBytes = pRawData;
if(channels > 3)
{
if(channels != 4)
throw Ex("Unexpected number of channels");
for(i = 0; i < nPixels; i++)
{
*pRGBQuads = gARGB(pBytes[3], pBytes[0], pBytes[1], pBytes[2]);
pBytes += channels;
pRGBQuads++;
}
}
else if(channels == 3)
{
for(i = 0; i < nPixels; i++)
{
*pRGBQuads = gARGB(0xff, pBytes[0], pBytes[1], pBytes[2]);
pBytes += channels;
pRGBQuads++;
}
}
else
{
delete[] pRawData;
throw Ex("Sorry, loading ", to_str(channels), "-channel png files is not yet supported");
}
delete[] pRawData;
// Check for additional tags
png_read_end(reader.m_pReadStruct, reader.m_pEndInfoStruct);
}