chiark / gitweb /
Store full-colour image with every screenshot
[ypp-sc-tools.db-test.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     (im)->rgb= alloc_rgb_image((w), (h));                       \
57                                                                 \
58     int x,y;                                                    \
59     unsigned long last_rgb= ~0UL;                               \
60     int last_c= -1;                                             \
61     for (y=0; y<(h); y++) {                                     \
62       for (x=0; x<(w); x++) {                                   \
63         const CanonColourInfo *cci;                             \
64         unsigned long rgb;                                      \
65         COMPUTE_RGB;                                            \
66         CANONIMG_ALSO_STORERGB((im)->rgb);                      \
67         if (rgb == last_rgb) {                                  \
68           (im)->d[y*(w) + x]= last_c;                           \
69         } else {                                                \
70           for (cci=canoncolourinfos; cci->c; cci++) {           \
71             if (cci->rgb == rgb) {                              \
72               last_rgb= rgb;                                    \
73               (im)->d[y*(w) + x]= last_c= cci->c;               \
74               break;                                            \
75             }                                                   \
76           }                                                     \
77         }                                                       \
78       }                                                         \
79       if (DEBUGP(rect)) {                                       \
80         fprintf(debug, "%4d ",y);                               \
81         fwrite(im->d + y*(w), 1,(w), debug);                    \
82         fputc('\n',debug);                                      \
83       }                                                         \
84     }                                                           \
85     debug_flush();                                              \
86   }while(0)
87
88
89 #define CANONIMG_ALSO_STORERGB(ri)              \
90   do{                                           \
91     unsigned char *rip= RI_PIXEL((ri),x,y);     \
92     rip[0]= rgb >> 16;                          \
93     rip[1]= rgb >> 8;                           \
94     rip[2]= rgb;                                \
95   }while(0)
96
97
98 #endif /*STRUCTURE_H*/