]> bicyclesonthemoon.info Git - ott/enhance/blob - remap_t_1.c
T-1 remap tool. debug needed!
[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[3] & 0x03) << 6) |   v[3]              ;
122                 ++data.n1;
123         }
124         
125         r = perform_action_1picture (
126                 0, //id
127                 0, 0, 0, 0, 0, 0, //x y f w h f
128                 &palette_remap,
129                 flags,
130                 &data
131         );
132         if (r)
133         {
134                 *err = CONVERT_FAILED;
135                 return r;
136         }
137         
138         r = save_picture(0, argv[argi+1], flags);
139         if (r!=0)
140         {
141                 *err = SAVE_FAILED;
142                 return r;
143         }
144         
145         return 0;
146 }
147
148 int palette_remap (ILuint n, struct PixelInfo *p, void *data)
149 {
150         struct remap_data *d = data;
151         ILuint j;
152         
153         // i = p->index / d->n1;
154         j = p->index % d->n1;
155         
156         p->red   = d->new_pal[j].red;
157         p->green = d->new_pal[j].green;
158         p->blue  = d->new_pal[j].blue;
159         
160         return 0;
161 }
162
163 ILubyte b642d(char c)
164 {
165         if(c >='A' && c <='Z')
166                 return c-'A';
167         else if(c >= 'a' && c <= 'z')
168                 return c-'a'+26;
169         else if(c >= '0' && c <= '9')
170                 return c-'0'+52;
171         else if(c=='+')
172                 return 62;
173         else if(c=='-')
174                 return 63;
175         else
176                 return 0;
177 }
178
179 /*
180 //#include <stdlib.h>
181 #include <string.h>
182 #include <stdio.h>
183 #include "IL/il.h"
184
185 int mustard(const char *t,int m,int e);
186 int main(int argc, char *argv[]);
187 unsigned char b642d(char b64);
188
189 ILuint inpix;
190 unsigned char q =0;
191
192 int main(int argc, char *argv[])
193 {
194         ILubyte *pal;
195         unsigned short i;
196         
197         if (argc<4)
198                 return mustard("remapt-1 inpix outpix palette [q]",0,1);
199   else if(argc > 4)
200                 q=1;
201         
202         if(strlen(argv[3])<340)
203                 return mustard("Palette too short.",0,1);
204         
205         ilInit();
206         
207         ilEnable(IL_ORIGIN_SET);
208         ilEnable(IL_FILE_OVERWRITE);
209         
210         ilGenImages(1,&inpix);
211         ilBindImage(inpix);
212         
213         if(!ilLoadImage(argv[1]))
214                 return mustard("inpix load fail.",1,1);
215         
216         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
217                 return mustard("inpix not indexed.",1,1);
218         
219         
220         ilConvertPal(IL_PAL_RGB24);
221         if(ilGetInteger(IL_PALETTE_NUM_COLS)!=255)
222                 return mustard("Wrong number of colors.",1,1);
223         
224         pal=ilGetPalette();
225         
226         if(strlen(argv[3])<340)
227                 return mustard("Palette too short.",1,1);
228         
229         
230         for(i=0; i<340; ++i)
231                 argv[3][i]=b642d(argv[3][i]);
232         for(i=0; i<85; ++i)
233         {
234                 pal[3*i  ]=( argv[3][4*i  ]       <<2)|
235                            ((argv[3][4*i+1]&0x30) >>4);
236                 
237                 pal[3*i+1]=((argv[3][4*i+1]&0x0f) <<4)|
238                            ((argv[3][4*i+2]&0x3c) >>2);
239                 
240                 pal[3*i+2]=((argv[3][4*i+2]&0x03) <<6)|
241                            ( argv[3][4*i+3]          );
242                 
243                 pal[3*i+255]=pal[3*i  ];
244                 pal[3*i+256]=pal[3*i+1];
245                 pal[3*i+257]=pal[3*i+2];
246                 
247                 pal[3*i+510]=pal[3*i  ];
248                 pal[3*i+511]=pal[3*i+1];
249                 pal[3*i+512]=pal[3*i+2];
250         }
251         if(!ilSave(IL_PNG,argv[2]))
252                 return mustard("outpix save fail",1,1);
253         
254         return mustard("remapped",1,0);
255 }
256
257 unsigned char b642d(char b64)
258 {
259         if(b64 >='A' && b64 <='Z')
260                 return b64-'A';
261         else if(b64 >= 'a' && b64 <= 'z')
262                 return b64-'a'+26;
263         else if(b64 >= '0' && b64 <= '9')
264                 return b64-'0'+52;
265         else if(b64=='+')
266                 return 62;
267         else if(b64=='-')
268                 return 63;
269         else
270                 return 0;
271 }
272
273 int mustard(const char *t, int m,int e)
274 {
275         if(!q)
276                 puts(t);
277         switch (m)
278         {
279         case 1:
280                 ilDeleteImages(1,&inpix);
281         case 0:
282         default:
283                 return e;
284         }
285 }
286 */