chiark / gitweb /
WIP island determination; pixmap handling in progress
[ypp-sc-tools.web-live.git] / pctb / rgbimage.c
1 /*
2  * Handling of colour (RGB) images
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 #include "convert.h"
29
30 /*
31  *
32  *
33  * widgets.txt
34  * format
35  *
36  *  <magic>
37  *
38  *  <context>
39  *  <string>
40  *  ppmnoraw depth 8
41  *
42  *  <context>
43  *  <string>
44  *  ppmnoraw depth 8
45  *  .
46  *
47  */
48
49 #include "convert.h"
50
51 #define dbassert(x)                                                     \
52   ((x) ? (void)0 :                                                      \
53      fatal("Error in pixmap database.\n"                                \
54            " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
55
56 static int identify(const RgbImage *base, Rect portion,
57                     char result[MAXIMGIDENT]) {
58   FILE *f;
59   sysassert( f= fopen("pixmaps.txt","r") );
60   struct pam inpam;
61
62 #define FGETSLINE (fgetsline(f,result,MAXIMGIDENT))
63
64   FGETSLINE;
65 fprintf(stderr,">%s<\n",result);
66   dbassert(!strcmp(result,"# ypp-sc-tools pctb pixmaps v1"));
67   
68   void *row= 0;
69   size_t rowa= 0;
70   
71   for (;;) {
72     FGETSLINE;
73     if (!result || result[0]=='#') continue;
74     if (!strcmp(result,".")) break;
75
76     pnm_readpaminit(f, &inpam, sizeof(inpam));
77     dbassert(inpam.maxval == 255);
78     dbassert(inpam.bytes_per_sample == 1);
79     dbassert(inpam.format == PPM_FORMAT);
80
81     if (rowa < inpam.width) {
82       rowa= inpam.width;
83       row= mrealloc(row, rowa*3);
84     }
85     int diff=
86       inpam.width  != RECT_W(portion) ||
87       inpam.height != RECT_H(portion);
88
89     int y;
90     for (y=0; y<inpam.height; y++) {
91       int r= fread(row,1, 3*inpam.width, f);
92       sysassert(!ferror(f));
93       dbassert(!feof(f));
94       assert(r == 3*inpam.width);
95       if (diff) continue;
96
97       diff= memcmp(RI_PIXEL(base, portion.tl.x, portion.tl.y + y),
98                    row, 3*inpam.width);
99     }
100     if (!diff)
101       return 1;
102   }
103   return 0;
104 }
105  
106 void identify_rgbimage(const RgbImage *base, Rect portion,
107                        char result[MAXIMGIDENT]) {
108   int ok= identify(base, portion, result);
109   if (ok) return;
110
111   if (DEBUGP(pixmap)) {
112     int x,y,i;
113     fprintf(stderr,"P3\n%d %d\n255\n", RECT_W(portion), RECT_H(portion));
114     for (y=portion.tl.y; y<=portion.br.y; y++) {
115       for (x=portion.tl.x; x<=portion.br.x; x++) {
116         putc(' ',stderr);
117         for (i=0; i<3; i++)
118           fprintf(stderr," %3d", RI_PIXEL(base,x,y)[i]);
119       }
120       putc('\n',stderr);
121     }
122   }
123   fatal("Unrecognised pixmap.");
124 }
125
126 RgbImage *alloc_rgb_image(int w, int h) {
127   RgbImage *ri;
128   ri= mmalloc(sizeof(*ri) + w*h*3);
129   ri->w= w;
130   ri->h= h;
131   return ri;
132 }