10 int alloc_image(struct image *img, int x, int y, int bpp)
12 memset(img, 0, sizeof *img);
16 img->scansz = img->pitch = x * bpp / 8;
18 if(!(img->pixels = malloc(y * img->scansz))) {
19 printf("failed to allocate %dx%d (%dbpp) pixel buffer\n", x, y, bpp);
23 /* just a guess, assume the user will fill the details, but set up reasonable
24 * defaults just in case...
28 img->cmap_ncolors = 1 << bpp;
29 } else if(bpp <= 24) {
37 int load_image(struct image *img, const char *fname)
43 int chan_bits, color_type;
46 unsigned char **scanline;
49 if(!(fp = fopen(fname, "rb"))) {
50 printf("failed to open: %s: %s\n", fname, strerror(errno));
54 if(!(png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
58 if(!(info = png_create_info_struct(png))) {
60 png_destroy_read_struct(&png, 0, 0);
63 if(setjmp(png_jmpbuf(png))) {
65 png_destroy_read_struct(&png, &info, 0);
70 png_read_png(png, info, 0, 0);
72 png_get_IHDR(png, info, &xsz, &ysz, &chan_bits, &color_type, 0, 0, 0);
75 img->nchan = png_get_channels(png, info);
76 img->bpp = img->nchan * chan_bits;
77 img->scansz = img->pitch = xsz * img->bpp / 8;
78 img->cmap_ncolors = 0;
80 if(color_type == PNG_COLOR_TYPE_PALETTE) {
81 png_get_PLTE(png, info, &palette, &img->cmap_ncolors);
82 memcpy(img->cmap, palette, img->cmap_ncolors * sizeof *img->cmap);
85 if(!(img->pixels = malloc(ysz * img->scansz))) {
86 perror("failed to allocate pixel buffer");
88 png_destroy_read_struct(&png, &info, 0);
93 scanline = (unsigned char**)png_get_rows(png, info);
94 for(i=0; i<ysz; i++) {
95 memcpy(dptr, scanline[i], img->scansz);
100 png_destroy_read_struct(&png, &info, 0);
104 int save_image(struct image *img, const char *fname)
106 int i, chan_bits, coltype;
111 unsigned char **scanline = 0;
114 if(!(fp = fopen(fname, "wb"))) {
115 printf("save_image: failed to open: %s: %s\n", fname, strerror(errno));
119 if(!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) {
123 if(!(info = png_create_info_struct(png))) {
124 png_destroy_write_struct(&png, 0);
129 txt.compression = PNG_TEXT_COMPRESSION_NONE;
130 txt.key = "Software";
131 txt.text = "img2tiles";
134 if(setjmp(png_jmpbuf(png))) {
135 png_destroy_write_struct(&png, &info);
143 if(img->cmap_ncolors > 0) {
144 coltype = PNG_COLOR_TYPE_PALETTE;
146 coltype = PNG_COLOR_TYPE_GRAY;
150 coltype = PNG_COLOR_TYPE_GRAY_ALPHA;
153 coltype = PNG_COLOR_TYPE_RGB;
156 coltype = PNG_COLOR_TYPE_RGB_ALPHA;
160 chan_bits = img->bpp / img->nchan;
161 png_set_IHDR(png, info, img->width, img->height, chan_bits, coltype, PNG_INTERLACE_NONE,
162 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
163 png_set_text(png, info, &txt, 1);
165 if(img->cmap_ncolors > 0) {
166 png_set_PLTE(png, info, (png_color*)img->cmap, img->cmap_ncolors);
169 if(!(scanline = malloc(img->height * sizeof *scanline))) {
170 png_destroy_write_struct(&png, &info);
176 for(i=0; i<img->height; i++) {
180 png_set_rows(png, info, scanline);
182 png_init_io(png, fp);
183 png_write_png(png, info, 0, 0);
184 png_destroy_write_struct(&png, &info);
190 int conv_image_rgb565(struct image *img16, struct image *img)
196 if(!img->cmap_ncolors && img->bpp < 24) {
197 fprintf(stderr, "unsupported conversion from %dbpp to 16bpp 565\n", img->bpp);
201 img16->width = img->width;
202 img16->height = img->height;
203 img16->nchan = img->nchan;
205 img16->scansz = img16->pitch = img->width * 2;
206 if(!(img16->pixels = malloc(img16->height * img16->scansz))) {
211 dptr = (uint16_t*)img16->pixels;
213 for(i=0; i<img->height; i++) {
214 for(j=0; j<img->width; j++) {
215 unsigned int r, g, b;
216 if(img->bpp <= 8 && img->cmap_ncolors) {
217 int idx = *sptr++ % img->cmap_ncolors;
218 r = img->cmap[idx].r >> 3;
219 g = img->cmap[idx].g >> 2;
220 b = img->cmap[idx].b >> 3;
221 } else if(img->nchan == 1) {
222 g = (*sptr++ >> 2) & 0x3f;
225 r = (*sptr++ >> 3) & 0x1f;
226 g = (*sptr++ >> 2) & 0x3f;
227 b = (*sptr++ >> 3) & 0x1f;
229 *dptr++ = (r << 11) | (g << 5) | b;
236 int cmp_image(struct image *a, struct image *b)
239 unsigned char *aptr = a->pixels;
240 unsigned char *bptr = b->pixels;
242 if(a->width != b->width || a->height != b->height || a->bpp != b->bpp || a->nchan != b->nchan) {
246 for(i=0; i<a->height; i++) {
247 if(memcmp(aptr, bptr, a->scansz) != 0) {
256 void blit_image(struct image *src, int sx, int sy, int w, int h, struct image *dst, int dx, int dy)
259 unsigned char *sptr, *dptr;
261 assert(src->bpp == dst->bpp);
262 assert(src->nchan == dst->nchan);
264 if(sx < 0) { w += sx; sx = 0; }
265 if(sy < 0) { h += sy; sy = 0; }
266 if(dx < 0) { w += dx; sx -= dx; dx = 0; }
267 if(dy < 0) { h += dy; sy -= dy; dy = 0; }
268 if(sx + w >= src->width) w = src->width - sx;
269 if(sy + h >= src->height) h = src->height - sy;
270 if(dx + w >= dst->width) w = dst->width - dx;
271 if(dy + h >= dst->height) h = dst->height - dy;
273 if(w <= 0 || h <= 0) return;
275 sptr = src->pixels + sy * src->pitch + sx * src->bpp / 8;
276 dptr = dst->pixels + dy * dst->pitch + dx * dst->bpp / 8;
279 memcpy(dptr, sptr, w * dst->bpp / 8);
285 void image_color_offset(struct image *img, int offs)
287 int i, sz = img->height * img->pitch;
288 unsigned char *pptr = img->pixels;
290 for(i=0; i<sz; i++) {