blob: 7b6baa366c4478ba697fef29834702b6f3486d84 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy3ed852e2009-09-05 21:47:34 +000062
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
cristybb503372010-05-27 20:51:26 +000091static inline size_t MagickMin(const size_t x,
92 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +000093{
94 if (x < y)
95 return(x);
96 return(y);
97}
98
99static Image *ReadSTEGANOImage(const ImageInfo *image_info,
100 ExceptionInfo *exception)
101{
cristybb503372010-05-27 20:51:26 +0000102#define GetBit(alpha,i) MagickMin((((size_t) (alpha) >> (size_t) \
cristy3ed852e2009-09-05 21:47:34 +0000103 (i)) & 0x01),16)
cristy4c08aed2011-07-01 19:47:50 +0000104#define SetBit(i,set) SetPixelIndex(image,((set) != 0 ? \
105 (size_t) GetPixelIndex(image,q) | (one << (size_t) (i)) : \
106 (size_t) GetPixelIndex(image,q) & ~(one << (size_t) (i))),q)
cristy3ed852e2009-09-05 21:47:34 +0000107
108 Image
109 *image,
110 *watermark;
111
112 ImageInfo
113 *read_info;
114
cristye1bf8ad2010-09-19 17:07:03 +0000115 int
116 c;
cristy3ed852e2009-09-05 21:47:34 +0000117
118 MagickBooleanType
119 status;
120
cristy101ab702011-10-13 13:06:32 +0000121 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000122 pixel;
123
cristy4c08aed2011-07-01 19:47:50 +0000124 register Quantum
cristy3ed852e2009-09-05 21:47:34 +0000125 *q;
126
cristye1bf8ad2010-09-19 17:07:03 +0000127 register ssize_t
128 x;
129
cristybb503372010-05-27 20:51:26 +0000130 size_t
cristyeaedf062010-05-29 22:36:02 +0000131 depth,
132 one;
cristy3ed852e2009-09-05 21:47:34 +0000133
cristye1bf8ad2010-09-19 17:07:03 +0000134 ssize_t
135 i,
136 j,
137 k,
138 y;
139
cristy3ed852e2009-09-05 21:47:34 +0000140 /*
141 Initialize Image structure.
142 */
143 assert(image_info != (const ImageInfo *) NULL);
144 assert(image_info->signature == MagickSignature);
145 if (image_info->debug != MagickFalse)
146 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
147 image_info->filename);
148 assert(exception != (ExceptionInfo *) NULL);
149 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +0000150 one=1;
cristy9950d572011-10-01 18:22:35 +0000151 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000152 if ((image->columns == 0) || (image->rows == 0))
153 ThrowReaderException(OptionError,"MustSpecifyImageSize");
154 read_info=CloneImageInfo(image_info);
155 SetImageInfoBlob(read_info,(void *) NULL,0);
156 *read_info->magick='\0';
157 watermark=ReadImage(read_info,exception);
158 read_info=DestroyImageInfo(read_info);
159 if (watermark == (Image *) NULL)
160 return((Image *) NULL);
161 watermark->depth=MAGICKCORE_QUANTUM_DEPTH;
cristy018f07f2011-09-04 21:15:19 +0000162 if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000163 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
164 if (image_info->ping != MagickFalse)
165 {
166 (void) CloseBlob(image);
167 return(GetFirstImageInList(image));
168 }
169 /*
170 Get hidden watermark from low-order bits of image.
171 */
172 c=0;
173 i=0;
174 j=0;
cristy222665d2010-09-19 17:08:12 +0000175 i=(ssize_t) (watermark->depth-1);
cristye1bf8ad2010-09-19 17:07:03 +0000176 depth=watermark->depth;
cristybb503372010-05-27 20:51:26 +0000177 for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +0000178 {
cristybb503372010-05-27 20:51:26 +0000179 for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +0000180 {
181 x=0;
cristye1bf8ad2010-09-19 17:07:03 +0000182 for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +0000183 {
cristy2ed42f62011-10-02 19:49:57 +0000184 Quantum
185 virtual_pixel[MaxPixelChannels];
186
cristybb503372010-05-27 20:51:26 +0000187 if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows)
cristy3ed852e2009-09-05 21:47:34 +0000188 break;
cristybb503372010-05-27 20:51:26 +0000189 (void) GetOneVirtualPixel(watermark,k % (ssize_t) watermark->columns,
cristy2ed42f62011-10-02 19:49:57 +0000190 k/(ssize_t) watermark->columns,virtual_pixel,exception);
191 pixel.red=virtual_pixel[RedPixelChannel];
192 pixel.green=virtual_pixel[GreenPixelChannel];
193 pixel.blue=virtual_pixel[BluePixelChannel];
194 pixel.alpha=virtual_pixel[AlphaPixelChannel];
cristy3ed852e2009-09-05 21:47:34 +0000195 q=GetAuthenticPixels(image,x,y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000196 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000197 break;
cristy3ed852e2009-09-05 21:47:34 +0000198 switch (c)
199 {
200 case 0:
201 {
cristy4c08aed2011-07-01 19:47:50 +0000202 SetBit(i,GetBit(pixel.red,j));
cristy3ed852e2009-09-05 21:47:34 +0000203 break;
204 }
205 case 1:
206 {
cristy4c08aed2011-07-01 19:47:50 +0000207 SetBit(i,GetBit(pixel.green,j));
cristy3ed852e2009-09-05 21:47:34 +0000208 break;
209 }
210 case 2:
211 {
cristy4c08aed2011-07-01 19:47:50 +0000212 SetBit(i,GetBit(pixel.blue,j));
cristy3ed852e2009-09-05 21:47:34 +0000213 break;
214 }
215 }
216 if (SyncAuthenticPixels(image,exception) == MagickFalse)
217 break;
218 c++;
219 if (c == 3)
220 c=0;
221 k++;
cristybb503372010-05-27 20:51:26 +0000222 if (k == (ssize_t) (watermark->columns*watermark->columns))
cristy3ed852e2009-09-05 21:47:34 +0000223 k=0;
224 if (k == image->offset)
225 j++;
226 }
227 }
cristy222665d2010-09-19 17:08:12 +0000228 status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth);
cristy3ed852e2009-09-05 21:47:34 +0000229 if (status == MagickFalse)
230 break;
231 }
232 watermark=DestroyImage(watermark);
cristyea1a8aa2011-10-20 13:24:06 +0000233 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000234 return(GetFirstImageInList(image));
235}
236
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%
cristybb503372010-05-27 20:51:26 +0000257% size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000258%
259*/
cristybb503372010-05-27 20:51:26 +0000260ModuleExport size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000261{
262 MagickInfo
263 *entry;
264
265 entry=SetMagickInfo("STEGANO");
266 entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage;
cristy009d7392010-07-25 22:08:41 +0000267 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000268 entry->description=ConstantString("Steganographic image");
269 entry->module=ConstantString("STEGANO");
270 (void) RegisterMagickInfo(entry);
271 return(MagickImageCoderSignature);
272}
273
274/*
275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276% %
277% %
278% %
279% U n r e g i s t e r S T E G A N O I m a g e %
280% %
281% %
282% %
283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284%
285% UnregisterSTEGANOImage() removes format registrations made by the
286% STEGANO module from the list of supported formats.
287%
288% The format of the UnregisterSTEGANOImage method is:
289%
290% UnregisterSTEGANOImage(void)
291%
292*/
293ModuleExport void UnregisterSTEGANOImage(void)
294{
295 (void) UnregisterMagickInfo("STEGANO");
296}