chiark / gitweb /
73f65718354a19e88dcdde11c307010d90f709f5
[ypp-sc-tools.web-live.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 { char blue2[256]; } CanonColourInfoBlues;
37 typedef struct { CanonColourInfoBlues *green2[256]; } CanonColourInfoGreens;
38 typedef struct { CanonColourInfoGreens *red2[256]; } CanonColourInfoReds;
39 extern CanonColourInfoReds canoncolourinfo_tree;
40
41 CanonImage *alloc_canon_image(int w, int h);
42
43 static inline char canon_lookup_colour(unsigned char r,
44                                        unsigned char g,
45                                        unsigned char b) {
46   CanonColourInfoGreens *greens= canoncolourinfo_tree.red2[r];
47   if (!greens) return '?';
48   CanonColourInfoBlues *blues= greens->green2[g];
49   if (!blues) return '?';
50   return blues->blue2[b];
51 }
52
53 #define CANONICALISE_IMAGE(im,w,h,rgb_save, COMPUTE_RGB) do{    \
54     /* compute_rgb should be a number of statements, or         \
55      * a block, which assigns to                                \
56      *   Rgb rgb;                                               \
57      * given the values of                                      \
58      *   int x,y;                                               \
59      * all of which are anamorphic.  Result is stored in im.    \
60      * The COMPUTE_RGB is executed exactly once for             \
61      * each pixel in reading order.                             \
62      */                                                         \
63     (im)= alloc_canon_image((w), (h));                          \
64     (rgb_save)= alloc_rgb_image((w), (h));                      \
65                                                                 \
66     int x,y;                                                    \
67     for (y=0; y<(h); y++) {                                     \
68       for (x=0; x<(w); x++) {                                   \
69         unsigned char r,g,b;                                    \
70         COMPUTE_RGB;                                            \
71         CANONIMG_ALSO_STORERGB((rgb_save));                     \
72         (im)->d[y*(w) + x]= canon_lookup_colour(r,g,b);         \
73       }                                                         \
74       if (DEBUGP(rect)) {                                       \
75         fprintf(debug, "%4d ",y);                               \
76         fwrite(im->d + y*(w), 1,(w), debug);                    \
77         fputc('\n',debug);                                      \
78       }                                                         \
79     }                                                           \
80     debug_flush();                                              \
81   }while(0)
82
83
84 #define CANONIMG_ALSO_STORERGB(ri)              \
85   do{                                           \
86     unsigned char *rip= RI_PIXEL((ri),x,y);     \
87     rip[0]= r;                                  \
88     rip[1]= g;                                  \
89     rip[2]= b;                                  \
90   }while(0)
91
92
93 #endif /*STRUCTURE_H*/