/* Loads PNG using libpng */ static TLN_Bitmap LoadPNG (char *filename) { png_image image; TLN_Bitmap bitmap = NULL; /* Only the image structure version number needs to be set. */ memset (&image, 0, sizeof image); image.version = PNG_IMAGE_VERSION; if (png_image_begin_read_from_file (&image, filename)) { int src_line_size = image.width * PNG_IMAGE_PIXEL_SIZE(image.format); int lut_size = PNG_IMAGE_COLORMAP_SIZE(image); BYTE* data = malloc (src_line_size * image.height); BYTE* lut = malloc (lut_size); bitmap = TLN_CreateBitmap (image.width, image.height, PNG_IMAGE_PIXEL_SIZE(image.format)<<3); /* Change this to try different formats! If you set a colormap format * then you must also supply a colormap below. */ image.format = PNG_FORMAT_RGB_COLORMAP; if (png_image_finish_read (&image, NULL, data, 0, lut)) { BYTE *src, *dst; unsigned int c; png_image_free (&image); /* copy scanlines */ src = data; for (c=0; c<image.height; c++) { dst = TLN_GetBitmapPtr (bitmap, 0, c); memcpy (dst, src, src_line_size); src += src_line_size; } /* get palette */ { BYTE *src = lut; png_uint_32 c; TLN_Palette palette; palette = TLN_CreatePalette (image.colormap_entries); for (c=0; c<image.colormap_entries; c++) { TLN_SetPaletteColor (palette, c, src[0], src[1], src[2]); src += 3; } TLN_SetBitmapPalette (bitmap, palette); } } free (lut); free (data); } return bitmap; }
int bounds_draw_line( png_image_exp image, const size_t x, const size_t y, const size_t pixeldist, const size_t len, png_byte val ) { png_byte bpp; size_t stride; size_t offset; size_t i; png_bytep out; if(!image || !image->buf || !len || x >= image->img.width || y >= image->img.height) { return -1; } bpp = PNG_IMAGE_PIXEL_SIZE(image->img.format); stride = bpp * image->img.width; offset = (y * stride) + (x * bpp); for( (i = 0), (out = image->buf + offset); (i < len) && (out < image->buf + PNG_IMAGE_SIZE(image->img)); (i++), (out += pixeldist * bpp) ) { out[0] = out[bpp - 1] = val; } return 0; }
void opengl_texture::load_PNG() { png_image png; memset(&png, 0, sizeof(png_image)); png.version = PNG_IMAGE_VERSION; png_image_begin_read_from_file(&png, (name + type).c_str()); if (png.warning_or_error) { data_state = resource_state::failed; ErrorLog(name + " error: " + std::string(png.message)); return; } if (png.format & PNG_FORMAT_FLAG_ALPHA) { data_format = GL_RGBA; data_components = GL_RGBA; png.format = PNG_FORMAT_RGBA; } else { data_format = GL_RGB; data_components = GL_RGB; png.format = PNG_FORMAT_RGB; } data_width = png.width; data_height = png.height; data.resize(PNG_IMAGE_SIZE(png)); png_image_finish_read(&png, nullptr, (void*)&data[0], -data_width * PNG_IMAGE_PIXEL_SIZE(png.format), nullptr); // we're storing texture data internally with bottom-left origin // so use negative stride if (png.warning_or_error) { data_state = resource_state::failed; ErrorLog(name + " error: " + std::string(png.message)); return; } data_mapcount = 1; data_state = resource_state::good; }
static int read_png(const char *fname, image *image, gradient *g) { int result = 0; memset(&image->image, 0, sizeof image->image); image->image.version = PNG_IMAGE_VERSION; if (png_image_begin_read_from_file(&image->image, image->file_name)) { image->image.format = PNG_FORMAT_RGBA; image->stride = PNG_IMAGE_ROW_STRIDE(image->image); image->buffer = malloc(PNG_IMAGE_SIZE(image->image)); image->pixel_bytes = PNG_IMAGE_PIXEL_SIZE(image->image.format); if (image->buffer != NULL) { if(png_image_finish_read(&image->image, NULL /*background*/, image->buffer, (png_int_32)image->stride, image->colormap)) { if(calculate_gradient(image, g)) result = 1; else printf("pngtocss: Gradient type not supported\n"); } else { fprintf(stderr, "pngtocss: read %s: %s\n", fname, image->image.message); png_image_free(&image->image); } } else fprintf(stderr, "pngtocss: out of memory: %lu bytes\n", (unsigned long)PNG_IMAGE_SIZE(image->image)); } else /* Failed to read the argument: */ fprintf(stderr, "pngtocss: %s: %s\n", fname, image->image.message); return result; }