2 * Handling of colour (RGB) images
5 * This is part of ypp-sc-tools, a set of third-party tools for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
51 static int identify1(const RgbImage *base, Rect portion,
52 char result[MAXIMGIDENT], const char *what,
54 char *dbfile_name= masprintf("#%s-pixmap#.txt",which);
55 if (!dbfile_open(dbfile_name))
58 #define FGETSLINE (dbfile_getsline(result,MAXIMGIDENT,__FILE__,__LINE__))
61 dbassert(!strcmp(result,"# ypp-sc-tools pctb pixmaps v1"));
68 if (!result[0] || result[0]=='#') continue;
69 if (!strcmp(result,".")) break;
72 dbfile_getsline(magic,sizeof(magic),__FILE__,__LINE__);
74 dbassert(!strcmp(magic,"P3"));
76 dbassert( dbfile_scanf("%d %d %d",&w,&h,&maxval) == 3);
77 dbassert(w>0); dbassert(h>0); dbassert(maxval==255);
81 row= mrealloc(row, rowa*3);
83 int diff= w != RECT_W(portion) || h != RECT_H(portion);
90 dbassert( dbfile_scanf("%d",&c) == 1);
91 dbassert(c>=0 && c<=255);
92 diff |= (c != RI_PIXEL(base, portion.tl.x + x, portion.tl.y + y)[i]);
97 progress_log("Identified %s image: %s.",what,result);
110 static int identify(const RgbImage *base, Rect portion,
111 char result[MAXIMGIDENT], const char *what) {
112 return identify1(base,portion,result,what, "master") ||
113 identify1(base,portion,result,what, "local");
116 static void fwrite_ppm(FILE *f, const RgbImage *base, Rect portion) {
118 fprintf(f,"P3\n%d %d\n255\n", RECT_W(portion), RECT_H(portion));
119 for (y=portion.tl.y; y<=portion.br.y; y++) {
120 for (x=portion.tl.x; x<=portion.br.x; x++) {
123 fprintf(f," %3d", RI_PIXEL(base,x,y)[i]);
127 sysassert(!ferror(f));
128 sysassert(!fflush(f));
131 void identify_rgbimage(const RgbImage *base, Rect portion,
132 char result[MAXIMGIDENT], const char *what) {
136 fetch_with_rsync("pixmap");
141 int ok= identify(base, portion, result, what);
145 fwrite_ppm(stderr,base,portion);
147 FILE *resolver= resolve_start();
149 fatal("Image recognition failed - unrecognised island.\n"
150 "See FIXME.FIXME\n");
152 fprintf(resolver, "pixmap\n" "%s\n", what);
153 fwrite_ppm(resolver, base, portion);
160 RgbImage *alloc_rgb_image(int w, int h) {
162 ri= mmalloc(sizeof(*ri) + w*h*3);