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