chiark / gitweb /
WIP island determination; pixmap handling in progress
[ypp-sc-tools.main.git] / pctb / structure.h
1 /*
2  * Image canonicalisation function for use by callers feeding
3  *  images into structure.c's routines.
4  */
5 /*
6  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
7  *  players of Yohoho Puzzle Pirates.
8  * 
9  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
10  * 
11  *  This program is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation, either version 3 of the License, or
14  *  (at your option) any later version.
15  * 
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  * 
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  * 
24  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
25  *  are used without permission.  This program is not endorsed or
26  *  sponsored by Three Rings.
27  */
28
29 #ifndef STRUCTURE_H
30 #define STRUCTURE_H
31
32
33 #include "convert.h"
34
35
36 typedef struct {
37   unsigned long rgb; /* on screen */
38   char c; /* canonical */
39 } CanonColourInfo;
40
41 extern const CanonColourInfo canoncolourinfos[];
42
43 CanonImage *alloc_canon_image(int w, int h);
44
45 #define CANONICALISE_IMAGE(im,w,h, COMPUTE_RGB) do{             \
46     /* compute_rgb should be a number of statements, or         \
47      * a block, which assigns to                                \
48      *   unsigned long rgb;                                     \
49      * given the values of                                      \
50      *   int x,y;                                               \
51      * all of which are anamorphic.  Result is stored in im.    \
52      * The COMPUTE_RGB is executed exactly once for             \
53      * each pixel in reading order.                             \
54      */                                                         \
55     (im)= alloc_canon_image((w), (h));                          \
56                                                                 \
57     int x,y;                                                    \
58     for (y=0; y<(h); y++) {                                     \
59       for (x=0; x<(w); x++) {                                   \
60         const CanonColourInfo *cci;                             \
61         unsigned long rgb;                                      \
62         COMPUTE_RGB;                                            \
63         for (cci=canoncolourinfos; cci->c; cci++) {             \
64           if (cci->rgb == rgb) {                                \
65             (im)->d[y*(w) + x]= cci->c;                         \
66             break;                                              \
67           }                                                     \
68         }                                                       \
69       }                                                         \
70       if (DEBUGP(rect)) {                                       \
71         fprintf(debug, "%4d ",y);                               \
72         fwrite(im->d + y*w, 1,w, debug);                        \
73         fputc('\n',debug);                                      \
74       }                                                         \
75     }                                                           \
76     debug_flush();                                              \
77   }while(0)
78
79
80 #define CANONIMG_ALSO_STORERGB(ri)              \
81   do{                                           \
82     char *rip= RI_PIXEL((ri),x,y);              \
83     rip[0]= rgb >> 16;                          \
84     rip[1]= rgb >> 8;                           \
85     rip[2]= rgb;                                \
86   }while(0)
87
88
89 #endif /*STRUCTURE_H*/