]> 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 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/colormap.h"
47 #include "magick/constitute.h"
48 #include "magick/exception.h"
49 #include "magick/exception-private.h"
50 #include "magick/image.h"
51 #include "magick/image-private.h"
52 #include "magick/list.h"
53 #include "magick/magick.h"
54 #include "magick/memory_.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 \f
62 /*
63 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64 %                                                                             %
65 %                                                                             %
66 %                                                                             %
67 %   R e a d S T E G A N O I m a g e                                           %
68 %                                                                             %
69 %                                                                             %
70 %                                                                             %
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %
73 %  ReadSTEGANOImage() reads a steganographic image hidden within another
74 %  image type.  It allocates the memory necessary for the new Image structure
75 %  and returns a pointer to the new image.
76 %
77 %  The format of the ReadSTEGANOImage method is:
78 %
79 %      Image *ReadSTEGANOImage(const ImageInfo *image_info,
80 %        ExceptionInfo *exception)
81 %
82 %  A description of each parameter follows:
83 %
84 %    o image_info: the image info.
85 %
86 %    o exception: return any errors or warnings in this structure.
87 %
88 */
89
90 static inline size_t MagickMin(const size_t x,
91   const size_t y)
92 {
93   if (x < y)
94     return(x);
95   return(y);
96 }
97
98 static Image *ReadSTEGANOImage(const ImageInfo *image_info,
99   ExceptionInfo *exception)
100 {
101 #define GetBit(alpha,i) MagickMin((((size_t) (alpha) >> (size_t) \
102   (i)) & 0x01),16)
103 #define SetBit(alpha,i,set) (alpha)=(IndexPacket) ((set) != 0 ? \
104   (size_t) (alpha) | (one << (size_t) (i)) : (size_t) \
105   (alpha) & ~(one << (size_t) (i)))
106
107   Image
108     *image,
109     *watermark;
110
111   ImageInfo
112     *read_info;
113
114   int
115     c;
116
117   MagickBooleanType
118     status;
119
120   PixelPacket
121     pixel;
122
123   register IndexPacket
124     *indexes;
125
126   register PixelPacket
127     *q;
128
129   register ssize_t
130     x;
131
132   size_t
133     depth,
134     one;
135
136   ssize_t
137     i,
138     j,
139     k,
140     y;
141
142   /*
143     Initialize Image structure.
144   */
145   assert(image_info != (const ImageInfo *) NULL);
146   assert(image_info->signature == MagickSignature);
147   if (image_info->debug != MagickFalse)
148     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
149       image_info->filename);
150   assert(exception != (ExceptionInfo *) NULL);
151   assert(exception->signature == MagickSignature);
152   one=1;
153   image=AcquireImage(image_info);
154   if ((image->columns == 0) || (image->rows == 0))
155     ThrowReaderException(OptionError,"MustSpecifyImageSize");
156   read_info=CloneImageInfo(image_info);
157   SetImageInfoBlob(read_info,(void *) NULL,0);
158   *read_info->magick='\0';
159   watermark=ReadImage(read_info,exception);
160   read_info=DestroyImageInfo(read_info);
161   if (watermark == (Image *) NULL)
162     return((Image *) NULL);
163   watermark->depth=MAGICKCORE_QUANTUM_DEPTH;
164   if (AcquireImageColormap(image,MaxColormapSize) == MagickFalse)
165     ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
166   if (image_info->ping != MagickFalse)
167     {
168       (void) CloseBlob(image);
169       return(GetFirstImageInList(image));
170     }
171   /*
172     Get hidden watermark from low-order bits of image.
173   */
174   c=0;
175   i=0;
176   j=0;
177   i=(ssize_t) (watermark->depth-1);
178   depth=watermark->depth;
179   for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--)
180   {
181     for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++)
182     {
183       x=0;
184       for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++)
185       {
186         if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows)
187           break;
188         (void) GetOneVirtualPixel(watermark,k % (ssize_t) watermark->columns,
189           k/(ssize_t) watermark->columns,&pixel,exception);
190         q=GetAuthenticPixels(image,x,y,1,1,exception);
191         if (q == (PixelPacket *) NULL)
192           break;
193         indexes=GetAuthenticIndexQueue(image);
194         switch (c)
195         {
196           case 0:
197           {
198             SetBit(*indexes,i,GetBit(pixel.red,j));
199             break;
200           }
201           case 1:
202           {
203             SetBit(*indexes,i,GetBit(pixel.green,j));
204             break;
205           }
206           case 2:
207           {
208             SetBit(*indexes,i,GetBit(pixel.blue,j));
209             break;
210           }
211         }
212         if (SyncAuthenticPixels(image,exception) == MagickFalse)
213           break;
214         c++;
215         if (c == 3)
216           c=0;
217         k++;
218         if (k == (ssize_t) (watermark->columns*watermark->columns))
219           k=0;
220         if (k == image->offset)
221           j++;
222       }
223     }
224     status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth);
225     if (status == MagickFalse)
226       break;
227   }
228   watermark=DestroyImage(watermark);
229   (void) SyncImage(image);
230   return(GetFirstImageInList(image));
231 }
232 \f
233 /*
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235 %                                                                             %
236 %                                                                             %
237 %                                                                             %
238 %   R e g i s t e r S T E G A N O I m a g e                                   %
239 %                                                                             %
240 %                                                                             %
241 %                                                                             %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 %
244 %  RegisterSTEGANOImage() adds attributes for the STEGANO image format to
245 %  the list of supported formats.  The attributes include the image format
246 %  tag, a method to read and/or write the format, whether the format
247 %  supports the saving of more than one frame to the same file or blob,
248 %  whether the format supports native in-memory I/O, and a brief
249 %  description of the format.
250 %
251 %  The format of the RegisterSTEGANOImage method is:
252 %
253 %      size_t RegisterSTEGANOImage(void)
254 %
255 */
256 ModuleExport size_t RegisterSTEGANOImage(void)
257 {
258   MagickInfo
259     *entry;
260
261   entry=SetMagickInfo("STEGANO");
262   entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage;
263   entry->format_type=ImplicitFormatType;
264   entry->description=ConstantString("Steganographic image");
265   entry->module=ConstantString("STEGANO");
266   (void) RegisterMagickInfo(entry);
267   return(MagickImageCoderSignature);
268 }
269 \f
270 /*
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 %                                                                             %
273 %                                                                             %
274 %                                                                             %
275 %   U n r e g i s t e r S T E G A N O I m a g e                               %
276 %                                                                             %
277 %                                                                             %
278 %                                                                             %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 %
281 %  UnregisterSTEGANOImage() removes format registrations made by the
282 %  STEGANO module from the list of supported formats.
283 %
284 %  The format of the UnregisterSTEGANOImage method is:
285 %
286 %      UnregisterSTEGANOImage(void)
287 %
288 */
289 ModuleExport void UnregisterSTEGANOImage(void)
290 {
291   (void) UnregisterMagickInfo("STEGANO");
292 }