3 The tool to enhance t-1 frames by remapping the palette
6 Copyright (C) 2015, 2022 Balthasar SzczepaĆski
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Affero General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 Requires Dev Image Library (libdevil) (http://openil.sourceforge.net/)
23 on Pentium III libdevil must be recompiled with
24 --disable-ssl2 --disable-ssl3
25 (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572954)
34 #include "remap_t_1.h"
44 struct ColorInfo new_pal[0x100];
47 int palette_remap (ILuint n, struct PixelInfo *p, void *data);
48 ILubyte b642d(char c);
50 char REMAPT1_MISSING_ARGS[] = "Missing parameters.\npremap_t_1 inPix outPix palette [n1 n2]\n";
52 int subtool_remap_t_1 (int argc, char **argv, int argi, char **err)
54 struct IL_full_info info;
55 FLAG_TYPE flags = MUST_BE_INDEXED | CAN_BE_MULTIPLE | CANNOT_HAVE_ALPHA | OK_PALETTE_ONLY;
56 struct remap_data data;
64 *err = REMAPT1_MISSING_ARGS;
70 sscanf(argv[argi+3],"%u",&(data.n0));
71 sscanf(argv[argi+4],"%u",&(data.n1));
78 data.n = data.n0 * data.n1;
81 *err = BAD_PALETTE_SIZE;
85 r = reserve_pictures(1);
92 r = load_picture(0, argv[argi], &info, &flags);
99 if (info.palette_num_cols != data.n)
101 *err = BAD_PALETTE_SIZE;
105 for (i=0, k=0; i<data.n1; ++i)
107 for(j=0; j<4; ++j, ++k)
109 v[j] = argv[argi+2][k];
119 data.new_pal[i].red = ( v[0] << 2) | ((v[1] & 0x30) >> 4);
120 data.new_pal[i].green = ((v[1] & 0x0f) << 4) | ((v[2] & 0x3c) >> 2);
121 data.new_pal[i].blue = ((v[2] & 0x03) << 6) | v[3] ;
124 r = perform_action_1picture (
126 0, 0, 0, 0, 0, 0, //x y f w h f
133 *err = CONVERT_FAILED;
137 r = save_picture(0, argv[argi+1], flags);
147 int palette_remap (ILuint n, struct PixelInfo *p, void *data)
149 struct remap_data *d = data;
152 // i = p->index / d->n1;
153 j = p->index % d->n1;
155 p->red = d->new_pal[j].red;
156 p->green = d->new_pal[j].green;
157 p->blue = d->new_pal[j].blue;
162 ILubyte b642d(char c)
164 if(c >='A' && c <='Z')
166 else if(c >= 'a' && c <= 'z')
168 else if(c >= '0' && c <= '9')
179 //#include <stdlib.h>
184 int mustard(const char *t,int m,int e);
185 int main(int argc, char *argv[]);
186 unsigned char b642d(char b64);
191 int main(int argc, char *argv[])
197 return mustard("remapt-1 inpix outpix palette [q]",0,1);
201 if(strlen(argv[3])<340)
202 return mustard("Palette too short.",0,1);
206 ilEnable(IL_ORIGIN_SET);
207 ilEnable(IL_FILE_OVERWRITE);
209 ilGenImages(1,&inpix);
212 if(!ilLoadImage(argv[1]))
213 return mustard("inpix load fail.",1,1);
215 if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
216 return mustard("inpix not indexed.",1,1);
219 ilConvertPal(IL_PAL_RGB24);
220 if(ilGetInteger(IL_PALETTE_NUM_COLS)!=255)
221 return mustard("Wrong number of colors.",1,1);
225 if(strlen(argv[3])<340)
226 return mustard("Palette too short.",1,1);
230 argv[3][i]=b642d(argv[3][i]);
233 pal[3*i ]=( argv[3][4*i ] <<2)|
234 ((argv[3][4*i+1]&0x30) >>4);
236 pal[3*i+1]=((argv[3][4*i+1]&0x0f) <<4)|
237 ((argv[3][4*i+2]&0x3c) >>2);
239 pal[3*i+2]=((argv[3][4*i+2]&0x03) <<6)|
242 pal[3*i+255]=pal[3*i ];
243 pal[3*i+256]=pal[3*i+1];
244 pal[3*i+257]=pal[3*i+2];
246 pal[3*i+510]=pal[3*i ];
247 pal[3*i+511]=pal[3*i+1];
248 pal[3*i+512]=pal[3*i+2];
250 if(!ilSave(IL_PNG,argv[2]))
251 return mustard("outpix save fail",1,1);
253 return mustard("remapped",1,0);
256 unsigned char b642d(char b64)
258 if(b64 >='A' && b64 <='Z')
260 else if(b64 >= 'a' && b64 <= 'z')
262 else if(b64 >= '0' && b64 <= '9')
272 int mustard(const char *t, int m,int e)
279 ilDeleteImages(1,&inpix);