/src/imagemagick/coders/stegano.c
Line | Count | Source |
1 | | /* |
2 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
3 | | % % |
4 | | % % |
5 | | % % |
6 | | % SSSSS TTTTT EEEEE GGGG AAA N N OOO % |
7 | | % SS T E G A A NN N O O % |
8 | | % SSS T EEE G GG AAAAA N N N O O % |
9 | | % SS T E G G A A N NN O O % |
10 | | % SSSSS T EEEEE GGG A A N N OOO % |
11 | | % % |
12 | | % % |
13 | | % Write A Steganographic Image. % |
14 | | % % |
15 | | % Software Design % |
16 | | % Cristy % |
17 | | % July 1992 % |
18 | | % % |
19 | | % % |
20 | | % Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization % |
21 | | % dedicated to making software imaging solutions freely available. % |
22 | | % % |
23 | | % You may not use this file except in compliance with the License. You may % |
24 | | % obtain a copy of the License at % |
25 | | % % |
26 | | % https://imagemagick.org/license/ % |
27 | | % % |
28 | | % Unless required by applicable law or agreed to in writing, software % |
29 | | % distributed under the License is distributed on an "AS IS" BASIS, % |
30 | | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
31 | | % See the License for the specific language governing permissions and % |
32 | | % limitations under the License. % |
33 | | % % |
34 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
35 | | % |
36 | | % |
37 | | */ |
38 | | |
39 | | /* |
40 | | Include declarations. |
41 | | */ |
42 | | #include "MagickCore/studio.h" |
43 | | #include "MagickCore/blob.h" |
44 | | #include "MagickCore/blob-private.h" |
45 | | #include "MagickCore/cache.h" |
46 | | #include "MagickCore/colormap.h" |
47 | | #include "MagickCore/constitute.h" |
48 | | #include "MagickCore/exception.h" |
49 | | #include "MagickCore/exception-private.h" |
50 | | #include "MagickCore/image.h" |
51 | | #include "MagickCore/image-private.h" |
52 | | #include "MagickCore/list.h" |
53 | | #include "MagickCore/magick.h" |
54 | | #include "MagickCore/memory_.h" |
55 | | #include "MagickCore/monitor.h" |
56 | | #include "MagickCore/monitor-private.h" |
57 | | #include "MagickCore/pixel-accessor.h" |
58 | | #include "MagickCore/quantum-private.h" |
59 | | #include "MagickCore/static.h" |
60 | | #include "MagickCore/string_.h" |
61 | | #include "MagickCore/module.h" |
62 | | |
63 | | /* |
64 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
65 | | % % |
66 | | % % |
67 | | % % |
68 | | % R e a d S T E G A N O I m a g e % |
69 | | % % |
70 | | % % |
71 | | % % |
72 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
73 | | % |
74 | | % ReadSTEGANOImage() reads a steganographic image hidden within another |
75 | | % image type. It allocates the memory necessary for the new Image structure |
76 | | % and returns a pointer to the new image. |
77 | | % |
78 | | % The format of the ReadSTEGANOImage method is: |
79 | | % |
80 | | % Image *ReadSTEGANOImage(const ImageInfo *image_info, |
81 | | % ExceptionInfo *exception) |
82 | | % |
83 | | % A description of each parameter follows: |
84 | | % |
85 | | % o image_info: the image info. |
86 | | % |
87 | | % o exception: return any errors or warnings in this structure. |
88 | | % |
89 | | */ |
90 | | static Image *ReadSTEGANOImage(const ImageInfo *image_info, |
91 | | ExceptionInfo *exception) |
92 | 26 | { |
93 | 26 | #define GetBit(alpha,i) (((size_t) (alpha) >> (size_t) (i)) & 0x01) |
94 | 26 | #define SetBit(i,set) SetPixelIndex(image,(Quantum) ((set) != 0 ? \ |
95 | 0 | (size_t) GetPixelIndex(image,q) | (one << (size_t) (i)) : \ |
96 | 0 | (size_t) GetPixelIndex(image,q) & ~(one << (size_t) (i))),q) |
97 | | |
98 | 26 | Image |
99 | 26 | *image, |
100 | 26 | *watermark; |
101 | | |
102 | 26 | ImageInfo |
103 | 26 | *read_info; |
104 | | |
105 | 26 | int |
106 | 26 | c; |
107 | | |
108 | 26 | MagickBooleanType |
109 | 26 | status; |
110 | | |
111 | 26 | PixelInfo |
112 | 26 | pixel; |
113 | | |
114 | 26 | Quantum |
115 | 26 | *q; |
116 | | |
117 | 26 | ssize_t |
118 | 26 | x; |
119 | | |
120 | 26 | size_t |
121 | 26 | depth, |
122 | 26 | one; |
123 | | |
124 | 26 | ssize_t |
125 | 26 | i, |
126 | 26 | j, |
127 | 26 | k, |
128 | 26 | y; |
129 | | |
130 | | /* |
131 | | Initialize Image structure. |
132 | | */ |
133 | 26 | assert(image_info != (const ImageInfo *) NULL); |
134 | 26 | assert(image_info->signature == MagickCoreSignature); |
135 | 26 | assert(exception != (ExceptionInfo *) NULL); |
136 | 26 | assert(exception->signature == MagickCoreSignature); |
137 | 26 | if (IsEventLogging() != MagickFalse) |
138 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
139 | 0 | image_info->filename); |
140 | 26 | one=1; |
141 | 26 | image=AcquireImage(image_info,exception); |
142 | 26 | if ((image->columns == 0) || (image->rows == 0)) |
143 | 26 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
144 | 0 | read_info=CloneImageInfo(image_info); |
145 | 0 | SetImageInfoBlob(read_info,(void *) NULL,0); |
146 | 0 | *read_info->magick='\0'; |
147 | 0 | watermark=ReadImage(read_info,exception); |
148 | 0 | read_info=DestroyImageInfo(read_info); |
149 | 0 | if (watermark == (Image *) NULL) |
150 | 0 | return(DestroyImage(image)); |
151 | 0 | watermark->depth=MAGICKCORE_QUANTUM_DEPTH; |
152 | 0 | if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse) |
153 | 0 | { |
154 | 0 | watermark=DestroyImage(watermark); |
155 | 0 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
156 | 0 | } |
157 | 0 | if (image_info->ping != MagickFalse) |
158 | 0 | { |
159 | 0 | watermark=DestroyImage(watermark); |
160 | 0 | (void) CloseBlob(image); |
161 | 0 | return(GetFirstImageInList(image)); |
162 | 0 | } |
163 | 0 | status=SetImageExtent(image,image->columns,image->rows,exception); |
164 | 0 | if (status == MagickFalse) |
165 | 0 | { |
166 | 0 | watermark=DestroyImage(watermark); |
167 | 0 | return(DestroyImageList(image)); |
168 | 0 | } |
169 | 0 | for (y=0; y < (ssize_t) image->rows; y++) |
170 | 0 | { |
171 | 0 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
172 | 0 | if (q == (Quantum *) NULL) |
173 | 0 | break; |
174 | 0 | for (x=0; x < (ssize_t) image->columns; x++) |
175 | 0 | { |
176 | 0 | SetPixelIndex(image,0,q); |
177 | 0 | q+=(ptrdiff_t) GetPixelChannels(image); |
178 | 0 | } |
179 | 0 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
180 | 0 | break; |
181 | 0 | } |
182 | | /* |
183 | | Get hidden watermark from low-order bits of image. |
184 | | */ |
185 | 0 | c=0; |
186 | 0 | i=0; |
187 | 0 | j=0; |
188 | 0 | i=(ssize_t) (watermark->depth-1); |
189 | 0 | depth=watermark->depth; |
190 | 0 | for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--) |
191 | 0 | { |
192 | 0 | for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++) |
193 | 0 | { |
194 | 0 | x=0; |
195 | 0 | for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++) |
196 | 0 | { |
197 | 0 | if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows) |
198 | 0 | break; |
199 | 0 | (void) GetOneVirtualPixelInfo(watermark,UndefinedVirtualPixelMethod, |
200 | 0 | k % (ssize_t) watermark->columns,k/(ssize_t) watermark->columns, |
201 | 0 | &pixel,exception); |
202 | 0 | q=GetAuthenticPixels(image,x,y,1,1,exception); |
203 | 0 | if (q == (Quantum *) NULL) |
204 | 0 | break; |
205 | 0 | switch (c) |
206 | 0 | { |
207 | 0 | case 0: |
208 | 0 | { |
209 | 0 | SetBit(i,GetBit(pixel.red,j)); |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | case 1: |
213 | 0 | { |
214 | 0 | SetBit(i,GetBit(pixel.green,j)); |
215 | 0 | break; |
216 | 0 | } |
217 | 0 | case 2: |
218 | 0 | { |
219 | 0 | SetBit(i,GetBit(pixel.blue,j)); |
220 | 0 | break; |
221 | 0 | } |
222 | 0 | } |
223 | 0 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
224 | 0 | break; |
225 | 0 | c++; |
226 | 0 | if (c == 3) |
227 | 0 | c=0; |
228 | 0 | k++; |
229 | 0 | if (k == (ssize_t) (watermark->columns*watermark->rows)) |
230 | 0 | k=0; |
231 | 0 | if (k == image->offset) |
232 | 0 | j++; |
233 | 0 | } |
234 | 0 | } |
235 | 0 | status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth); |
236 | 0 | if (status == MagickFalse) |
237 | 0 | break; |
238 | 0 | } |
239 | 0 | watermark=DestroyImage(watermark); |
240 | 0 | (void) SyncImage(image,exception); |
241 | 0 | return(GetFirstImageInList(image)); |
242 | 0 | } |
243 | | |
244 | | /* |
245 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
246 | | % % |
247 | | % % |
248 | | % % |
249 | | % R e g i s t e r S T E G A N O I m a g e % |
250 | | % % |
251 | | % % |
252 | | % % |
253 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
254 | | % |
255 | | % RegisterSTEGANOImage() adds attributes for the STEGANO image format to |
256 | | % the list of supported formats. The attributes include the image format |
257 | | % tag, a method to read and/or write the format, whether the format |
258 | | % supports the saving of more than one frame to the same file or blob, |
259 | | % whether the format supports native in-memory I/O, and a brief |
260 | | % description of the format. |
261 | | % |
262 | | % The format of the RegisterSTEGANOImage method is: |
263 | | % |
264 | | % size_t RegisterSTEGANOImage(void) |
265 | | % |
266 | | */ |
267 | | ModuleExport size_t RegisterSTEGANOImage(void) |
268 | 8 | { |
269 | 8 | MagickInfo |
270 | 8 | *entry; |
271 | | |
272 | 8 | entry=AcquireMagickInfo("STEGANO","STEGANO","Steganographic image"); |
273 | 8 | entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage; |
274 | 8 | entry->format_type=ImplicitFormatType; |
275 | 8 | (void) RegisterMagickInfo(entry); |
276 | 8 | return(MagickImageCoderSignature); |
277 | 8 | } |
278 | | |
279 | | /* |
280 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
281 | | % % |
282 | | % % |
283 | | % % |
284 | | % U n r e g i s t e r S T E G A N O I m a g e % |
285 | | % % |
286 | | % % |
287 | | % % |
288 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
289 | | % |
290 | | % UnregisterSTEGANOImage() removes format registrations made by the |
291 | | % STEGANO module from the list of supported formats. |
292 | | % |
293 | | % The format of the UnregisterSTEGANOImage method is: |
294 | | % |
295 | | % UnregisterSTEGANOImage(void) |
296 | | % |
297 | | */ |
298 | | ModuleExport void UnregisterSTEGANOImage(void) |
299 | 0 | { |
300 | 0 | (void) UnregisterMagickInfo("STEGANO"); |
301 | 0 | } |