]> bicyclesonthemoon.info Git - ott/enhance/blob - remapt-1.c
08b7c1582830adf3a2146a6519c0dfb38bc2dc5f
[ott/enhance] / remapt-1.c
1 // remapt-1.c
2 // The tool to enhance t-1 frames by remapping the palette
3 // 16.09.2015
4 // 
5 // Copyright (C) 2015  Balthasar SzczepaƄski
6 // 
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
11 // 
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU Affero General Public License for more details.
16 // 
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 // 
20
21 // Requires Dev Image Library (libdevil) (http://openil.sourceforge.net/)
22 // on Pentium III libdevil must be recompiled with
23 // --disable-ssl2 --disable-ssl3
24 // (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572954)
25
26 //#include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "IL/il.h"
30
31 int mustard(const char *t,int m,int e);
32 int main(int argc, char *argv[]);
33 unsigned char b642d(char b64);
34
35 ILuint inpix;
36 unsigned char q =0;
37
38 int main(int argc, char *argv[])
39 {
40         ILubyte *pal;
41         unsigned short i;
42         
43         if (argc<4)
44                 return mustard("remapt-1 inpix outpix palette [q]",0,1);
45   else if(argc > 4)
46                 q=1;
47         
48         if(strlen(argv[3])<340)
49                 return mustard("Palette too short.",0,1);
50         
51         ilInit();
52         
53         ilEnable(IL_ORIGIN_SET);
54         ilEnable(IL_FILE_OVERWRITE);
55         
56         ilGenImages(1,&inpix);
57         ilBindImage(inpix);
58         
59         if(!ilLoadImage(argv[1]))
60                 return mustard("inpix load fail.",1,1);
61         
62         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
63                 return mustard("inpix not indexed.",1,1);
64         
65         
66         ilConvertPal(IL_PAL_RGB24);
67         if(ilGetInteger(IL_PALETTE_NUM_COLS)!=255)
68                 return mustard("Wrong number of colors.",1,1);
69         
70         pal=ilGetPalette();
71         
72         if(strlen(argv[3])<340)
73                 return mustard("Palette too short.",1,1);
74         
75         
76         for(i=0; i<340; ++i)
77                 argv[3][i]=b642d(argv[3][i]);
78         for(i=0; i<85; ++i)
79         {
80                 pal[3*i  ]=( argv[3][4*i  ]       <<2)|
81                            ((argv[3][4*i+1]&0x30) >>4);
82                 
83                 pal[3*i+1]=((argv[3][4*i+1]&0x0f) <<4)|
84                            ((argv[3][4*i+2]&0x3c) >>2);
85                 
86                 pal[3*i+2]=((argv[3][4*i+2]&0x03) <<6)|
87                            ( argv[3][4*i+3]          );
88                 
89                 pal[3*i+255]=pal[3*i  ];
90                 pal[3*i+256]=pal[3*i+1];
91                 pal[3*i+257]=pal[3*i+2];
92                 
93                 pal[3*i+510]=pal[3*i  ];
94                 pal[3*i+511]=pal[3*i+1];
95                 pal[3*i+512]=pal[3*i+2];
96         }
97         if(!ilSave(IL_PNG,argv[2]))
98                 return mustard("outpix save fail",1,1);
99         
100         return mustard("remapped",1,0);
101 }
102
103 unsigned char b642d(char b64)
104 {
105         if(b64 >='A' && b64 <='Z')
106                 return b64-'A';
107         else if(b64 >= 'a' && b64 <= 'z')
108                 return b64-'a'+26;
109         else if(b64 >= '0' && b64 <= '9')
110                 return b64-'0'+52;
111         else if(b64=='+')
112                 return 62;
113         else if(b64=='-')
114                 return 63;
115         else
116                 return 0;
117 }
118
119 int mustard(const char *t, int m,int e)
120 {
121         if(!q)
122                 puts(t);
123         switch (m)
124         {
125         case 1:
126                 ilDeleteImages(1,&inpix);
127         case 0:
128         default:
129                 return e;
130         }
131 }