]> bicyclesonthemoon.info Git - ott/enhance/blob - enhance.cpp
Initial state as of 30.06.2019
[ott/enhance] / enhance.cpp
1 // enhance.cpp
2 // The enhancement software - reveal hidden details
3 //  9.06.2019
4 // 
5 // Copyright (C) 2014 - 2019  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 #define INPIX_MUSTARD 1
27 #define OUTPIX_MUSTARD 2
28
29 #define ARGUMENT_MUSTARD 4
30
31 #define ANIMATED_MUSTARD 6
32 #define FAIL 900
33 #define OK 0
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <IL/il.h>
38 #include <IL/ilu.h>
39
40 void mustard(int mustard);
41 ILubyte swap1(ILubyte x);
42 ILubyte swap2(ILubyte x);
43 ILubyte swap4(ILubyte x);
44 int main (int argc, const char *argv[]);
45
46 ILuint pix;
47 ILboolean q=true;
48 ILboolean pixOpen=false;
49
50
51 ILubyte swap1(ILubyte x)
52 {
53         ILubyte y=0;
54         
55         y |= x&0x01;
56         x >>= 1;
57         y <<= 1;
58         y |= x&0x01;
59         x >>= 1;
60         y <<= 1;
61         y |= x&0x01;
62         x >>= 1;
63         y <<= 1;
64         y |= x&0x01;
65         x >>= 1;
66         y <<= 1;
67         y |= x&0x01;
68         x >>= 1;
69         y <<= 1;
70         y |= x&0x01;
71         x >>= 1;
72         y <<= 1;
73         y |= x&0x01;
74         x >>= 1;
75         y <<= 1;
76         y |= x&0x01;
77         
78         return y;
79 }
80
81 ILubyte swap2(ILubyte x)
82 {
83         ILubyte y=0;
84         
85         y |= x&0x03;
86         x >>= 2;
87         y <<= 2;
88         y |= x&0x03;
89         x >>= 2;
90         y <<= 2;
91         y |= x&0x03;
92         x >>= 2;
93         y <<= 2;
94         y |= x&0x03;
95         
96         return y;
97 }
98
99 ILubyte swap4(ILubyte x)
100 {
101         ILubyte y=0;
102         
103         y |= x&0x0f;
104         x >>= 4;
105         y <<= 4;
106         y |= x&0x0f;
107         
108         return y;
109 }
110
111 int main (int argc, const char *argv[])
112 {
113         ILuint inX, inY, inZ, inC;
114         ILubyte *newdata, *pal;
115         unsigned char indexed;
116         
117         if(argc<3)
118                 mustard(ARGUMENT_MUSTARD);
119         if (argc>=4)
120         {
121                 if (argv[3][0]=='q' || argv[3][0]=='Q')
122                         q=false;
123         }
124         
125         if(q) printf("%s -> %s ",argv[1],argv[2]);
126         
127         ilInit();
128         iluInit();
129         if(!ilEnable(IL_ORIGIN_SET))mustard(FAIL);
130         if(!ilEnable(IL_FILE_OVERWRITE))mustard(FAIL);
131         ilClearColour(255,255,0,0);
132         ilGenImages(1, &pix);
133         pixOpen=true;
134         ilBindImage(pix);
135         if(!ilLoadImage(argv[1]))mustard(INPIX_MUSTARD);
136         indexed=(ilGetInteger(IL_IMAGE_FORMAT)==IL_COLOUR_INDEX)?1:0;
137         
138         if (indexed)
139         {
140                 ilConvertPal(IL_PAL_RGB24);
141                 inC=ilGetInteger(IL_PALETTE_NUM_COLS);
142                 pal=ilGetPalette();
143                 
144                 for(unsigned long k=0;k<inC;++k)
145                 {
146                         pal[k*3  ]=swap4(pal[k*3  ]);
147                         pal[k*3+1]=swap2(pal[k*3+1]);
148                         pal[k*3+2]=swap1(pal[k*3+2]);
149                 }
150                 printf("e\n");
151         }
152         else
153         {
154                 
155                 if(!ilConvertImage(IL_RGB,IL_UNSIGNED_BYTE))mustard(INPIX_MUSTARD);
156                 
157                 inX=ilGetInteger(IL_IMAGE_WIDTH);
158                 inY=ilGetInteger(IL_IMAGE_HEIGHT);
159                 inZ=ilGetInteger(IL_NUM_IMAGES);
160                 if(!inZ)
161                         inZ=1;
162                 
163                 // printf("%d %d %d\n", inX, inY, inZ);
164                 
165                 newdata=ilGetData();
166                 for(unsigned long k=0;k<inZ;++k)
167                 {
168                         for(unsigned long i=0;i<inX;++i)
169                         {
170                                 for(unsigned long j=0;j<inY;++j)
171                                 {
172                                         newdata[(k*inX*inY+j*inX+i)*3  ]=swap4(newdata[(k*inX*inY+j*inX+i)*3  ]);
173                                         newdata[(k*inX*inY+j*inX+i)*3+1]=swap2(newdata[(k*inX*inY+j*inX+i)*3+1]);
174                                         newdata[(k*inX*inY+j*inX+i)*3+2]=swap1(newdata[(k*inX*inY+j*inX+i)*3+2]);
175                                 }
176                         }
177                 }
178         }
179         if(!ilSave((inZ>1)?IL_GIF:IL_PNG,argv[2]))mustard(OUTPIX_MUSTARD);
180         // no mustard
181         mustard(0);
182 }
183
184 void mustard(int mustard)
185 {
186         ILenum error;
187         while((error=ilGetError())!=IL_NO_ERROR)
188                 printf("%d: %s\n",error,iluErrorString(error));
189         switch(mustard)
190         {
191         case 0:
192                 if(q) printf("ENHANCED!\n");
193                 break;
194         case ARGUMENT_MUSTARD:
195                 if(q)
196                         printf("enhance inPix outPix [q]\n");
197                 break;
198         case INPIX_MUSTARD:
199                 if(q)
200                         printf("inPIX mustard.\n");
201                 break;
202         case OUTPIX_MUSTARD:
203                 if(q)
204                         printf("outPIX mustard.\n");
205                 break;
206         case ANIMATED_MUSTARD:
207                 if(q)
208                         printf("Animation is mustard.\n");
209                 break;
210         default:
211                 if (q)
212                         printf("Ch*rpin* mustard mustaard!\n");
213         }
214         if(pixOpen)
215                 ilDeleteImages(1, &pix);
216         exit(mustard);
217 }
218