当前位置: 首页>>代码示例>>C++>>正文


C++ png_create_info_struct函数代码示例

本文整理汇总了C++中png_create_info_struct函数的典型用法代码示例。如果您正苦于以下问题:C++ png_create_info_struct函数的具体用法?C++ png_create_info_struct怎么用?C++ png_create_info_struct使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了png_create_info_struct函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: write_png_raw

/**
 * @brief internal function used to write a byte array as a PNG file
 *
 * The PNG file is written as a 8bit image file, interlaced,
 * truecolor. Depending on the number of channels, the color model is
 * gray, gray+alpha, rgb, rgb+alpha.
 *
 * @todo handle 16bit
 *
 * @param fname PNG file name, "-" means stdout
 * @param data deinterlaced (RRR..GGG..BBB..AAA) image byte array
 * @param nx, ny, nc number of columns, lines and channels
 * @param dtype identifier for the data type to be used for output
 * @return 0 if everything OK, -1 if an error occured
 */
static int write_png_raw(const char *fname, const void *data,
                         size_t nx, size_t ny, size_t nc, int dtype)
{
    png_structp png_ptr;
    png_infop info_ptr;
    png_byte *idata = NULL, *idata_ptr = NULL;
    png_bytep *row_pointers = NULL;
    png_byte bit_depth;
    /* volatile : because of setjmp/longjmp */
    FILE *volatile fp;
    const unsigned char *data_u8 = NULL;
    const unsigned char *data_u8_ptr = NULL;
    const float *data_f32 = NULL;
    const float *data_f32_ptr = NULL;
    float tmp;
    int color_type, interlace, compression, filter;
    size_t size;
    size_t i, j, k;

    /* parameters check */
    if (0 >= nx || 0 >= ny || 0 >= nc)
        return -1;
    if (NULL == fname || NULL == data)
        return -1;
    if (IO_PNG_U8 != dtype && IO_PNG_F32 != dtype)
        return -1;

    /* open the PNG output file */
    if (0 == strcmp(fname, "-"))
        fp = stdout;
    else if (NULL == (fp = fopen(fname, "wb")))
        return -1;

    /* allocate the interlaced array and row pointers */
    size = nx * ny * nc;
    if (NULL == (idata = (png_byte *) malloc(size * sizeof(png_byte))))
        return write_png_abort(fp, NULL, NULL, NULL, NULL);

    if (NULL == (row_pointers = (png_bytep *) malloc(ny * sizeof(png_bytep))))
        return write_png_abort(fp, idata, NULL, NULL, NULL);

    /*
     * create and initialize the png_struct
     * with the default stderr and error handling
     */
    if (NULL == (png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
                                                   NULL, NULL, NULL)))
        return write_png_abort(fp, idata, row_pointers, NULL, NULL);

    /* allocate/initialize the memory for image information */
    if (NULL == (info_ptr = png_create_info_struct(png_ptr)))
        return write_png_abort(fp, idata, row_pointers, &png_ptr, NULL);

    /* set error handling */
    if (0 != setjmp(png_jmpbuf(png_ptr)))
        /* if we get here, we had a problem reading the file */
        return write_png_abort(fp, idata, row_pointers, &png_ptr, &info_ptr);

    /* set up the input control using standard C streams */
    png_init_io(png_ptr, fp);

    /* set image informations */
    bit_depth = 8;
    switch (nc)
    {
    case 1:
        color_type = PNG_COLOR_TYPE_GRAY;
        break;
    case 2:
        color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
        break;
    case 3:
        color_type = PNG_COLOR_TYPE_RGB;
        break;
    case 4:
        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
        break;
    default:
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        free(row_pointers);
        free(idata);
        (void) fclose(fp);
        return -1;
    }
    interlace = PNG_INTERLACE_ADAM7;
//.........这里部分代码省略.........
开发者ID:tguillemot,项目名称:midway_equalization,代码行数:101,代码来源:io_png.c

示例2: save

void save(void)
{
	char file[MAX_PATH];
	strcpy(file,firstFile);

	char* p = strrchr(file,'\\');
	if (p == 0)
		p = file;
	else
		p++;
	char* x = strrchr(p,'.');
	if (x == 0)
		x = p+strlen(p);
	strcpy(x,".png");

	OPENFILENAME ofn;
	ZeroMemory(&ofn, sizeof ofn);
	ofn.lStructSize = sizeof ofn;
	ofn.hwndOwner = wnd;
	ofn.lpstrFilter = "PNG Image Files (*.png)\0*.png\0All Files (*.*)\0*.*\0";
	ofn.lpstrFile = file;
	ofn.nMaxFile = sizeof file;
	ofn.lpstrTitle = "Save as a PNG Image";
	ofn.Flags = OFN_ENABLEHOOK|OFN_ENABLESIZING|OFN_EXPLORER|OFN_HIDEREADONLY;
	ofn.lpfnHook = fileDialogProc;
	prepareOFN(&ofn);

	if (GetSaveFileName(&ofn))
	{
		FILE* f = fopen(file,"wb");
		if (f == 0)
			fatal("Could not open ouput PNG file");

		png_structp pngp = png_create_write_struct
			(PNG_LIBPNG_VER_STRING,(png_voidp)0,0,0);
		if (pngp == 0)
			fatal("Could not create PNG write structure");
		png_infop infop = png_create_info_struct(pngp);
		if (infop == 0)
			fatal("Could not create PNG info structure");

		if (setjmp(png_jmpbuf(pngp)))
			fatal("Could not write PNG file");

		png_init_io(pngp,f);
		png_set_IHDR(pngp,infop,width,height,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);
		png_set_bgr(pngp);

		png_bytep* rows = (png_bytep*)malloc(sizeof(png_bytep)*height);
		for (int y = 0; y < height; y++)
			rows[y] = bits+(3*y*width);
		png_set_rows(pngp,infop,rows);

		png_write_png(pngp,infop,PNG_TRANSFORM_IDENTITY,0);

		free(rows);
		png_destroy_write_struct(&pngp,&infop);
		fclose(f);
	}
}
开发者ID:DavidKinder,项目名称:DBWRender,代码行数:61,代码来源:display.cpp

示例3: savePNGto

static int savePNGto(FILE *fp, gPixmap *pixmap)
{
	gUnmanagedSurface *surface = pixmap->surface;
	if (!surface)
		return -2;

	png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
	if (!png_ptr)
	{
		eDebug("[ePNG] couldn't allocate write struct");
		return -2;
	}
	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr)
	{
		eDebug("[ePNG] failed to allocate info struct");
		png_destroy_write_struct(&png_ptr, 0);
		return -3;
	}

	png_set_IHDR(png_ptr, info_ptr, surface->x, surface->y, surface->bpp/surface->bypp,
		PNG_COLOR_TYPE_RGB_ALPHA,
		PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

	if (setjmp(png_jmpbuf(png_ptr)))
	{
		eDebug("[ePNG] png setjump failed or activated");
		png_destroy_write_struct(&png_ptr, &info_ptr);
		return -4;
	}
	png_init_io(png_ptr, fp);
	png_set_filter(png_ptr, 0, PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_PAETH);
	png_set_compression_level(png_ptr, Z_BEST_COMPRESSION);

	png_write_info(png_ptr, info_ptr);
	png_set_packing(png_ptr);

	png_byte *row_pointer;
	png_byte *cr = new png_byte[surface->y * surface->stride];
	if (cr == NULL)
	{
		eDebug("[ePNG] failed to allocate memory image");
		return -5;
	}
	for (int i = 0; i < surface->y; ++i)
	{
		row_pointer = ((png_byte*)surface->data) + i * surface->stride;
		if (surface->bypp == 4)
		{
			memcpy(cr, row_pointer, surface->stride);
			for (int j = 0; j < surface->stride; j += 4)
			{
				unsigned char tmp = cr[j];
				cr[j] = cr[j+2];
				cr[j+2] = tmp;
			}
			png_write_row(png_ptr, cr);
		}
		else
			png_write_row(png_ptr, row_pointer);
	}
	delete [] cr;

	png_write_end(png_ptr, info_ptr);
	png_destroy_write_struct(&png_ptr, &info_ptr);
	return 0;
}
开发者ID:Akki01,项目名称:enigma2,代码行数:67,代码来源:epng.cpp

示例4: ReadPNG

/* Read a PNG image file from "fname", using routines from libpng.
 * Returns 0 on success, or negative on various failures:
 *   -2: File is not PNG, or file is corrupt
 *   -3: Can't malloc a big enough image buffer
 *   -4: PNG image data is neither grayscale nor RGB (unsupported format)
 */
int ReadPNG(const char *fname, unsigned char (**data)[3], int *w, int *h)
{
    FILE *fp = fopen(fname, "rb");
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_infop end_info = NULL;
    int transforms = PNG_TRANSFORM_STRIP_16
                   | PNG_TRANSFORM_STRIP_ALPHA
                   | PNG_TRANSFORM_PACKING
                   | PNG_TRANSFORM_PACKSWAP
                   | PNG_TRANSFORM_EXPAND
                   | PNG_TRANSFORM_SHIFT;
    int channels;
    int bytes;
    png_bytepp row_pointers = NULL;
    int rc = 0;

    if (fp == NULL)
      return -1;

    (*data) = NULL;

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
    if (png_ptr == NULL) goto err_nomem;

    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) goto err_nomem;

    end_info = png_create_info_struct(png_ptr);
    if (end_info == NULL) goto err_nomem;

    if (setjmp(png_jmpbuf(png_ptr))) goto err_corrupt;
    png_init_io(png_ptr, fp);
    png_read_png(png_ptr, info_ptr, transforms, NULL);

    *w = png_get_image_width(png_ptr, info_ptr);
    *h = png_get_image_height(png_ptr, info_ptr);
    channels = png_get_channels(png_ptr, info_ptr);
    bytes = png_get_rowbytes(png_ptr, info_ptr);

    if (bytes != channels*(*w))
      goto err_unsupported;

    row_pointers = png_get_rows(png_ptr, info_ptr);
    if (row_pointers == NULL) goto err_nomem;

    (*data) = malloc(*w * *h * sizeof **data);
    if (*data == NULL) goto err_nomem;

    if (channels == 1) {
        int j;
        for (j=0; j < *h; ++j) {
            int i;
            unsigned char (*imrow)[3] = (*data) + j*(*w);
            for (i=0; i < *w; ++i)
              memset(imrow[i], row_pointers[j][i], 3);
        }
    }
    else if (channels == 3) {
        int j;
        for (j=0; j < *h; ++j) {
            unsigned char (*imrow)[3] = (*data) + j*(*w);
            memcpy(imrow, row_pointers[j], 3*(*w));
        }
    }
    else goto err_unsupported;

go_return:
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
    fclose(fp);
    return rc;

err_corrupt:
    rc = -2;
    free(data);
    goto go_return;
err_nomem:
    rc = -3;
    free(data);
    goto go_return;
err_unsupported:
    rc = -4;
    free(data);
    goto go_return;
}
开发者ID:Gnommy,项目名称:JugglersDrift,代码行数:91,代码来源:readpng.c

示例5: fopen

/**
* From: http://stackoverflow.com/a/11297197
*/
GLuint gl4::TextureManager::_loadTextureFromPNG(const char * file_name, int * width, int * height)
{
    png_byte header[8];

    FILE *fp = fopen(file_name, "rb");
    if (fp == 0)
    {
        perror(file_name);
        return 0;
    }

    // read the header
    fread(header, 1, 8, fp);

    if (png_sig_cmp(header, 0, 8))
    {
        fprintf(stderr, "error: %s is not a PNG.\n", file_name);
        fclose(fp);
        return 0;
    }

    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr)
    {
        fprintf(stderr, "error: png_create_read_struct returned 0.\n");
        fclose(fp);
        return 0;
    }

    // create png info struct
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        fprintf(stderr, "error: png_create_info_struct returned 0.\n");
        png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
        fclose(fp);
        return 0;
    }

    // create png info struct
    png_infop end_info = png_create_info_struct(png_ptr);
    if (!end_info)
    {
        fprintf(stderr, "error: png_create_info_struct returned 0.\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
        fclose(fp);
        return 0;
    }

    // the code in this if statement gets called if libpng encounters an error
    if (setjmp(png_jmpbuf(png_ptr))) {
        fprintf(stderr, "error from libpng\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(fp);
        return 0;
    }

    // init png reading
    png_init_io(png_ptr, fp);

    // let libpng know you already read the first 8 bytes
    png_set_sig_bytes(png_ptr, 8);

    // read all the info up to the image data
    png_read_info(png_ptr, info_ptr);

    // variables to pass to get info
    int bit_depth, color_type;
    png_uint_32 temp_width, temp_height;

    // get info about png
    png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type,
        NULL, NULL, NULL);

    if (width != 0){ *width = temp_width; }
    if (height != 0){ *height = temp_height; }

    // Update the png info struct.
    png_read_update_info(png_ptr, info_ptr);

    // Row size in bytes.
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

    // glTexImage2d requires rows to be 4-byte aligned
    rowbytes += 3 - ((rowbytes-1) % 4);

    // Allocate the image_data as a big block, to be given to opengl
    png_byte * image_data;
    image_data = (png_byte*)std::malloc(rowbytes * temp_height * sizeof(png_byte)+15);
    if (image_data == NULL)
    {
        fprintf(stderr, "error: could not allocate memory for PNG image data\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
        fclose(fp);
        return 0;
    }

//.........这里部分代码省略.........
开发者ID:jonasstrandstedt,项目名称:infvisGL,代码行数:101,代码来源:TextureManager.cpp

示例6: png_cache_convert

/** PNG content to bitmap conversion.
 *
 * This routine generates a bitmap object from a PNG image content
 */
static struct bitmap *
png_cache_convert(struct content *c)
{
	png_structp png_ptr;
	png_infop info_ptr;
	png_infop end_info_ptr;
	volatile struct bitmap *bitmap = NULL;
	struct png_cache_read_data_s png_cache_read_data;
	png_uint_32 width, height;
	volatile png_bytep *row_pointers = NULL;

	png_cache_read_data.data = 
		content__get_source_data(c, &png_cache_read_data.size);

	if ((png_cache_read_data.data == NULL) || 
	    (png_cache_read_data.size <= 8)) {
		return NULL;
	}

	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
			nspng_error, nspng_warning);
	if (png_ptr == NULL) {
		return NULL;
	}

	info_ptr = png_create_info_struct(png_ptr);
	if (info_ptr == NULL) {
		png_destroy_read_struct(&png_ptr, NULL, NULL);
		return NULL;
	}

	end_info_ptr = png_create_info_struct(png_ptr);
	if (end_info_ptr == NULL) {
		png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
		return NULL;
	}

	/* setup error exit path */
	if (setjmp(png_jmpbuf(png_ptr))) {
		/* cleanup and bail */
		goto png_cache_convert_error;
	}

	/* read from a buffer instead of stdio */
	png_set_read_fn(png_ptr, &png_cache_read_data, png_cache_read_fn);

	/* ensure the png info structure is populated */
	png_read_info(png_ptr, info_ptr);

	/* setup output transforms */
	nspng_setup_transforms(png_ptr, info_ptr);

	width = png_get_image_width(png_ptr, info_ptr);
	height = png_get_image_height(png_ptr, info_ptr);

	/* Claim the required memory for the converted PNG */
	bitmap = bitmap_create(width, height, BITMAP_NEW);
	if (bitmap == NULL) {
		/* cleanup and bail */
		goto png_cache_convert_error;
	}

	row_pointers = calc_row_pointers((struct bitmap *) bitmap);

	if (row_pointers != NULL) {
		png_read_image(png_ptr, (png_bytep *) row_pointers);
	}

png_cache_convert_error:

	/* cleanup png read */
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info_ptr);

	free((png_bytep *) row_pointers);

	if (bitmap != NULL)
		bitmap_modified((struct bitmap *)bitmap);

	return (struct bitmap *)bitmap;
}
开发者ID:bkeepers,项目名称:cheribsd,代码行数:84,代码来源:png.c

示例7: ALIGN_SIZE

VBuf *BmpHandleToPngByte(HBITMAP hBmp)
{
	BITMAP		bmp;
	BITMAPINFO	*bmi = NULL;
	int			palette, total_size, header_size, data_size, line_size;
	HWND		hWnd = ::GetDesktopWindow();
	HDC			hDc = NULL;
	VBuf		bmpVbuf;
	png_struct	*png  = NULL;
	png_info	*info = NULL;
	png_color_8	sbit;
	png_byte	**lines = NULL;
	VBuf		*vbuf = NULL, *ret = NULL;

	if (!::GetObject(hBmp, sizeof(bmp), &bmp)) return NULL;

	//if (bmp.bmBitsPixel < 24)
	bmp.bmBitsPixel = 24;

	line_size   = bmp.bmWidth * ALIGN_SIZE(bmp.bmBitsPixel, 8) / 8;
	line_size   = ALIGN_SIZE(line_size, 4);
	data_size   = line_size * bmp.bmHeight;
	palette     = bmp.bmBitsPixel <= 8 ? 1 << bmp.bmBitsPixel : 0;
	header_size = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * palette;
	total_size	= header_size + data_size;

	if (!bmpVbuf.AllocBuf(total_size)) return	NULL;
	bmi = (BITMAPINFO *)bmpVbuf.Buf();

	bmi->bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
	bmi->bmiHeader.biWidth        = bmp.bmWidth;
	bmi->bmiHeader.biHeight       = bmp.bmHeight;
	bmi->bmiHeader.biPlanes       = 1;
	bmi->bmiHeader.biBitCount     = bmp.bmBitsPixel;
	bmi->bmiHeader.biCompression  = BI_RGB;
	bmi->bmiHeader.biClrUsed      = palette;
	bmi->bmiHeader.biClrImportant = palette;

	if (!(hDc = ::GetDC(hWnd)) ||
		!::GetDIBits(hDc, hBmp, 0, bmp.bmHeight, (char *)bmi + header_size, bmi, DIB_RGB_COLORS)) {
		goto END;
	}

	if (!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) return NULL;
	if (!(info = png_create_info_struct(png))) goto END;
	if (!(vbuf = new VBuf(0, total_size))) goto END;

	png_set_write_fn(png, (void *)vbuf, (png_rw_ptr)png_vbuf_wfunc,
					(png_flush_ptr)png_vbuf_wflush);

	if (palette) {
		png_color	png_palette[256];
		for (int i=0; i < palette; i++) {
			png_palette[i].red		= bmi->bmiColors[i].rgbRed;
			png_palette[i].green	= bmi->bmiColors[i].rgbGreen;
			png_palette[i].blue		= bmi->bmiColors[i].rgbBlue;
		}
		png_set_IHDR(png, info, bmp.bmWidth, bmp.bmHeight, bmp.bmBitsPixel,
					PNG_COLOR_TYPE_PALETTE,
					PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
		png_set_PLTE(png, info, png_palette, palette);
	}
	else {
		png_set_IHDR(png, info, bmp.bmWidth, bmp.bmHeight, 8,
					bmp.bmBitsPixel > 24 ? PNG_COLOR_TYPE_RGB_ALPHA  : PNG_COLOR_TYPE_RGB,
					PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
	}
	sbit.red = sbit.green = sbit.blue = 8;
	sbit.alpha = bmp.bmBitsPixel > 24 ? 8 : 0;
	png_set_sBIT(png, info, &sbit);

	if (setjmp(png_jmpbuf(png))) {
		goto END;
	}
	else {
		png_write_info(png, info);
		png_set_bgr(png);

		lines = (png_byte **)malloc(sizeof(png_bytep *) * bmp.bmHeight);

		for (int i = 0; i < bmp.bmHeight; i++) {
			lines[i] = bmpVbuf.Buf() + header_size + line_size * (bmp.bmHeight - i - 1);
		}
		png_write_image(png, lines);
		png_write_end(png, info);
		ret = vbuf;
	}

END:
	if (png) png_destroy_write_struct(&png, &info);
	if (hDc) ::ReleaseDC(hWnd, hDc);
	if (lines) free(lines);
	if (!ret && vbuf) delete vbuf;
	return	ret;
}
开发者ID:jigar3001,项目名称:ipmsg_dc,代码行数:95,代码来源:image.cpp

示例8: LoadPNG

static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 id, volatile bool mask)
{
	png_byte header[8];
	png_structp png_ptr;
	png_infop info_ptr, end_info;
	uint bit_depth, colour_type;
	uint i, pixelsize;
	SpriteLoader::CommonPixel *dst;

	if (!OpenPNGFile(filename, id, mask)) return mask; // If mask is true, and file not found, continue true anyway, as it isn't a show-stopper

	/* Check the header */
	FioReadBlock(header, 8);
	if (png_sig_cmp(header, 0, 8) != 0) return false;

	/* Create the reader */
	png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, png_my_error, png_my_warning);
	if (png_ptr == NULL) return false;

	/* Create initial stuff */
	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 false;
	}
	end_info = png_create_info_struct(png_ptr);
	if (end_info == NULL) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
		return false;
	}

	/* Make sure that upon error, we can clean up graceful */
	if (setjmp(png_jmpbuf(png_ptr))) {
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		return false;
	}

	/* Read the file */
	png_set_read_fn(png_ptr, NULL, png_my_read);
	png_set_sig_bytes(png_ptr, 8);

	png_read_info(png_ptr, info_ptr);

	if (!mask) {
		/* Read the text chunks */
		png_textp text_ptr;
		int num_text = 0;
		png_get_text(png_ptr, info_ptr, &text_ptr, &num_text);
		if (num_text == 0) DEBUG(misc, 0, "Warning: PNG Sprite '%s/%d.png' doesn't have x_offs and y_offs; expect graphical problems", filename, id);
		for (int i = 0; i < num_text; i++) {
			/* x_offs and y_offs are in the text-chunk of PNG */
			if (strcmp("x_offs", text_ptr[i].key) == 0) sprite->x_offs = strtol(text_ptr[i].text, NULL, 0);
			if (strcmp("y_offs", text_ptr[i].key) == 0) sprite->y_offs = strtol(text_ptr[i].text, NULL, 0);
		}

		sprite->height = png_get_image_height(png_ptr, info_ptr);
		sprite->width  = png_get_image_width(png_ptr, info_ptr);
		sprite->AllocateData(sprite->width * sprite->height);
	}

	bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
	colour_type = png_get_color_type(png_ptr, info_ptr);

	if (mask && (bit_depth != 8 || colour_type != PNG_COLOR_TYPE_PALETTE)) {
		DEBUG(misc, 0, "Ignoring mask for SpriteID %d as it isn't a 8 bit palette image", id);
		return true;
	}

	if (!mask) {
		if (bit_depth == 16) png_set_strip_16(png_ptr);

		if (colour_type == PNG_COLOR_TYPE_PALETTE) {
			png_set_palette_to_rgb(png_ptr);
			colour_type = PNG_COLOR_TYPE_RGB;
		}
		if (colour_type == PNG_COLOR_TYPE_GRAY || colour_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
			png_set_gray_to_rgb(png_ptr);
			colour_type = PNG_COLOR_TYPE_RGB;
		}

		if (colour_type == PNG_COLOR_TYPE_RGB) {
			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
		}

		pixelsize = sizeof(uint32);
	} else {
		pixelsize = sizeof(uint8);
	}

	png_bytep row_pointer = AllocaM(png_byte, png_get_image_width(png_ptr, info_ptr) * pixelsize);

	for (i = 0; i < png_get_image_height(png_ptr, info_ptr); i++) {
		png_read_row(png_ptr, row_pointer, NULL);

		dst = sprite->data + i * png_get_image_width(png_ptr, info_ptr);

		for (uint x = 0; x < png_get_image_width(png_ptr, info_ptr); x++) {
			if (mask) {
				if (row_pointer[x * sizeof(uint8)] != 0) {
					dst[x].r = 0;
//.........这里部分代码省略.........
开发者ID:andrew889,项目名称:OpenTTD,代码行数:101,代码来源:png.cpp

示例9: RETURN_ERROR

Image* Image::LoadPNG( IFileReader* file ) {
	png_byte header[8];
	if ( file->ReadByte( header, sizeof(header) ) == false ) {
		RETURN_ERROR( "failed to read png header from %s", file->GetFileName( ) );
	}

	if ( png_sig_cmp(header, 0, 8) != 0 ) {
		RETURN_ERROR( "%s is not png format!", file->GetFileName( ) );
	}

	png_structp png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL );
	if ( png_ptr == NULL ) {
		RETURN_ERROR( "png_create_read_struct returned 0" );
	}

	// create png info struct
	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_ERROR( "png_create_info_struct returned 0" );
	}

	// create png info struct
	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_ERROR( "error: png_create_info_struct returned 0" );
	}

	// init png reading
	png_set_read_fn( png_ptr, file, png_read_callback );

	// the code in this if statement gets called if libpng encounters an error
	if ( setjmp( png_jmpbuf( png_ptr ) ) ) {
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		RETURN_ERROR( "error from libpng" );
	}

	// let libpng know you already read the first 8 bytes
	png_set_sig_bytes( png_ptr, 8 );

	// read all the info up to the image data
	png_read_info( png_ptr, info_ptr );

	// variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 width, height;

	// get info about png
	png_get_IHDR( png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL );

	bool hasAlpha = false;
	if ( png_get_valid( png_ptr, info_ptr, PNG_INFO_tRNS ) ) {
		png_set_tRNS_to_alpha( png_ptr );
		hasAlpha = true;
	}
	// Expands PNG with less than 8bits per channel to 8bits.
	if ( bit_depth < 8 ) {
		png_set_packing ( png_ptr );
	// Shrinks PNG with 16bits per color channel down to 8bits.
	}
	else if ( bit_depth == 16 ) {
		png_set_strip_16( png_ptr );
	}
	// Indicates that image needs conversion to RGBA if needed.
	GLint format;
	switch ( color_type ) {
	case PNG_COLOR_TYPE_PALETTE:
		png_set_palette_to_rgb( png_ptr );
		format = hasAlpha ? GL_RGBA : GL_RGB;
		break;
	case PNG_COLOR_TYPE_RGB:
		format = hasAlpha ? GL_RGBA : GL_RGB;
		break;
	case PNG_COLOR_TYPE_RGBA:
		format = GL_RGBA;
		break;
	case PNG_COLOR_TYPE_GRAY:
		png_set_expand_gray_1_2_4_to_8( png_ptr );
		format = hasAlpha ? GL_LUMINANCE_ALPHA:GL_LUMINANCE;
		break;
	case PNG_COLOR_TYPE_GA:
		png_set_expand_gray_1_2_4_to_8( png_ptr );
		format = GL_LUMINANCE_ALPHA;
		break;
    default:
        format = 0;
        break;
	}

	// Update the png info struct.
	png_read_update_info( png_ptr, info_ptr );

	// Row size in bytes.
	int rowbytes = (int)png_get_rowbytes( png_ptr, info_ptr );

	// glTexImage2d requires rows to be 4-byte aligned
	rowbytes += 3 - ((rowbytes-1) % 4);

//.........这里部分代码省略.........
开发者ID:dcracker,项目名称:daphihae-new,代码行数:101,代码来源:Image.cpp

示例10: read_png

void
read_png(unsigned char **block, png_uint_32 *width, png_uint_32 *height, FILE *file)
{
    png_structp png_ptr;
    png_infop   info_ptr;
    png_bytep *row_pointers;
    unsigned row, x, y;
    int rowbytes;
    char *dst;

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
    if (png_ptr == NULL) {
	printf("\nERROR: read_png: Could not create read struct.\n");
	exit(1);
    }

    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) {
	printf("\nERROR: read_png: Could not create info struct.\n");
	png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
	exit(1);
    }


    if (setjmp(png_ptr->jmpbuf)) {

	printf("\nERROR: read_png: fatal error.\n");
        png_destroy_read_struct(&png_ptr, &info_ptr, (png_info **)0);
        /* free pointers before returning, if necessary */
        free(png_ptr);
        free(info_ptr);

	exit(1);
    }

    /* Set up the input control if you are using standard C streams */
    png_init_io(png_ptr, file);


    /* The call to png_read_info() gives us all of the information from the
     * PNG file before the first IDAT (image data chunk).  REQUIRED
     */
    png_read_info(png_ptr, info_ptr);

    png_get_IHDR(png_ptr, info_ptr, width, height, &bit_depth, &color_type, &interlace_type, NULL, NULL);

    // printf("read_png: width=%d, height=%d, bit_depth=%d\n", width, height, bit_depth);
    // printf("read_png: color_type=%d, interlace_type=%d\n", color_type, interlace_type);
    
    // strip alpha information
    if (color_type & PNG_COLOR_MASK_ALPHA) {
        png_set_strip_alpha(png_ptr);
    }

//    /* tell libpng to strip 16 bit/color files down to 8 bits/color */
//    png_set_strip_16(png_ptr);

    /* 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);

    /* Expand paletted colors into true RGB triplets */
    png_set_expand(png_ptr);

    png_start_read_image(png_ptr);

    /* The easiest way to read the image: */

    rowbytes = png_get_rowbytes(png_ptr, info_ptr) * 3;
    row_pointers = (png_bytep *)malloc((*height) * sizeof(*row_pointers));

    row_pointers[0] = (png_bytep)malloc(rowbytes * (*height) * 2);

    for(row = 1; row < (*height); row++) {
	row_pointers[row] = row_pointers[row - 1] + rowbytes*2;
    }
    /* Read the entire image in one go */
    png_read_image(png_ptr, row_pointers);

    // we use fixed height here because block is of limited, fixed size
    // not fixed any more

    *block = realloc(*block, *height * *width * 3);

    // *block = malloc(*height * *width * 6);

    dst = *block;
    for(y = 0; y < *height; y++) {
	for(x = 0; x < *width * 3; x++) {
	  *dst++ = row_pointers[y][x];

	  // *dst++ = 0;
	}
    }
  
    free(row_pointers[0]);
    free(row_pointers);

    /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
//.........这里部分代码省略.........
开发者ID:tbentropy,项目名称:tilecutter,代码行数:101,代码来源:rdwr_png.c

示例11: open_png

static int
open_png(const char *name, png_structp *png_ptr, png_infop *info_ptr,
	 FILE **fp, png_uint_32 *width, png_uint_32 *height,
	 png_byte *channels)
{
	char resPath[256];
	unsigned char header[8];
	int color_type, bit_depth, result = 0;
	size_t bytesRead;

	snprintf(resPath, sizeof(resPath) - 1, "/res/images/%s.png", name);
	resPath[sizeof(resPath)-1] = '\0';
	*fp = fopen(resPath, "rb");
	if (*fp == NULL) {
		result = -1;
		goto exit;
	}

	bytesRead = fread(header, 1, sizeof(header), *fp);
	if (bytesRead != sizeof(header)) {
		result = -2;
		goto exit;
	}

	if (png_sig_cmp(header, 0, sizeof(header))) {
		result = -3;
		goto exit;
	}

	*png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL,
					  NULL);
	if (!*png_ptr) {
		result = -4;
		goto exit;
	}

	*info_ptr = png_create_info_struct(*png_ptr);
	if (!*info_ptr) {
		result = -5;
		goto exit;
	}

	if (setjmp(png_jmpbuf(*png_ptr))) {
		result = -6;
		goto exit;
	}

	png_init_io(*png_ptr, *fp);
	png_set_sig_bytes(*png_ptr, sizeof(header));
	png_read_info(*png_ptr, *info_ptr);

	png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
		     &color_type, NULL, NULL, NULL);

	*channels = png_get_channels(*png_ptr, *info_ptr);

	if (bit_depth == 8 && *channels == 3 &&
	    color_type == PNG_COLOR_TYPE_RGB) {
		/* 8-bit RGB images: great, nothing to do. */
	} else if (bit_depth <= 8 && *channels == 1 &&
		   color_type == PNG_COLOR_TYPE_GRAY) {
		/* 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. */
		png_set_expand_gray_1_2_4_to_8(*png_ptr);
	} else if (bit_depth <= 8 && *channels == 1 &&
		   color_type == PNG_COLOR_TYPE_PALETTE) {
		/* paletted images: expand to 8-bit RGB. Note that we DON'T
		 * currently expand the tRNS chunk (if any) to an alpha
		 * channel, because minui doesn't support alpha channels
		 * in general. */
		png_set_palette_to_rgb(*png_ptr);
		*channels = 3;
	} else {
		fprintf(stderr,
			"minui doesn't support PNG depth %d channels %d "
			"color_type %d\n",
			bit_depth, *channels, color_type);
		result = -7;
		goto exit;
	}

	return result;

exit:
	if (result < 0)
		png_destroy_read_struct(png_ptr, info_ptr, NULL);

	if (*fp != NULL) {
		fclose(*fp);
		*fp = NULL;
	}

	return result;
}
开发者ID:pk-codebox-evo,项目名称:os-sailfishos-yamui,代码行数:93,代码来源:resources.c

示例12: readPNGFile

    /*! \brief Reads a PNG file and places the pixel data in the
     * passed array.
     *
     * \param filename The name of the file to read.
     * \param image The array of \ref Pixel data to read the image into.
     * \param width Variable used to return the width of the image.
     * \param height Variable used to return the height of the image.
     */
    inline void readPNGFile(const std::string& filename,
			    std::vector<uint8_t>& image, 
			    size_t& width, size_t& height,
			    size_t& components) 
    {

      std::ifstream pngFile(filename.c_str(), std::fstream::binary);

      if(!pngFile.is_open()) {
	std::stringstream strm;
	strm << "failed to open file '" << filename << "'";
	throw std::runtime_error(strm.str().c_str());
      }
      
      pngFile.exceptions(std::fstream::badbit | std::fstream::failbit);
      
      const size_t pngHeaderSize = 8;
      png_byte pngHeader[pngHeaderSize];

      pngFile.read(reinterpret_cast<char*>(pngHeader), pngHeaderSize);

      if(png_sig_cmp(pngHeader, 0, pngHeaderSize)) {
	std::stringstream strm;
	strm << "failed to read '" << filename << "': not a png file";
	throw std::runtime_error(strm.str().c_str());
      }

      png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
					       NULL, NULL, NULL);

      if(!png)
	throw std::runtime_error("failed to allocate png_struct");

      png_infop pngInfo = png_create_info_struct(png);

      if(!pngInfo) {
	png_destroy_read_struct(&png, NULL, NULL);
	throw std::runtime_error("failed to allocate png_info_struct");
      }

      png_infop pngEndInfo = png_create_info_struct(png);

      if(!pngEndInfo) {
	png_destroy_read_struct(&png, &pngInfo, NULL);
	throw std::runtime_error("failed to allocate png_info_struct");
      }

      // set up error handling the hard way
      if(setjmp(png_jmpbuf(png))) {
	png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
	throw std::runtime_error("libpng: failed to set up io");
      }

      png_set_read_fn(png, static_cast<void*>(&pngFile),
		      detail::read);

      png_set_sig_bytes(png, pngHeaderSize);

      png_read_info(png, pngInfo);

      if(png_get_color_type(png, pngInfo) != PNG_COLOR_TYPE_RGBA &&
	 png_get_color_type(png, pngInfo) != PNG_COLOR_TYPE_RGB) {

	png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);

	std::stringstream strm;
	strm << "unsupported color type in '" << filename << "'";
	throw std::runtime_error(strm.str().c_str());
      }

      width = png_get_image_width(png, pngInfo);
      height = png_get_image_height(png, pngInfo);
      components = png_get_channels(png, pngInfo);

      if ((components != 3) && (components != 4))
	throw std::runtime_error("Unsupported number of components");
      size_t bitDepth = png_get_bit_depth(png, pngInfo);


      if(bitDepth != 8) {
	png_destroy_read_struct(&png, &pngInfo, &pngEndInfo);
	
	std::stringstream strm;
	strm << "failed to read '" << filename << "': invalid bit " <<
	  "depth: " << bitDepth;
	throw std::runtime_error(strm.str().c_str());
      }

      size_t bytesPerRow = png_get_rowbytes(png, pngInfo);
      png_bytep* pngRows = new png_bytep[height];
      png_bytep pngData = 0;

//.........这里部分代码省略.........
开发者ID:BigMacchia,项目名称:DynamO,代码行数:101,代码来源:PNG.hpp

示例13: writePNGFile

    /*! \brief Writes a PNG file using the pixel data in the passed
     * array.
     *
     * \param filename The name of the file to create/overwrite.
     * \param image The array of \ref Pixel data to write out.
     * \param width The width of the image.
     * \param height The height of the image.
     * \param compressionLevel The level of compression requested from the PNG library.
     * \param disableFiltering Prevent the PNG library from filtering the output.
     * \param flip Flips the vertical ordering of the image (to allow easy saving of OpenGL renderings).
     */
    inline void writePNGFile(const std::string& filename,
			     std::vector<uint8_t >& image, 
			     size_t width, size_t height,
			     size_t components,
			     int compressionLevel = PNG_COMPRESSION_TYPE_DEFAULT,
			     bool disableFiltering = false, bool flip = false) {

      if(image.size() != width * height * components) 
	{
	  std::stringstream strm;
	  strm << "invalid input to writePNGFile(): " <<
	    "size mismatch of input vector (is " << image.size() <<
	    ", should be " << width << "x" << height << " = " <<
	    width * height;
	  throw std::runtime_error(strm.str().c_str());
	}

      if(compressionLevel < 0 || compressionLevel > 9) {
	std::stringstream strm;
	strm << "invalid input to writePNGFile(): " <<
	  "valid compression levels range from 0 to 9 (default: " <<
	  PNG_COMPRESSION_TYPE_DEFAULT << ")";
	throw std::runtime_error(strm.str().c_str());
      }

      std::ofstream pngFile(filename.c_str(), std::fstream::binary);

      if(!pngFile.is_open()) 
	{
	  std::stringstream strm;
	  strm << "failed to open file '" << filename << "'";
	  throw std::runtime_error(strm.str().c_str());
	}

      pngFile.exceptions(std::fstream::badbit | std::fstream::failbit);

      png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
						NULL, NULL, NULL);

      if(!png)
	throw std::runtime_error("failed to allocate png_struct");

      png_infop pngInfo = png_create_info_struct(png);

      if(!pngInfo) {
	png_destroy_write_struct(&png, NULL);
	throw std::runtime_error("failed to allocate png_info_struct");
      }

      // set up error handling the hard way
      if(setjmp(png_jmpbuf(png))) {
	png_destroy_write_struct(&png, &pngInfo);
	throw std::runtime_error("libpng: failed to set up io");
      }

      png_set_write_fn(png, static_cast<void*>(&pngFile),
		       detail::write, detail::flush);

      switch (components)
	{
	case 3:
	  png_set_IHDR(png, pngInfo, width, height, 8, PNG_COLOR_TYPE_RGB,
		       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
		       PNG_FILTER_TYPE_BASE);
	  break;
	case 4:
	  png_set_IHDR(png, pngInfo, width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
		       PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
		       PNG_FILTER_TYPE_BASE);
	  break;
	default:
	  throw std::runtime_error("Unsupported number of components");
	}

      if(disableFiltering)
	png_set_filter(png, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);

      if(compressionLevel != PNG_COMPRESSION_TYPE_DEFAULT)
	png_set_compression_level(png, compressionLevel);

      png_write_info(png, pngInfo);

      size_t bytesPerRow = png_get_rowbytes(png, pngInfo);
      png_bytep* pngRows = new png_bytep[height];

      if(image.size() != (height * bytesPerRow)) 
	{
	  std::stringstream strm;
	  strm << "writePNGFile(): invalid size of input data";
//.........这里部分代码省略.........
开发者ID:BigMacchia,项目名称:DynamO,代码行数:101,代码来源:PNG.hpp

示例14: if

/**
 * @brief internal function used to read a PNG file into an array
 *
 * @todo don't loose 16bit info
 *
 * @param fname PNG file name, "-" means stdin
 * @param nx, ny, nc pointers to variables to be filled
 *        with the number of columns, lines and channels of the image
 * @param transform a PNG_TRANSFORM to be added to the default read transforms
 * @param dtype identifier for the data type to be used for output
 * @return pointer to an allocated array of pixels,
 *         or NULL if an error happens
 */
static void *read_png_raw(const char *fname,
                          size_t * nx, size_t * ny, size_t * nc,
                          int transform, int dtype)
{
    png_byte png_sig[PNG_SIG_LEN];
    png_structp png_ptr;
    png_infop info_ptr;
    png_bytepp row_pointers;
    png_bytep row_ptr;
    /* volatile : because of setjmp/longjmp */
    FILE *volatile fp = NULL;
    void *data = NULL;
    unsigned char *data_u8 = NULL;
    unsigned char *data_u8_ptr = NULL;
    float *data_f32 = NULL;
    float *data_f32_ptr = NULL;
    size_t size;
    size_t i, j, k;

    /* parameters check */
    if (NULL == fname || NULL == nx || NULL == ny || NULL == nc)
        return NULL;
    if (IO_PNG_U8 != dtype && IO_PNG_F32 != dtype)
        return NULL;

    /* open the PNG input file */
    if (0 == strcmp(fname, "-"))
        fp = stdin;
    else if (NULL == (fp = fopen(fname, "rb")))
        return NULL;

    /* read in some of the signature bytes and check this signature */
    if ((PNG_SIG_LEN != fread(png_sig, 1, PNG_SIG_LEN, fp))
        || 0 != png_sig_cmp(png_sig, (png_size_t) 0, PNG_SIG_LEN))
        return read_png_abort(fp, NULL, NULL);

    /*
     * create and initialize the png_struct
     * with the default stderr and error handling
     */
    if (NULL == (png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
                                                  NULL, NULL, NULL)))
        return read_png_abort(fp, NULL, NULL);

    /* allocate/initialize the memory for image information */
    if (NULL == (info_ptr = png_create_info_struct(png_ptr)))
        return read_png_abort(fp, &png_ptr, NULL);

    /* set error handling */
    if (0 != setjmp(png_jmpbuf(png_ptr)))
        /* if we get here, we had a problem reading the file */
        /* free all of the memory associated with the png_ptr and info_ptr */
        return read_png_abort(fp, &png_ptr, &info_ptr);

    /* set up the input control using standard C streams */
    png_init_io(png_ptr, fp);

    /* let libpng know that some bytes have been read */
    png_set_sig_bytes(png_ptr, PNG_SIG_LEN);

    /*
     * set the read filter transforms, to get 8bit RGB whatever the
     * original file may contain:
     * PNG_TRANSFORM_STRIP_16      strip 16-bit samples to 8 bits
     * PNG_TRANSFORM_PACKING       expand 1, 2 and 4-bit
     *                             samples to bytes
     */
    transform |= (PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING);

    /* read in the entire image at once */
    png_read_png(png_ptr, info_ptr, transform, NULL);

    /* get image informations */
    *nx = (size_t) png_get_image_width(png_ptr, info_ptr);
    *ny = (size_t) png_get_image_height(png_ptr, info_ptr);
    *nc = (size_t) png_get_channels(png_ptr, info_ptr);
    row_pointers = png_get_rows(png_ptr, info_ptr);

    /*
     * allocate the output data RGB array
     * deinterlace and convert png RGB RGB RGB 8bit to RRR GGG BBB
     * the image is deinterlaced layer after layer
     * this generic loop also works for one single channel
     */
    size = *nx * *ny * *nc;
    switch (dtype)
    {
//.........这里部分代码省略.........
开发者ID:tguillemot,项目名称:midway_equalization,代码行数:101,代码来源:io_png.c

示例15: save_as_png

void save_as_png(T & file, std::vector<mapnik::rgb> const& palette,
                 mapnik::image_data_8 const& image,
                 unsigned width,
                 unsigned height,
                 unsigned color_depth,
                 int compression,
                 int strategy,
                 std::vector<unsigned> const&alpha)
{
    png_voidp error_ptr=0;
    png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
                                                error_ptr,0, 0);

    if (!png_ptr) return;

    // switch on optimization only if supported
#if defined(PNG_LIBPNG_VER) && (PNG_LIBPNG_VER >= 10200) && defined(PNG_MMX_CODE_SUPPORTED)
    png_uint_32 mask, flags;
    flags = png_get_asm_flags(png_ptr);
    mask = png_get_asm_flagmask(PNG_SELECT_READ | PNG_SELECT_WRITE);
    png_set_asm_flags(png_ptr, flags | mask);
#endif
    png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        png_destroy_write_struct(&png_ptr,(png_infopp)0);
        return;
    }
    jmp_buf* jmp_context = (jmp_buf*) png_get_error_ptr(png_ptr);
    if (jmp_context)
    {
        png_destroy_write_struct(&png_ptr, &info_ptr);
        return;
    }
    png_set_write_fn (png_ptr, &file, &write_data<T>, &flush_data<T>);

    png_set_compression_level(png_ptr, compression);
    png_set_compression_strategy(png_ptr, strategy);
    png_set_compression_buffer_size(png_ptr, 32768);

    png_set_IHDR(png_ptr, info_ptr,width,height,color_depth,
                 PNG_COLOR_TYPE_PALETTE,PNG_INTERLACE_NONE,
                 PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT);

    png_color* pal = const_cast<png_color*>(reinterpret_cast<const png_color*>(&palette[0]));
    png_set_PLTE(png_ptr, info_ptr, pal, palette.size());

    // make transparent lowest indexes, so tRNS is small
    if (alpha.size()>0)
    {
        std::vector<png_byte> trans(alpha.size());
        unsigned alphaSize=0;//truncate to nonopaque values
        for(unsigned i=0; i < alpha.size(); i++)
        {
            trans[i]=alpha[i];
            if (alpha[i]<255)
                alphaSize = i+1;
        }
        if (alphaSize>0)
            png_set_tRNS(png_ptr, info_ptr, (png_bytep)&trans[0], alphaSize, 0);
    }

    png_write_info(png_ptr, info_ptr);
    for (unsigned i=0;i<height;i++)
    {
        png_write_row(png_ptr,(png_bytep)image.getRow(i));
    }

    png_write_end(png_ptr, info_ptr);
    png_destroy_write_struct(&png_ptr, &info_ptr);
}
开发者ID:NeilvB,项目名称:mapnik,代码行数:72,代码来源:png_io.hpp


注:本文中的png_create_info_struct函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。