]> bicyclesonthemoon.info Git - ott/enhance/blob - bluenh.cpp
more towards general action
[ott/enhance] / bluenh.cpp
1     //  //bluenhance
2    //  // ENHANCE the bluepix!
3   //  //  
4  //  //   Requires Dev Image Library,
5 //  //  //on Pentium III libdevil must be
6    //  // recompiled with --disable-ssl2 --disable-ssl3
7   //  //
8  //  //   ~~bicyclesonthemoon
9
10 #define INPIX_MUSTARD 1
11 #define OUTPIX_MUSTARD 2
12
13 #define ARGUMENT_MUSTARD 4
14
15 #define ANIMATED_MUSTARD 6
16 #define FAIL 900
17 #define OK 0
18
19 #define CR newdata[4*(i+inX*j)+0]
20 #define CG newdata[4*(i+inX*j)+1]
21 #define CB newdata[4*(i+inX*j)+2]
22
23 #define A ((long)(a))
24 #define B ((long)(b))
25 #define C ((long)(c))
26 #define D ((long)(d))
27 #define E ((long)(e))
28 #define F ((long)(f))
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <IL/il.h>
33
34 void mustard(int mustard);
35 ILubyte enhance(ILubyte x, long r, long s, long t);
36 ILubyte enhance2(ILubyte x, ILubyte a, ILubyte b, ILubyte c, ILubyte d, ILubyte e, ILubyte f);
37 ILubyte enhance3(ILubyte x, ILubyte a, ILubyte b, ILubyte c);
38 int main (int argc, const char *argv[]);
39
40 ILuint pix;
41 ILboolean q=true;
42 ILboolean pixOpen=false;
43
44
45 ILubyte enhance(ILubyte x, long r, long s, long t)
46 {
47         long y=(r*x+t)/s;
48         return (ILubyte)((y>255)?255:((y<0)?0:y));
49 }
50
51 ILubyte enhance2(ILubyte x, ILubyte a, ILubyte b, ILubyte c, ILubyte d, ILubyte e, ILubyte f)
52 {
53         return ((x<=a)?d:((x>=c)?f:((x>b)?(enhance(x,F-E,C-B,E*C-B*F)):((x<b)?(enhance(x,E-D,B-A,D*B-A*E)):e))));
54 }
55
56 ILubyte enhance3(ILubyte x, ILubyte a, ILubyte b, ILubyte c)
57 {
58         return enhance(x,C-A,B-A,(B-C)*A);
59 }
60
61 int main (int argc, const char *argv[])
62 {
63         ILubyte a,b,c,d,e,f,g;
64         ILubyte *h, *s, *l;
65         ILuint inX, inY;
66         ILubyte *newdata;
67         
68         if(argc<9)
69                 mustard(ARGUMENT_MUSTARD);
70         if (argc>=10)
71         {
72                 if (argv[9][0]=='q' || argv[9][0]=='Q')
73                         q=false;
74         }
75         
76         sscanf(argv[3],"%hhu",&a);
77         sscanf(argv[4],"%hhu",&b);
78         sscanf(argv[5],"%hhu",&c);
79         sscanf(argv[6],"%hhu",&d);
80         sscanf(argv[7],"%hhu",&e);
81         sscanf(argv[8],"%hhu",&f);
82         
83         ilInit();
84         if(!ilEnable(IL_ORIGIN_SET))mustard(FAIL);
85         if(!ilEnable(IL_FILE_OVERWRITE))mustard(FAIL);
86         ilClearColour(255,255,0,0);
87         ilGenImages(1, &pix);
88         pixOpen=true;
89         ilBindImage(pix);
90         if(!ilLoadImage(argv[1]))mustard(INPIX_MUSTARD);
91         if(!ilConvertImage(IL_RGBA,IL_UNSIGNED_BYTE))mustard(INPIX_MUSTARD);
92         
93         inX=ilGetInteger(IL_IMAGE_WIDTH);
94         inY=ilGetInteger(IL_IMAGE_HEIGHT);
95         if(ilGetInteger(IL_NUM_IMAGES)>1)
96                 mustard(ANIMATED_MUSTARD);
97         
98         newdata=ilGetData();
99         for(unsigned long i=0;i<inX;++i)
100         {
101                 for(unsigned long j=0;j<inY;++j)
102                 {
103                         if(CR==CG&&CG==CB)
104                                 continue;
105                         //it'll stilll work correctly if two are equal.
106                         if(CR>CG)
107                         {
108                                 if(CG>CB)
109                                 {
110                                         h=&(CR);
111                                         s=&(CG);
112                                         l=&(CB);
113                                 }
114                                 else if(CB>CR)
115                                 {
116                                         h=&(CB);
117                                         s=&(CR);
118                                         l=&(CG);
119                                 }
120                                 else
121                                 {
122                                         h=&(CR);
123                                         s=&(CB);
124                                         l=&(CG);
125                                 }
126                         }
127                         else
128                         {
129                                 if(CR>CB)
130                                 {
131                                         h=&(CG);
132                                         s=&(CR);
133                                         l=&(CB);
134                                 }
135                                 else if(CB>CG)
136                                 {
137                                         h=&(CB);
138                                         s=&(CG);
139                                         l=&(CR);
140                                 }
141                                 else
142                                 {
143                                         h=&(CG);
144                                         s=&(CB);
145                                         l=&(CR);
146                                 }
147                         }
148                         g=*h;
149                         *h=enhance2(*h,a,b,c,d,e,f);
150                         *s=enhance3(*s,*l,g,*h);
151                 }
152         }
153         if(!ilSave(IL_PNG,argv[2]))mustard(OUTPIX_MUSTARD);
154         // no mustard
155         mustard(0);
156 }
157
158 void mustard(int mustard)
159 {
160         switch(mustard)
161         {
162         case 0:
163                 if(q) printf("ENHANCED!\n");break;
164         case ARGUMENT_MUSTARD:
165                 if(q) printf("bluenhanced inPix outPix a b c d e f [q]\n");break;
166         case INPIX_MUSTARD:
167                 if(q) printf("inPIX mustard.\n");break;
168         case OUTPIX_MUSTARD:
169                 if(q) printf("outPIX mustard.\n");break;
170         case ANIMATED_MUSTARD:
171                 if(q) printf("Animation is mustard.\n");break;
172         default:
173                 if (q) printf("Ch*rpin* mustard mustaard!\n");
174         }
175         if(pixOpen)
176                 ilDeleteImages(1, &pix);
177         exit(mustard);
178 }
179