]> bicyclesonthemoon.info Git - ott/enhance/blob - insert.c
perform action on multiple pictures at once. NOT FINISHED!
[ott/enhance] / insert.c
1 // insert.c
2 // The tool to hide two indexed images iside one
3 // 25.06.2015
4 // 
5 // Copyright (C) 2015  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 #include <stdlib.h>
27 #include <stdio.h>
28 #include "IL/il.h"
29
30 int mustard(const char *t,int m,int e);
31 int main(int argc, char *argv[]);
32
33 ILuint inpix1, inpix2, outpix;
34 unsigned char q =0;
35
36
37 int main(int argc, char *argv[])
38 {
39         
40         ILubyte *pal1, *pal2, *data1, *data2, *data;
41         ILubyte pal[256*3];
42         unsigned short i,j;
43         unsigned long k;
44         ILuint col1, col2, col,  x1, x2, y1, y2;
45         
46         unsigned short p1, p2, p3;
47         
48         if (argc<4)
49                 return mustard("insert inpix1 inpix2 outpix [p1 p2]",0,1);
50         if(argc>=5)
51                 p1=atoi(argv[4]);
52         else
53                 p1=1;
54         if(argc>=6)
55                 p2=atoi(argv[5]);
56         else
57                 p2=0;
58         if(argc>=7)
59                 q=1;
60         p3=p1+p2;
61         
62         ilInit();
63                 
64         ilEnable(IL_ORIGIN_SET);
65         ilEnable(IL_FILE_OVERWRITE);
66         
67         ilGenImages(1,&inpix1);
68         ilGenImages(1,&inpix2);
69         ilGenImages(1,&outpix);
70         
71         ilBindImage(inpix1);
72         if(!ilLoadImage(argv[1]))
73                 return mustard("inpix1 load fail.",1,1);
74         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
75                 return mustard("inpix1 not indexed.",1,1);
76         ilConvertPal(IL_PAL_RGB24);
77         col1=ilGetInteger(IL_PALETTE_NUM_COLS);
78         if(col1>16)
79                 return mustard("inpix1 too many colors.",1,1);
80         x1=ilGetInteger(IL_IMAGE_WIDTH);
81         y1=ilGetInteger(IL_IMAGE_HEIGHT);
82         pal1=ilGetPalette();
83         data1=ilGetData();
84         
85         ilBindImage(inpix2);
86         if(!ilLoadImage(argv[2]))
87                 return mustard("inpix2 load fail.",1,1);
88         if(ilGetInteger(IL_IMAGE_FORMAT)!=IL_COLOUR_INDEX)
89                 return mustard("inpix2 not indexed.",1,1);
90         ilConvertPal(IL_PAL_RGB24);
91         col2=ilGetInteger(IL_PALETTE_NUM_COLS);
92         if(col2>16)
93                 return mustard("inpix2 too many colors.",1,1);
94         if(col1!=col2)
95                 return mustard("different palette size.",1,1);
96         x2=ilGetInteger(IL_IMAGE_WIDTH);
97         if(x1!=x2)
98                 return mustard("different widths",1,1);
99         y2=ilGetInteger(IL_IMAGE_HEIGHT);
100         if(y1!=y2)
101                 return mustard("different heights",1,1);
102         pal2=ilGetPalette();
103         data2=ilGetData();
104         col=col1*col2;
105         
106         for(i=0;i<col1;++i)
107         {
108                 for(j=0;j<col2;++j)
109                 {
110                         pal[3*(i*col1+j)  ]=(pal1[3*i  ]*p1+pal2[3*j  ]*p2)/p3;
111                         pal[3*(i*col1+j)+1]=(pal1[3*i+1]*p1+pal2[3*j+1]*p2)/p3;
112                         pal[3*(i*col1+j)+2]=(pal1[3*i+2]*p1+pal2[3*j+2]*p2)/p3;
113                 }
114         }
115         
116         ilBindImage(outpix);
117         if(!ilTexImage(x1,y1,1,1,IL_COLOUR_INDEX,IL_UNSIGNED_BYTE,NULL))
118                 return mustard ("outpix create fail.",1,1);
119         ilRegisterPal(pal,col*3,IL_PAL_RGB24);
120         data=ilGetData();
121         
122         for(k=0;k<x1*y1;++k)
123                 data[k]=data1[k]*col2+data2[k];
124         
125         if(!ilSave(IL_PNG,argv[3]))
126                 return mustard("outpix save fail",1,1);
127                                 
128         return mustard("Ok",1,0);
129         
130         
131 }
132 int mustard(const char *t, int m,int e)
133 {
134         if(!q)puts(t);
135         switch (m)
136   {
137   case 1:
138         ilDeleteImages(1,&inpix1);
139                 ilDeleteImages(1,&inpix2);
140                 ilDeleteImages(1,&outpix);
141   case 0:
142   default:
143                 return e;
144         }
145 }
146