9 int alloc_image(struct image *img, int x, int y, int bpp)
11 memset(img, 0, sizeof *img);
15 img->scansz = img->pitch = x * bpp / 8;
17 if(!(img->pixels = malloc(y * img->scansz))) {
18 fprintf(stderr, "failed to allocate %dx%d (%dbpp) pixel buffer\n", x, y, bpp);
22 /* just a guess, assume the user will fill the details, but set up reasonable
23 * defaults just in case...
27 img->cmap_ncolors = 1 << bpp;
28 } else if(bpp <= 24) {
36 int load_image(struct image *img, const char *fname)
42 int chan_bits, color_type;
45 unsigned char **scanline;
48 if(!(fp = fopen(fname, "rb"))) {
49 fprintf(stderr, "failed to open: %s: %s\n", fname, strerror(errno));
53 if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
57 if(!(info = png_create_info_struct(png))) {
59 png_destroy_read_struct(&png, 0, 0);
62 if(setjmp(png_jmpbuf(png))) {
64 png_destroy_read_struct(&png, &info, 0);
69 png_read_png(png, info, 0, 0);
71 png_get_IHDR(png, info, &xsz, &ysz, &chan_bits, &color_type, 0, 0, 0);
74 img->nchan = png_get_channels(png, info);
75 img->bpp = img->nchan * chan_bits;
76 img->scansz = img->pitch = xsz * img->bpp / 8;
77 img->cmap_ncolors = 0;
79 if(color_type == PNG_COLOR_TYPE_PALETTE) {
80 png_get_PLTE(png, info, &palette, &img->cmap_ncolors);
81 memcpy(img->cmap, palette, img->cmap_ncolors * sizeof *img->cmap);
84 if(!(img->pixels = malloc(ysz * img->scansz))) {
85 perror("failed to allocate pixel buffer");
87 png_destroy_read_struct(&png, &info, 0);
92 scanline = (unsigned char**)png_get_rows(png, info);
93 for(i=0; i<ysz; i++) {
94 memcpy(dptr, scanline[i], img->scansz);
99 png_destroy_read_struct(&png, &info, 0);
103 int save_image(struct image *img, const char *fname)
105 int i, chan_bits, coltype;
110 unsigned char **scanline = 0;
113 if(!(fp = fopen(fname, "wb"))) {
114 fprintf(stderr, "save_image: failed to open: %s: %s\n", fname, strerror(errno));
118 if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
122 if(!(info = png_create_info_struct(png))) {
123 png_destroy_write_struct(&png, 0);
128 txt.compression = PNG_TEXT_COMPRESSION_NONE;
129 txt.key = "Software";
130 txt.text = "img2tiles";
133 if(setjmp(png_jmpbuf(png))) {
134 png_destroy_write_struct(&png, &info);
142 if(img->cmap_ncolors > 0) {
143 coltype = PNG_COLOR_TYPE_PALETTE;
145 coltype = PNG_COLOR_TYPE_GRAY;
149 coltype = PNG_COLOR_TYPE_GRAY_ALPHA;
152 coltype = PNG_COLOR_TYPE_RGB;
155 coltype = PNG_COLOR_TYPE_RGB_ALPHA;
159 chan_bits = img->bpp / img->nchan;
160 png_set_IHDR(png, info, img->width, img->height, chan_bits, coltype, PNG_INTERLACE_NONE,
161 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
162 png_set_text(png, info, &txt, 1);
164 if(img->cmap_ncolors > 0) {
165 png_set_PLTE(png, info, (png_color*)img->cmap, img->cmap_ncolors);
168 if(!(scanline = malloc(img->height * sizeof *scanline))) {
169 png_destroy_write_struct(&png, &info);
175 for(i=0; i<img->height; i++) {
179 png_set_rows(png, info, scanline);
181 png_init_io(png, fp);
182 png_write_png(png, info, 0, 0);
183 png_destroy_write_struct(&png, &info);
190 int cmp_image(struct image *a, struct image *b)
193 unsigned char *aptr = a->pixels;
194 unsigned char *bptr = b->pixels;
196 if(a->width != b->width || a->height != b->height || a->bpp != b->bpp || a->nchan != b->nchan) {
200 for(i=0; i<a->height; i++) {
201 if(memcmp(aptr, bptr, a->scansz) != 0) {
210 void blit(struct image *src, int sx, int sy, int w, int h, struct image *dst, int dx, int dy)
213 unsigned char *sptr, *dptr;
215 assert(src->bpp == dst->bpp);
216 assert(src->nchan == dst->nchan);
218 if(sx < 0) { w += sx; sx = 0; }
219 if(sy < 0) { h += sy; sy = 0; }
220 if(dx < 0) { w += dx; sx -= dx; dx = 0; }
221 if(dy < 0) { h += dy; sy -= dy; dy = 0; }
222 if(sx + w >= src->width) w = src->width - sx;
223 if(sy + h >= src->height) h = src->height - sy;
224 if(dx + w >= dst->width) w = dst->width - dx;
225 if(dy + h >= dst->height) h = dst->height - dy;
227 if(w <= 0 || h <= 0) return;
229 sptr = src->pixels + sy * src->pitch + sx * src->bpp / 8;
230 dptr = dst->pixels + dy * dst->pitch + dx * dst->bpp / 8;
233 memcpy(dptr, sptr, w * dst->bpp / 8);