]> granicus.if.org Git - imagemagick/blob - coders/stegano.c
https://github.com/ImageMagick/ImageMagick/issues/631
[imagemagick] / coders / stegano.c
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-2017 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://www.imagemagick.org/script/license.php                           %
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 \f
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 \f
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 {
93 #define GetBit(alpha,i) (((size_t) (alpha) >> (size_t) (i)) & 0x01)
94 #define SetBit(i,set) SetPixelIndex(image,(Quantum) ((set) != 0 ? \
95   (size_t) GetPixelIndex(image,q) | (one << (size_t) (i)) : \
96   (size_t) GetPixelIndex(image,q) & ~(one << (size_t) (i))),q)
97
98   Image
99     *image,
100     *watermark;
101
102   ImageInfo
103     *read_info;
104
105   int
106     c;
107
108   MagickBooleanType
109     status;
110
111   PixelInfo
112     pixel;
113
114   register Quantum
115     *q;
116
117   register ssize_t
118     x;
119
120   size_t
121     depth,
122     one;
123
124   ssize_t
125     i,
126     j,
127     k,
128     y;
129
130   /*
131     Initialize Image structure.
132   */
133   assert(image_info != (const ImageInfo *) NULL);
134   assert(image_info->signature == MagickCoreSignature);
135   if (image_info->debug != MagickFalse)
136     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
137       image_info->filename);
138   assert(exception != (ExceptionInfo *) NULL);
139   assert(exception->signature == MagickCoreSignature);
140   one=1;
141   image=AcquireImage(image_info,exception);
142   if ((image->columns == 0) || (image->rows == 0))
143     ThrowReaderException(OptionError,"MustSpecifyImageSize");
144   read_info=CloneImageInfo(image_info);
145   SetImageInfoBlob(read_info,(void *) NULL,0);
146   *read_info->magick='\0';
147   watermark=ReadImage(read_info,exception);
148   read_info=DestroyImageInfo(read_info);
149   if (watermark == (Image *) NULL)
150     return((Image *) NULL);
151   watermark->depth=MAGICKCORE_QUANTUM_DEPTH;
152   if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse)
153     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
154   if (image_info->ping != MagickFalse)
155     {
156       (void) CloseBlob(image);
157       return(GetFirstImageInList(image));
158     }
159   status=SetImageExtent(image,image->columns,image->rows,exception);
160   if (status == MagickFalse)
161     return(DestroyImageList(image));
162   for (y=0; y < (ssize_t) image->rows; y++)
163   {
164     q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
165     if (q == (Quantum *) NULL)
166       break;
167     for (x=0; x < (ssize_t) image->columns; x++)
168     {
169       SetPixelIndex(image,0,q);
170       q+=GetPixelChannels(image);
171     }
172     if (SyncAuthenticPixels(image,exception) == MagickFalse)
173       break;
174   }
175   /*
176     Get hidden watermark from low-order bits of image.
177   */
178   c=0;
179   i=0;
180   j=0;
181   i=(ssize_t) (watermark->depth-1);
182   depth=watermark->depth;
183   for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--)
184   {
185     for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++)
186     {
187       x=0;
188       for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++)
189       {
190         if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows)
191           break;
192         (void) GetOneVirtualPixelInfo(watermark,UndefinedVirtualPixelMethod,
193           k % (ssize_t) watermark->columns,k/(ssize_t) watermark->columns,
194           &pixel,exception);
195         q=GetAuthenticPixels(image,x,y,1,1,exception);
196         if (q == (Quantum *) NULL)
197           break;
198         switch (c)
199         {
200           case 0:
201           {
202             SetBit(i,GetBit(pixel.red,j));
203             break;
204           }
205           case 1:
206           {
207             SetBit(i,GetBit(pixel.green,j));
208             break;
209           }
210           case 2:
211           {
212             SetBit(i,GetBit(pixel.blue,j));
213             break;
214           }
215         }
216         if (SyncAuthenticPixels(image,exception) == MagickFalse)
217           break;
218         c++;
219         if (c == 3)
220           c=0;
221         k++;
222         if (k == (ssize_t) (watermark->columns*watermark->columns))
223           k=0;
224         if (k == image->offset)
225           j++;
226       }
227     }
228     status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth);
229     if (status == MagickFalse)
230       break;
231   }
232   watermark=DestroyImage(watermark);
233   (void) SyncImage(image,exception);
234   return(GetFirstImageInList(image));
235 }
236 \f
237 /*
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 %                                                                             %
240 %                                                                             %
241 %                                                                             %
242 %   R e g i s t e r S T E G A N O I m a g e                                   %
243 %                                                                             %
244 %                                                                             %
245 %                                                                             %
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 %
248 %  RegisterSTEGANOImage() adds attributes for the STEGANO image format to
249 %  the list of supported formats.  The attributes include the image format
250 %  tag, a method to read and/or write the format, whether the format
251 %  supports the saving of more than one frame to the same file or blob,
252 %  whether the format supports native in-memory I/O, and a brief
253 %  description of the format.
254 %
255 %  The format of the RegisterSTEGANOImage method is:
256 %
257 %      size_t RegisterSTEGANOImage(void)
258 %
259 */
260 ModuleExport size_t RegisterSTEGANOImage(void)
261 {
262   MagickInfo
263     *entry;
264
265   entry=AcquireMagickInfo("STEGANO","STEGANO","Steganographic image");
266   entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage;
267   entry->format_type=ImplicitFormatType;
268   (void) RegisterMagickInfo(entry);
269   return(MagickImageCoderSignature);
270 }
271 \f
272 /*
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 %                                                                             %
275 %                                                                             %
276 %                                                                             %
277 %   U n r e g i s t e r S T E G A N O I m a g e                               %
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 %
283 %  UnregisterSTEGANOImage() removes format registrations made by the
284 %  STEGANO module from the list of supported formats.
285 %
286 %  The format of the UnregisterSTEGANOImage method is:
287 %
288 %      UnregisterSTEGANOImage(void)
289 %
290 */
291 ModuleExport void UnregisterSTEGANOImage(void)
292 {
293   (void) UnregisterMagickInfo("STEGANO");
294 }