]> bicyclesonthemoon.info Git - ott/enhance/blob - remap_t_1.c
reveal online
[ott/enhance] / remap_t_1.c
1 /*
2 remap_t_1.c
3 The tool to enhance t-1 frames by remapping the palette
4 02.12.2022
5
6 Copyright (C) 2015, 2022  Balthasar SzczepaƄski
7
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.
12
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.
17
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/>.
20
21
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)
26
27 */
28
29 #include <stdint.h>
30 #include <getopt.h>
31 #include <errno.h>
32
33 #include "core.h"
34 #include "remap_t_1.h"
35
36 #define N0  3
37 #define N1 85
38
39 struct remap_data
40 {
41         ILuint n0;
42         ILuint n1;
43         ILuint n;
44         struct ColorInfo new_pal[0x100];
45 };
46
47 int palette_remap (ILuint n, struct PixelInfo *p, void *data);
48 ILubyte b642d(char c);
49
50 char REMAPT1_MISSING_ARGS[] = "Missing parameters.\npremap_t_1 inPix outPix palette [n1 n2]\n";
51
52 int subtool_remap_t_1 (int argc, char **argv, int argi, char **err)
53 {
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;
57         ILuint i, j, k;
58         ILubyte v[4];
59         uint_fast8_t eot = 0;
60         int r;
61         
62         if (argc < argi + 3)
63         {
64                 *err = REMAPT1_MISSING_ARGS;
65                 return EINVAL;
66         }
67         
68         if (argc >= argi + 5)
69         {
70                 sscanf(argv[argi+3],"%u",&(data.n0));
71                 sscanf(argv[argi+4],"%u",&(data.n1));
72         }
73         else
74         {
75                 data.n0 = N0;
76                 data.n1 = N1;
77         }
78         data.n = data.n0 * data.n1;
79         if (data.n>=0x100)
80         {
81                 *err = BAD_PALETTE_SIZE;
82                 return EINVAL;
83         }
84         
85         r = reserve_pictures(1);
86         if (r)
87         {
88                 *err = CREATE_FAILED;
89                 return r;
90         }
91         
92         r = load_picture(0, argv[argi], &info, &flags);
93         if (r)
94         {
95                 *err = LOAD_FAILED;
96                 return r;
97         }
98         
99         if (info.palette_num_cols != data.n)
100         {
101                 *err = BAD_PALETTE_SIZE;
102                 return EINVAL;
103         }
104         
105         for (i=0, k=0; i<data.n1; ++i)
106         {
107                 for(j=0; j<4; ++j, ++k)
108                 {
109                         v[j] = argv[argi+2][k];
110                         if (v[j] == '\0')
111                         {
112                                 eot = 1;
113                                 break;
114                         }
115                         v[j] = b642d(v[j]);
116                 }
117                 if (eot)
118                         break;
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]              ;
122         }
123         
124         r = perform_action_1picture (
125                 0, //id
126                 0, 0, 0, 0, 0, 0, //x y f w h f
127                 &palette_remap,
128                 flags,
129                 &data
130         );
131         if (r)
132         {
133                 *err = CONVERT_FAILED;
134                 return r;
135         }
136         
137         r = save_picture(0, argv[argi+1], flags);
138         if (r!=0)
139         {
140                 *err = SAVE_FAILED;
141                 return r;
142         }
143         
144         return 0;
145 }
146
147 int palette_remap (ILuint n, struct PixelInfo *p, void *data)
148 {
149         struct remap_data *d = data;
150         ILuint j;
151         
152         // i = p->index / d->n1;
153         j = p->index % d->n1;
154         
155         p->red   = d->new_pal[j].red;
156         p->green = d->new_pal[j].green;
157         p->blue  = d->new_pal[j].blue;
158         
159         return 0;
160 }
161
162 ILubyte b642d(char c)
163 {
164         if(c >='A' && c <='Z')
165                 return c-'A';
166         else if(c >= 'a' && c <= 'z')
167                 return c-'a'+26;
168         else if(c >= '0' && c <= '9')
169                 return c-'0'+52;
170         else if(c=='+')
171                 return 62;
172         else if(c=='-')
173                 return 63;
174         else
175                 return 0;
176 }
177
178 /*
179 //#include <stdlib.h>
180 #include <string.h>
181 #include <stdio.h>
182 #include "IL/il.h"
183
184 int mustard(const char *t,int m,int e);
185 int main(int argc, char *argv[]);
186 unsigned char b642d(char b64);
187
188 ILuint inpix;
189 unsigned char q =0;
190
191 int main(int argc, char *argv[])
192 {
193         ILubyte *pal;
194         unsigned short i;
195         
196         if (argc<4)
197                 return mustard("remapt-1 inpix outpix palette [q]",0,1);
198   else if(argc > 4)
199                 q=1;
200         
201         if(strlen(argv[3])<340)
202                 return mustard("Palette too short.",0,1);
203         
204         ilInit();
205         
206         ilEnable(IL_ORIGIN_SET);
207         ilEnable(IL_FILE_OVERWRITE);
208         
209         ilGenImages(1,&inpix);
210         ilBindImage(inpix);
211         
212         if(!ilLoadImage(argv[1]))
213                 return mustard("inpix load fail.",1,1);
214         
215         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
216                 return mustard("inpix not indexed.",1,1);
217         
218         
219         ilConvertPal(IL_PAL_RGB24);
220         if(ilGetInteger(IL_PALETTE_NUM_COLS)!=255)
221                 return mustard("Wrong number of colors.",1,1);
222         
223         pal=ilGetPalette();
224         
225         if(strlen(argv[3])<340)
226                 return mustard("Palette too short.",1,1);
227         
228         
229         for(i=0; i<340; ++i)
230                 argv[3][i]=b642d(argv[3][i]);
231         for(i=0; i<85; ++i)
232         {
233                 pal[3*i  ]=( argv[3][4*i  ]       <<2)|
234                            ((argv[3][4*i+1]&0x30) >>4);
235                 
236                 pal[3*i+1]=((argv[3][4*i+1]&0x0f) <<4)|
237                            ((argv[3][4*i+2]&0x3c) >>2);
238                 
239                 pal[3*i+2]=((argv[3][4*i+2]&0x03) <<6)|
240                            ( argv[3][4*i+3]          );
241                 
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];
245                 
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];
249         }
250         if(!ilSave(IL_PNG,argv[2]))
251                 return mustard("outpix save fail",1,1);
252         
253         return mustard("remapped",1,0);
254 }
255
256 unsigned char b642d(char b64)
257 {
258         if(b64 >='A' && b64 <='Z')
259                 return b64-'A';
260         else if(b64 >= 'a' && b64 <= 'z')
261                 return b64-'a'+26;
262         else if(b64 >= '0' && b64 <= '9')
263                 return b64-'0'+52;
264         else if(b64=='+')
265                 return 62;
266         else if(b64=='-')
267                 return 63;
268         else
269                 return 0;
270 }
271
272 int mustard(const char *t, int m,int e)
273 {
274         if(!q)
275                 puts(t);
276         switch (m)
277         {
278         case 1:
279                 ilDeleteImages(1,&inpix);
280         case 0:
281         default:
282                 return e;
283         }
284 }
285 */