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