]> bicyclesonthemoon.info Git - ott/enhance/blob - info.c
fix some pointer & stuff
[ott/enhance] / info.c
1 /*
2 info.c
3 Get information
4 05.11.2022
5
6 Copyright (C) 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 <inttypes.h>
31 #include <errno.h>
32
33 #include "core.h"
34 #include "info.h"
35
36 char INFO_MISSING_ARGS[] = "Missing parameters.\ninfo inPix\n";
37
38 void print_info_uint (char *name, long unsigned value)
39 {
40         fprintf(stdout, "%s: %lu\n", name, value);
41 }
42 void print_info_bool (char *name, long unsigned value)
43 {
44         fprintf(stdout, "%s: %s\n", name, value?"TRUE":"FALSE");
45 }
46 void print_info_datatype (char *name, long unsigned value)
47 {
48         fprintf(stdout,"%s: ",name);
49         switch (value)
50         {
51         case IL_BYTE:
52                 fputs("BYTE", stdout);
53                 break;
54         case IL_DOUBLE:
55                 fputs("DOUBLE", stdout);
56                 break;
57         case IL_FLOAT:
58                 fputs("FLOAT", stdout);
59                 break;
60         case IL_INT:
61                 fputs("INT", stdout);
62                 break;
63         case IL_SHORT:
64                 fputs("SHORT", stdout);
65                 break;
66         case IL_UNSIGNED_INT:
67                 fputs("UNSIGNED INT", stdout);
68                 break;
69         case IL_UNSIGNED_BYTE:
70                 fputs("UNSIGNED BYTE", stdout);
71                 break;
72         case IL_UNSIGNED_SHORT:
73                 fputs("UNSIGNED SHORT", stdout);
74                 break;
75         default:
76                 fprintf(stdout, "? %ld", value);
77                 break;
78         }
79         fputs("\n", stdout);
80 }
81
82 void print_info_dataformat (char *name, long unsigned value)
83 {
84         fprintf(stdout,"%s: ",name);
85         switch (value)
86         {
87         case IL_BGR:
88                 fputs("BGR", stdout);
89                 break;
90         case IL_BGRA:
91                 fputs("BGRA", stdout);
92                 break;
93         case IL_COLOUR_INDEX:
94                 fputs("COLOUR INDEX", stdout);
95                 break;
96         case IL_LUMINANCE:
97                 fputs("LUMINANCE", stdout);
98                 break;
99         case IL_LUMINANCE_ALPHA:
100                 fputs("LUMINANCE ALPHA", stdout);
101                 break;
102         case IL_RGB:
103                 fputs("RGB", stdout);
104                 break;
105         case IL_RGBA:
106                 fputs("RGBA", stdout);
107                 break;
108         default:
109                 fprintf(stdout, "? %ld", value);
110                 break;
111         }
112         fputs("\n", stdout);
113 }
114
115 void print_info_originmode (char *name, long unsigned value)
116 {
117         fprintf(stdout,"%s: ",name);
118         switch (value)
119         {
120         case IL_ORIGIN_LOWER_LEFT:
121                 fputs("LOWER LEFT", stdout);
122                 break;
123         case IL_ORIGIN_UPPER_LEFT:
124                 fputs("UPPER LEFT", stdout);
125                 break;
126         default:
127                 fprintf(stdout, "? %ld", value);
128                 break;
129         }
130         fputs("\n", stdout);
131 }
132
133 void print_info_palettetype (char *name, long unsigned value)
134 {
135         fprintf(stdout,"%s: ",name);
136         switch (value)
137         {
138         case IL_PAL_BGR24:
139                 fputs("BGR24", stdout);
140                 break;
141         case IL_PAL_BGR32:
142                 fputs("BGR32", stdout);
143                 break;
144         case IL_PAL_BGRA32:
145                 fputs("BGRA32", stdout);
146                 break;
147         case IL_PAL_RGB24:
148                 fputs("RGB24", stdout);
149                 break;
150         case IL_PAL_RGB32:
151                 fputs("RGB32", stdout);
152                 break;
153         case IL_PAL_RGBA32:
154                 fputs("RGBA32", stdout);
155                 break;
156         default:
157                 fprintf(stdout, "? %ld", value);
158                 break;
159         }
160         fputs("\n", stdout);
161 }
162
163 int info (int argc, char **argv, char **err)
164 {
165         int r;
166         int i;
167         struct IL_full_info info;
168         
169         if (argc<1)
170         {
171                 *err = INFO_MISSING_ARGS;
172                 return EINVAL;
173         }
174         
175         r = create_pictures(1);
176         if (r)
177         {
178                 *err = CREATE_FAILED;
179                 return r;
180         }
181         
182         for (i=0; i<argc; ++i)
183         {
184                 fprintf(stdout,"\n>>> %s <<<\n",argv[i]);
185                 r = load_picture(0, argv[i],
186                         OK_PALETTE_ONLY | CAN_BE_MULTIPLE | CAN_BE_OVER_8BIT);
187                 if (r)
188                 {
189                         fputs("FAIL\n",stdout);
190                         continue;
191                 }
192                 
193                 get_info(0, &info);
194                 print_info_uint       ("active_image",          info.active_image);
195                 print_info_uint       ("active_layer",          info.active_layer);
196                 print_info_uint       ("active_mipmap",         info.active_mipmap);
197                 print_info_bool       ("conv_pal",              info.conv_pal);
198                 print_info_uint       ("cur_image",             info.cur_image);
199                 print_info_bool       ("file_mode",             info.file_mode);
200                 print_info_dataformat ("format_mode",           info.format_mode);
201                 print_info_bool       ("format_set",            info.format_set);
202                 print_info_uint       ("image_bits_per_pixel",  info.image_bits_per_pixel);
203                 print_info_uint       ("image_bytes_per_pixel", info.image_bytes_per_pixel);
204                 print_info_dataformat ("image_format",          info.image_format);
205                 print_info_uint       ("image_height",          info.image_height);
206                 print_info_datatype   ("image_type",            info.image_type);
207                 print_info_uint       ("image_width",           info.image_width);
208                 print_info_uint       ("num_images",            info.num_images);
209                 print_info_uint       ("num_mipmaps",           info.num_mipmaps);
210                 print_info_originmode ("origin_mode",           info.origin_mode);
211                 print_info_bool       ("origin_set",            info.origin_set);
212                 print_info_uint       ("palette_bpp",           info.palette_bpp);
213                 print_info_uint       ("palette_num_cols",      info.palette_num_cols);
214                 print_info_palettetype("palette_type",          info.palette_type);
215                 print_info_datatype   ("type_mode",             info.type_mode);
216                 print_info_bool       ("type_set",              info.type_set);
217                 print_info_bool       ("use_key_colour",        info.use_key_colour);
218                 print_info_uint       ("version_num",           info.palette_num_cols);
219         }
220         return 0;
221 }
222
223 /*
224
225 #define INPIX_MUSTARD 1
226 #define OUTPIX_MUSTARD 2
227
228 #define ARGUMENT_MUSTARD 4
229
230 #define ANIMATED_MUSTARD 6
231 #define FAIL 900
232 #define OK 0
233
234 #define CR newdata[4*(i+inX*j)+0]
235 #define CG newdata[4*(i+inX*j)+1]
236 #define CB newdata[4*(i+inX*j)+2]
237
238 #define RA ((long)(*ra))
239 #define RB ((long)(*rb))
240 #define GA ((long)(*ga))
241 #define GB ((long)(*gb))
242 #define BA ((long)(*ba))
243 #define BB ((long)(*bb))
244
245 #include <stdlib.h>
246 #include <stdio.h>
247 #include <IL/il.h>
248
249 void mustard(int mustard);
250
251 ILubyte enhance(ILubyte x, long r, long s, long t);
252 int main (int argc, const char *argv[]);
253
254 ILuint pix;
255 ILboolean q=true;
256 ILboolean pixOpen=false;
257 ILboolean allchannels=false;
258
259 ILubyte enhance(ILubyte x, long A, long B)
260 {
261         if(A==B)
262                 return x;
263         long y=(((long)x)-A)*255/(B-A);
264         return (ILubyte)((y>255)?255:((y<0)?0:y));
265 }
266
267 int main (int argc, const char *argv[])
268 {
269         ILubyte rav, rbv, gav, gbv, bav, bbv;
270         ILubyte *ra, *rb, *ga, *gb, *ba, *bb;
271         ILuint inX, inY, frame;
272         ILubyte *newdata;
273         
274         if(argc<3)
275                 mustard(ARGUMENT_MUSTARD);
276         if (argc>=4)
277         {
278                 sscanf(argv[3],"%u",&frame);
279         }
280         else
281                 frame=0;
282         if (argc>=5)
283         {
284                 if (argv[4][0]=='q' || argv[4][0]=='Q')
285                         q=false;
286         }
287         if (argc>=6)
288         {
289                 if (argv[5][0]=='a' || argv[5][0]=='A')
290                         allchannels=true;
291         }
292         
293         ra=&rav;
294         rb=&rbv;
295         if(allchannels)
296         {
297                 ga=&gav;
298                 gb=&gbv;
299                 ba=&bav;
300                 bb=&bbv;
301         }
302         else
303         {
304                 ga=&rav;
305                 gb=&rbv;
306                 ba=&rav;
307                 bb=&rbv;
308         }
309         
310         ilInit();
311         if(!ilEnable(IL_ORIGIN_SET))mustard(FAIL);
312         if(!ilEnable(IL_FILE_OVERWRITE))mustard(FAIL);
313         ilClearColour(255,255,0,0);
314         ilGenImages(1, &pix);
315         pixOpen=true;
316         ilBindImage(pix);
317         if(!ilLoadImage(argv[1]))mustard(INPIX_MUSTARD);
318         if(!ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE))mustard(INPIX_MUSTARD);
319         
320         inX=ilGetInteger(IL_IMAGE_WIDTH);
321         inY=ilGetInteger(IL_IMAGE_HEIGHT);
322         if(ilGetInteger(IL_NUM_IMAGES)>1)
323                 mustard(ANIMATED_MUSTARD);
324         
325         newdata=ilGetData();
326         
327         *ra=255;
328         *ga=255;
329         *ba=255;
330         *rb=0;
331         *gb=0;
332         *bb=0;
333         
334         for(unsigned long i=frame;i<inX-frame;++i)
335         {
336                 for(unsigned long j=frame;j<inY-frame;++j)
337                 {
338                         if(CR<*ra)
339                                 *ra=CR;
340                         if(CR>*rb)
341                                 *rb=CR;
342                         if(CG<*ga)
343                                 *ga=CG;
344                         if(CG>*gb)
345                                 *gb=CG;
346                         if(CB<*ba)
347                                 *ba=CB;
348                         if(CB>*bb)
349                                 *bb=CB;
350                 }
351         }
352                         
353         for(unsigned long i=frame;i<inX-frame;++i)
354         {
355                 for(unsigned long j=frame;j<inY-frame;++j)
356                 {
357                         
358                         CR=enhance(CR,RA,RB);
359                         CG=enhance(CG,GA,GB);
360                         CB=enhance(CB,BA,BB);
361                 }
362         }
363         if(!ilSave(IL_PNG,argv[2]))mustard(OUTPIX_MUSTARD);
364         // no mustard
365         mustard(0);
366 }
367
368 void mustard(int mustard)
369 {
370         switch(mustard)
371         {
372         case 0:
373                 if(q) printf("ENHANCED!\n");break;
374         case ARGUMENT_MUSTARD:
375                 if(q) printf("nofadig inPix outPix [framesize] [q] [a]\n");break;
376         case INPIX_MUSTARD:
377                 if(q) printf("inPIX mustard.\n");break;
378         case OUTPIX_MUSTARD:
379                 if(q) printf("outPIX mustard.\n");break;
380         case ANIMATED_MUSTARD:
381                 if(q) printf("Animation is mustard.\n");break;
382         default:
383                 if (q) printf("Ch*rpin* mustard mustaard!\n");
384         }
385         if(pixOpen)
386                 ilDeleteImages(1, &pix);
387         exit(mustard);
388 }
389 */