]> bicyclesonthemoon.info Git - ott/enhance/blob - extract.c
build new picture
[ott/enhance] / extract.c
1 // extract.c\r
2 // The tool to extract two images hidden iside one indexed image\r
3 // 25.06.2015\r
4 // \r
5 // Copyright (C) 2015  Balthasar SzczepaƄski\r
6 // \r
7 // This program is free software: you can redistribute it and/or modify\r
8 // it under the terms of the GNU Affero General Public License as\r
9 // published by the Free Software Foundation, either version 3 of the\r
10 // License, or (at your option) any later version.\r
11 // \r
12 // This program is distributed in the hope that it will be useful,\r
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 // GNU Affero General Public License for more details.\r
16 // \r
17 // You should have received a copy of the GNU Affero General Public License\r
18 // along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
19 // \r
20 \r
21 // Requires Dev Image Library (libdevil) (http://openil.sourceforge.net/)\r
22 // on Pentium III libdevil must be recompiled with\r
23 // --disable-ssl2 --disable-ssl3\r
24 // (https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=572954)\r
25 \r
26 #include <stdlib.h>\r
27 #include <stdio.h>\r
28 #include "IL/il.h"\r
29 \r
30 int mustard(const char *t,int m,int e);\r
31 int main(int argc, char *argv[]);\r
32 //unsigned short psqrt(unsigned short s);\r
33 unsigned short tsqrt(unsigned short s);\r
34 \r
35 ILuint inpix, outpix1, outpix2;\r
36 unsigned char q =0;\r
37 \r
38 \r
39 \r
40 int main(int argc, char *argv[])\r
41 {\r
42         \r
43         ILubyte *pal, *data1, *data2, *data;\r
44         ILubyte pal12[16*3];\r
45         unsigned short i;\r
46         unsigned long k;\r
47         ILuint col12, col,  x, y;\r
48         \r
49         if (argc<4)\r
50                 return mustard("extract inpix outpix1 outpix2 [q]",0,1);\r
51         else if (argc>4)\r
52                 q=1;\r
53         ilInit();\r
54                 \r
55         ilEnable(IL_ORIGIN_SET);\r
56         ilEnable(IL_FILE_OVERWRITE);\r
57         \r
58         ilGenImages(1,&inpix);\r
59         ilGenImages(1,&outpix1);\r
60         ilGenImages(1,&outpix2);\r
61         \r
62         ilBindImage(inpix);\r
63         if(!ilLoadImage(argv[1]))\r
64                 return mustard("inpix load fail.",1,1);\r
65         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)\r
66                 return mustard("inpix not indexed.",1,1);\r
67         ilConvertPal(IL_PAL_RGB24);\r
68         col=ilGetInteger(IL_PALETTE_NUM_COLS);\r
69         col12=tsqrt(col);\r
70         if(!col12)\r
71                 return mustard("Palette is not squarish.",1,1);\r
72         x=ilGetInteger(IL_IMAGE_WIDTH);\r
73         y=ilGetInteger(IL_IMAGE_HEIGHT);\r
74         pal=ilGetPalette();\r
75         data=ilGetData();\r
76         \r
77         for(i=0;i<col12;++i)\r
78         {\r
79                 pal12[3*i  ]=pal[3*i*(col12+1)  ];\r
80                 pal12[3*i+1]=pal[3*i*(col12+1)+1];\r
81                 pal12[3*i+2]=pal[3*i*(col12+1)+2];\r
82         }\r
83         \r
84         ilBindImage(outpix1);\r
85         if(!ilTexImage(x,y,1,1,IL_COLOUR_INDEX,IL_UNSIGNED_BYTE,NULL))\r
86                 return mustard ("outpix1 create fail.",1,1);\r
87         ilRegisterPal(pal12,col12*3,IL_PAL_RGB24);\r
88         data1=ilGetData();\r
89         \r
90         ilBindImage(outpix2);\r
91         if(!ilTexImage(x,y,1,1,IL_COLOUR_INDEX,IL_UNSIGNED_BYTE,NULL))\r
92                 return mustard ("outpix2 create fail.",1,1);\r
93         ilRegisterPal(pal12,col12*3,IL_PAL_RGB24);\r
94         data2=ilGetData();\r
95         \r
96         for(k=0;k<x*y;++k)\r
97         {\r
98                 data1[k]=data[k]/col12;\r
99                 data2[k]=data[k]%col12;\r
100         }\r
101         \r
102         if(!ilSave(IL_PNG,argv[3]))\r
103                 return mustard("outpix2 save fail",1,1);\r
104         ilBindImage(outpix1);              \r
105         if(!ilSave(IL_PNG,argv[2]))\r
106                 return mustard("outpix1 save fail",1,1);\r
107                                 \r
108         return mustard("Ok",1,0);\r
109         \r
110         \r
111 }\r
112 \r
113 int mustard(const char *t, int m,int e)\r
114 {\r
115         if(!q)puts(t);\r
116         switch (m)\r
117   {\r
118   case 1:\r
119         ilDeleteImages(1,&inpix);\r
120                 ilDeleteImages(1,&outpix1);\r
121                 ilDeleteImages(1,&outpix2);\r
122   case 0:\r
123   default:\r
124                 return e;\r
125         }\r
126 }\r
127 \r
128 unsigned short tsqrt(unsigned short s)\r
129 {\r
130         switch(s)\r
131         {\r
132         case 256:\r
133                 return 16;\r
134         case 225:\r
135                 return 15;\r
136         case 196:\r
137                 return 14;\r
138         case 169:\r
139                 return 13;\r
140         case 144:\r
141                 return 12;\r
142         case 121:\r
143                 return 11;\r
144         case 100:\r
145                 return 10;\r
146         case 81:\r
147                 return 9;\r
148         case 64:\r
149                 return 8;\r
150         case 49:\r
151                 return 7;\r
152         case 36:\r
153                 return 6;\r
154         case 25:\r
155                 return 5;\r
156         case 16:\r
157                 return 4;\r
158         case 9:\r
159                 return 3;\r
160         case 4:\r
161                 return 2;\r
162         case 1:\r
163                 return 1;\r
164         default:\r
165                 return 0;\r
166         }\r
167 }       \r
168 //unsigned short isqrt(unsigned s)\r
169 //{\r
170 //      unsigned short r;\r
171 //      unsigned short b=0x0040;\r
172 //      \r
173 //      while(b>s)\r
174 //            b>>=2;\r
175 //      while(b)\r
176 //      {\r
177 //            if(s>=r+b)\r
178 //            {\r
179 //                    s-=r+b;\r
180 //                    r=(r>>1)+b;\r
181 //            }\r
182 //            else\r
183 //                    r>>1;\r
184 //            b>>2;\r
185 //      }\r
186 //      return r;\r
187 //}\r