blob: 7a2ac17208e683c5843f60bd965162da6dbe566b [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
42#include "magick/studio.h"
43#include "magick/blob.h"
44#include "magick/blob-private.h"
45#include "magick/cache.h"
cristye20ffe52010-04-24 23:51:41 +000046#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000047#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
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
cristybb503372010-05-27 20:51:26 +000090static inline size_t MagickMin(const size_t x,
91 const size_t y)
cristy3ed852e2009-09-05 21:47:34 +000092{
93 if (x < y)
94 return(x);
95 return(y);
96}
97
98static Image *ReadSTEGANOImage(const ImageInfo *image_info,
99 ExceptionInfo *exception)
100{
cristybb503372010-05-27 20:51:26 +0000101#define GetBit(alpha,i) MagickMin((((size_t) (alpha) >> (size_t) \
cristy3ed852e2009-09-05 21:47:34 +0000102 (i)) & 0x01),16)
103#define SetBit(alpha,i,set) (alpha)=(IndexPacket) ((set) != 0 ? \
cristyeaedf062010-05-29 22:36:02 +0000104 (size_t) (alpha) | (one << (size_t) (i)) : (size_t) \
105 (alpha) & ~(one << (size_t) (i)))
cristy3ed852e2009-09-05 21:47:34 +0000106
107 Image
108 *image,
109 *watermark;
110
111 ImageInfo
112 *read_info;
113
cristye1bf8ad2010-09-19 17:07:03 +0000114 int
115 c;
cristy3ed852e2009-09-05 21:47:34 +0000116
117 MagickBooleanType
118 status;
119
120 PixelPacket
121 pixel;
122
123 register IndexPacket
124 *indexes;
125
cristy3ed852e2009-09-05 21:47:34 +0000126 register PixelPacket
127 *q;
128
cristye1bf8ad2010-09-19 17:07:03 +0000129 register ssize_t
130 x;
131
cristybb503372010-05-27 20:51:26 +0000132 size_t
cristyeaedf062010-05-29 22:36:02 +0000133 depth,
134 one;
cristy3ed852e2009-09-05 21:47:34 +0000135
cristye1bf8ad2010-09-19 17:07:03 +0000136 ssize_t
137 i,
138 j,
139 k,
140 y;
141
cristy3ed852e2009-09-05 21:47:34 +0000142 /*
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);
cristyeaedf062010-05-29 22:36:02 +0000152 one=1;
cristy3ed852e2009-09-05 21:47:34 +0000153 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;
cristy222665d2010-09-19 17:08:12 +0000177 i=(ssize_t) (watermark->depth-1);
cristye1bf8ad2010-09-19 17:07:03 +0000178 depth=watermark->depth;
cristybb503372010-05-27 20:51:26 +0000179 for (k=image->offset; (i >= 0) && (j < (ssize_t) depth); i--)
cristy3ed852e2009-09-05 21:47:34 +0000180 {
cristybb503372010-05-27 20:51:26 +0000181 for (y=0; (y < (ssize_t) image->rows) && (j < (ssize_t) depth); y++)
cristy3ed852e2009-09-05 21:47:34 +0000182 {
183 x=0;
cristye1bf8ad2010-09-19 17:07:03 +0000184 for ( ; (x < (ssize_t) image->columns) && (j < (ssize_t) depth); x++)
cristy3ed852e2009-09-05 21:47:34 +0000185 {
cristybb503372010-05-27 20:51:26 +0000186 if ((k/(ssize_t) watermark->columns) >= (ssize_t) watermark->rows)
cristy3ed852e2009-09-05 21:47:34 +0000187 break;
cristybb503372010-05-27 20:51:26 +0000188 (void) GetOneVirtualPixel(watermark,k % (ssize_t) watermark->columns,
189 k/(ssize_t) watermark->columns,&pixel,exception);
cristy3ed852e2009-09-05 21:47:34 +0000190 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++;
cristybb503372010-05-27 20:51:26 +0000218 if (k == (ssize_t) (watermark->columns*watermark->columns))
cristy3ed852e2009-09-05 21:47:34 +0000219 k=0;
220 if (k == image->offset)
221 j++;
222 }
223 }
cristy222665d2010-09-19 17:08:12 +0000224 status=SetImageProgress(image,LoadImagesTag,(MagickOffsetType) i,depth);
cristy3ed852e2009-09-05 21:47:34 +0000225 if (status == MagickFalse)
226 break;
227 }
228 watermark=DestroyImage(watermark);
229 (void) SyncImage(image);
230 return(GetFirstImageInList(image));
231}
232
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%
cristybb503372010-05-27 20:51:26 +0000253% size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000254%
255*/
cristybb503372010-05-27 20:51:26 +0000256ModuleExport size_t RegisterSTEGANOImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000257{
258 MagickInfo
259 *entry;
260
261 entry=SetMagickInfo("STEGANO");
262 entry->decoder=(DecodeImageHandler *) ReadSTEGANOImage;
cristy009d7392010-07-25 22:08:41 +0000263 entry->format_type=ImplicitFormatType;
cristy3ed852e2009-09-05 21:47:34 +0000264 entry->description=ConstantString("Steganographic image");
265 entry->module=ConstantString("STEGANO");
266 (void) RegisterMagickInfo(entry);
267 return(MagickImageCoderSignature);
268}
269
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*/
289ModuleExport void UnregisterSTEGANOImage(void)
290{
291 (void) UnregisterMagickInfo("STEGANO");
292}