]> bicyclesonthemoon.info Git - ott/enhance/blob - reveal.c
fix index generating
[ott/enhance] / reveal.c
1 /*
2 reveal.c
3 reveal hidden details
4 29.11.2022
5
6 Copyright (C) 2014 - 2019, 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 <stdio.h>
30 #include <errno.h>
31
32 #include "reveal.h"
33 #include "core.h"
34
35 char REVEAL_MISSING_ARGS[] = "Missing parameters.\nreveal inPix outPix\n";
36
37 int reveal (ILuint n, struct PixelInfo *p, void *data);
38 static inline ILubyte reveal_swap1(ILubyte x);
39 static inline ILubyte reveal_swap2(ILubyte x);
40 static inline ILubyte reveal_swap4(ILubyte x);
41
42
43 int subtool_reveal (int argc, char **argv, int argi, char **err)
44 {
45         struct IL_full_info info;
46         FLAG_TYPE flags = CAN_BE_MULTIPLE | OK_PALETTE_ONLY;
47         int r;
48         
49         if (argc < argi + 2)
50         {
51                 *err = REVEAL_MISSING_ARGS;
52                 return EINVAL;
53         }
54         
55         r = reserve_pictures(1);
56         if (r)
57         {
58                 *err = CREATE_FAILED;
59                 return r;
60         }
61         
62         r = load_picture(0, argv[argi], &info, &flags);
63         if (r)
64         {
65                 *err = LOAD_FAILED;
66                 return r;
67         }
68         
69         r = perform_action_1picture (
70                 0,
71                 0, 0, 0, 0, 0, 0,
72                 &reveal,
73                 flags,
74                 NULL
75         );
76         if (r)
77         {
78                 *err = CONVERT_FAILED;
79                 return r;
80         }
81         
82         r = save_picture (0, argv[argi+1], flags);
83         if (r)
84         {
85                 *err = SAVE_FAILED;
86                 return r;
87         }
88         
89         return 0;
90 }
91
92 int reveal (ILuint n, struct PixelInfo *p, void *data)
93 {
94         if (p->flags & EFF_GRAY)
95                 p->value = reveal_swap2(p->value);
96         else
97         {
98                 p->red   = reveal_swap4(p->red);
99                 p->green = reveal_swap2(p->green);
100                 p->blue  = reveal_swap1(p->blue);
101         }
102         
103         return 0;
104 }
105
106 static inline ILubyte reveal_swap1(ILubyte x)
107 {
108         ILubyte y = 0x00;
109         
110         y |= x&0x01;
111         x >>= 1;
112         y <<= 1;
113         y |= x&0x01;
114         x >>= 1;
115         y <<= 1;
116         y |= x&0x01;
117         x >>= 1;
118         y <<= 1;
119         y |= x&0x01;
120         x >>= 1;
121         y <<= 1;
122         y |= x&0x01;
123         x >>= 1;
124         y <<= 1;
125         y |= x&0x01;
126         x >>= 1;
127         y <<= 1;
128         y |= x&0x01;
129         x >>= 1;
130         y <<= 1;
131         y |= x&0x01;
132         
133         return y;
134 }
135
136 static inline ILubyte reveal_swap2(ILubyte x)
137 {
138         ILubyte y = 0x00;
139         
140         y |= x&0x03;
141         x >>= 2;
142         y <<= 2;
143         y |= x&0x03;
144         x >>= 2;
145         y <<= 2;
146         y |= x&0x03;
147         x >>= 2;
148         y <<= 2;
149         y |= x&0x03;
150         
151         return y;
152 }
153
154 static inline ILubyte reveal_swap4(ILubyte x)
155 {
156         ILubyte y = 0x00;
157         
158         y |= x&0x0f;
159         x >>= 4;
160         y <<= 4;
161         y |= x&0x0f;
162         
163         return y;
164 }