eobish

view src/image.c @ 8:c0e8bbf96849

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 19 Jan 2015 15:49:36 +0200
parents
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include "image.h"
7 #define LOADFAIL(f, m) fprintf(stderr, "failed to load image: %s: %s\n", f, m)
9 int load_image(struct image *img, const char *fname)
10 {
11 static const char *magic = "TILEIMAG";
12 FILE *fp;
13 char sig[8];
15 if(!(fp = fopen(fname, "rb"))) {
16 LOADFAIL(fname, strerror(errno));
17 return -1;
18 }
19 if(fread(sig, sizeof sig, 1, fp) <= 0 || memcmp(sig, magic, 8) != 0) {
20 LOADFAIL(fname, "corrupted or empty file");
21 fclose(fp);
22 return -1;
23 }
24 if(fread(&img->xsz, 4, 1, fp) <= 0 || fread(&img->ysz, 4, 1, fp) <= 0) {
25 LOADFAIL(fname, "failed to read file header");
26 fclose(fp);
27 return -1;
28 }
30 if(!(img->pixels = malloc(img->xsz * img->ysz))) {
31 LOADFAIL(fname, "failed to allocate pixel buffer");
32 fclose(fp);
33 return -1;
34 }
35 if(fread(img->pixels, 1, img->xsz * img->ysz, fp) < img->xsz * img->ysz) {
36 LOADFAIL(fname, "unexpected end of file while reading pixels");
37 fclose(fp);
38 return -1;
39 }
40 fclose(fp);
41 return 0;
42 }
44 void destroy_image(struct image *img)
45 {
46 if(img) {
47 free(img->pixels);
48 img->pixels = 0;
49 }
50 }
52 int load_palette(struct color *col, const char *fname)
53 {
54 FILE *fp;
55 char buf[128];
56 int nent = 0;
58 if(!(fp = fopen(fname, "r"))) {
59 fprintf(stderr, "failed to open palette file: %s: %s\n", fname, strerror(errno));
60 return -1;
61 }
63 while(fgets(buf, sizeof buf, fp)) {
64 char *endp, *line = buf;
65 int r, g, b;
67 if(!line || !*line) continue;
69 if(*line == '#') { /* hex html-like values */
70 unsigned int val = strtol(line + 1, &endp, 16);
71 if(endp == line) {
72 fprintf(stderr, "unrecognized line \"%s\" in palette file: %s\n", line, fname);
73 fclose(fp);
74 return -1;
75 }
77 r = (val >> 16) & 0xff;
78 g = (val >> 8) & 0xff;
79 b = val & 0xff;
80 } else {
81 fprintf(stderr, "unrecognized line \"%s\" in palette file: %s\n", line, fname);
82 fclose(fp);
83 return -1;
84 }
86 if(nent >= 256) {
87 fprintf(stderr, "palette file %s contains more than 256 entries ... skipping the rest\n", fname);
88 break;
89 }
91 col[nent].r = r;
92 col[nent].g = g;
93 col[nent].b = b;
94 nent++;
95 }
97 printf("loaded palette: %s (%d colors)\n", fname, nent);
99 fclose(fp);
100 return nent;
101 }
103 #define MIN(a, b) ((a) < (b) ? (a) : (b))
104 #define MAX(a, b) ((a) > (b) ? (a) : (b))
106 void blitkey(struct image *destimg, int dstx, int dsty, struct image *srcimg, int key)
107 {
108 int srcx = 0, srcy = 0;
109 int i, j, width, height;
110 unsigned char *src, *dst;
112 if(dstx < 0) {
113 srcx += -dstx;
114 dstx = 0;
115 }
116 if(dsty < 0) {
117 srcy += -dsty;
118 dsty = 0;
119 }
121 width = MIN(destimg->xsz - dstx, srcimg->xsz - srcx);
122 height = MIN(destimg->ysz - dsty, srcimg->ysz - srcy);
124 if(width <= 0 || height <= 0) return; /* ended up with a zero-area blit */
126 src = srcimg->pixels + srcy * srcimg->xsz + srcx;
127 dst = destimg->pixels + dsty * destimg->xsz + dstx;
129 for(i=0; i<height; i++) {
130 for(j=0; j<width; j++) {
131 if(src[j] != key) {
132 dst[j] = src[j];
133 }
134 }
135 src += srcimg->xsz;
136 dst += destimg->xsz;
137 }
138 }