/src/imagemagick/coders/mono.c
Line | Count | Source |
1 | | /* |
2 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
3 | | % % |
4 | | % % |
5 | | % % |
6 | | % M M OOO N N OOO % |
7 | | % MM MM O O NN N O O % |
8 | | % M M M O O N N N O O % |
9 | | % M M O O N NN O O % |
10 | | % M M OOO N N OOO % |
11 | | % % |
12 | | % % |
13 | | % Read/Write Raw Bi-Level Bitmap Format % |
14 | | % % |
15 | | % Software Design % |
16 | | % Cristy % |
17 | | % July 1992 % |
18 | | % % |
19 | | % % |
20 | | % Copyright @ 1999 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://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 "MagickCore/studio.h" |
43 | | #include "MagickCore/attribute.h" |
44 | | #include "MagickCore/blob.h" |
45 | | #include "MagickCore/blob-private.h" |
46 | | #include "MagickCore/cache.h" |
47 | | #include "MagickCore/color-private.h" |
48 | | #include "MagickCore/colormap.h" |
49 | | #include "MagickCore/colorspace.h" |
50 | | #include "MagickCore/colorspace-private.h" |
51 | | #include "MagickCore/exception.h" |
52 | | #include "MagickCore/exception-private.h" |
53 | | #include "MagickCore/image.h" |
54 | | #include "MagickCore/image-private.h" |
55 | | #include "MagickCore/list.h" |
56 | | #include "MagickCore/magick.h" |
57 | | #include "MagickCore/memory_.h" |
58 | | #include "MagickCore/monitor.h" |
59 | | #include "MagickCore/monitor-private.h" |
60 | | #include "MagickCore/pixel-accessor.h" |
61 | | #include "MagickCore/quantum-private.h" |
62 | | #include "MagickCore/static.h" |
63 | | #include "MagickCore/string_.h" |
64 | | #include "MagickCore/module.h" |
65 | | |
66 | | /* |
67 | | Forward declarations. |
68 | | */ |
69 | | static MagickBooleanType |
70 | | WriteMONOImage(const ImageInfo *,Image *,ExceptionInfo *); |
71 | | |
72 | | /* |
73 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
74 | | % % |
75 | | % % |
76 | | % % |
77 | | % R e a d M O N O I m a g e % |
78 | | % % |
79 | | % % |
80 | | % % |
81 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
82 | | % |
83 | | % ReadMONOImage() reads an image of raw bites in LSB order and returns |
84 | | % it. It allocates the memory necessary for the new Image structure and |
85 | | % returns a pointer to the new image. |
86 | | % |
87 | | % The format of the ReadMONOImage method is: |
88 | | % |
89 | | % Image *ReadMONOImage(const ImageInfo *image_info, |
90 | | % ExceptionInfo *exception) |
91 | | % |
92 | | % A description of each parameter follows: |
93 | | % |
94 | | % o image_info: the image info. |
95 | | % |
96 | | % o exception: return any errors or warnings in this structure. |
97 | | % |
98 | | */ |
99 | | static Image *ReadMONOImage(const ImageInfo *image_info, |
100 | | ExceptionInfo *exception) |
101 | 242 | { |
102 | 242 | Image |
103 | 242 | *image; |
104 | | |
105 | 242 | MagickBooleanType |
106 | 242 | status; |
107 | | |
108 | 242 | Quantum |
109 | 242 | *q; |
110 | | |
111 | 242 | ssize_t |
112 | 242 | x; |
113 | | |
114 | 242 | size_t |
115 | 242 | bit, |
116 | 242 | byte; |
117 | | |
118 | 242 | ssize_t |
119 | 242 | y; |
120 | | |
121 | | /* |
122 | | Open image file. |
123 | | */ |
124 | 242 | assert(image_info != (const ImageInfo *) NULL); |
125 | 242 | assert(image_info->signature == MagickCoreSignature); |
126 | 242 | assert(exception != (ExceptionInfo *) NULL); |
127 | 242 | assert(exception->signature == MagickCoreSignature); |
128 | 242 | if (IsEventLogging() != MagickFalse) |
129 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
130 | 0 | image_info->filename); |
131 | 242 | image=AcquireImage(image_info,exception); |
132 | 242 | if ((image->columns == 0) || (image->rows == 0)) |
133 | 242 | ThrowReaderException(OptionError,"MustSpecifyImageSize"); |
134 | 0 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
135 | 0 | if (status == MagickFalse) |
136 | 0 | { |
137 | 0 | image=DestroyImageList(image); |
138 | 0 | return((Image *) NULL); |
139 | 0 | } |
140 | 0 | if (DiscardBlobBytes(image,(MagickSizeType) image->offset) == MagickFalse) |
141 | 0 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
142 | 0 | image->filename); |
143 | | /* |
144 | | Initialize image colormap. |
145 | | */ |
146 | 0 | image->depth=1; |
147 | 0 | if (AcquireImageColormap(image,2,exception) == MagickFalse) |
148 | 0 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
149 | 0 | if (image_info->ping != MagickFalse) |
150 | 0 | { |
151 | 0 | (void) CloseBlob(image); |
152 | 0 | return(GetFirstImageInList(image)); |
153 | 0 | } |
154 | 0 | status=SetImageExtent(image,image->columns,image->rows,exception); |
155 | 0 | if (status == MagickFalse) |
156 | 0 | return(DestroyImageList(image)); |
157 | | /* |
158 | | Convert bi-level image to pixel packets. |
159 | | */ |
160 | 0 | for (y=0; y < (ssize_t) image->rows; y++) |
161 | 0 | { |
162 | 0 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
163 | 0 | if (q == (Quantum *) NULL) |
164 | 0 | break; |
165 | 0 | bit=0; |
166 | 0 | byte=0; |
167 | 0 | for (x=0; x < (ssize_t) image->columns; x++) |
168 | 0 | { |
169 | 0 | if (bit == 0) |
170 | 0 | byte=(size_t) ReadBlobByte(image); |
171 | 0 | if (image_info->endian == LSBEndian) |
172 | 0 | SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x00 : 0x01),q); |
173 | 0 | else |
174 | 0 | SetPixelIndex(image,(Quantum) (((byte & 0x01) != 0) ? 0x01 : 0x00),q); |
175 | 0 | bit++; |
176 | 0 | if (bit == 8) |
177 | 0 | bit=0; |
178 | 0 | byte>>=1; |
179 | 0 | q+=(ptrdiff_t) GetPixelChannels(image); |
180 | 0 | } |
181 | 0 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
182 | 0 | break; |
183 | 0 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
184 | 0 | image->rows); |
185 | 0 | if (status == MagickFalse) |
186 | 0 | break; |
187 | 0 | } |
188 | 0 | (void) SyncImage(image,exception); |
189 | 0 | if (EOFBlob(image) != MagickFalse) |
190 | 0 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
191 | 0 | image->filename); |
192 | 0 | if (CloseBlob(image) == MagickFalse) |
193 | 0 | status=MagickFalse; |
194 | 0 | if (status == MagickFalse) |
195 | 0 | return(DestroyImageList(image)); |
196 | 0 | return(GetFirstImageInList(image)); |
197 | 0 | } |
198 | | |
199 | | /* |
200 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
201 | | % % |
202 | | % % |
203 | | % % |
204 | | % R e g i s t e r M O N O I m a g e % |
205 | | % % |
206 | | % % |
207 | | % % |
208 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
209 | | % |
210 | | % RegisterMONOImage() adds attributes for the MONO image format to |
211 | | % the list of supported formats. The attributes include the image format |
212 | | % tag, a method to read and/or write the format, whether the format |
213 | | % supports the saving of more than one frame to the same file or blob, |
214 | | % whether the format supports native in-memory I/O, and a brief |
215 | | % description of the format. |
216 | | % |
217 | | % The format of the RegisterMONOImage method is: |
218 | | % |
219 | | % size_t RegisterMONOImage(void) |
220 | | % |
221 | | */ |
222 | | ModuleExport size_t RegisterMONOImage(void) |
223 | 9 | { |
224 | 9 | MagickInfo |
225 | 9 | *entry; |
226 | | |
227 | 9 | entry=AcquireMagickInfo("MONO","MONO","Raw bi-level bitmap"); |
228 | 9 | entry->decoder=(DecodeImageHandler *) ReadMONOImage; |
229 | 9 | entry->encoder=(EncodeImageHandler *) WriteMONOImage; |
230 | 9 | entry->flags|=CoderRawSupportFlag; |
231 | 9 | entry->flags|=CoderEndianSupportFlag; |
232 | 9 | entry->flags^=CoderAdjoinFlag; |
233 | 9 | (void) RegisterMagickInfo(entry); |
234 | 9 | return(MagickImageCoderSignature); |
235 | 9 | } |
236 | | |
237 | | /* |
238 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
239 | | % % |
240 | | % % |
241 | | % % |
242 | | % U n r e g i s t e r M O N O I m a g e % |
243 | | % % |
244 | | % % |
245 | | % % |
246 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
247 | | % |
248 | | % UnregisterMONOImage() removes format registrations made by the |
249 | | % MONO module from the list of supported formats. |
250 | | % |
251 | | % The format of the UnregisterMONOImage method is: |
252 | | % |
253 | | % UnregisterMONOImage(void) |
254 | | % |
255 | | */ |
256 | | ModuleExport void UnregisterMONOImage(void) |
257 | 0 | { |
258 | 0 | (void) UnregisterMagickInfo("MONO"); |
259 | 0 | } |
260 | | |
261 | | /* |
262 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
263 | | % % |
264 | | % % |
265 | | % % |
266 | | % W r i t e M O N O I m a g e % |
267 | | % % |
268 | | % % |
269 | | % % |
270 | | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
271 | | % |
272 | | % WriteMONOImage() writes an image of raw bits in LSB order to a file. |
273 | | % |
274 | | % The format of the WriteMONOImage method is: |
275 | | % |
276 | | % MagickBooleanType WriteMONOImage(const ImageInfo *image_info, |
277 | | % Image *image,ExceptionInfo *exception) |
278 | | % |
279 | | % A description of each parameter follows. |
280 | | % |
281 | | % o image_info: the image info. |
282 | | % |
283 | | % o image: The image. |
284 | | % |
285 | | % o exception: return any errors or warnings in this structure. |
286 | | % |
287 | | */ |
288 | | static MagickBooleanType WriteMONOImage(const ImageInfo *image_info, |
289 | | Image *image,ExceptionInfo *exception) |
290 | 0 | { |
291 | 0 | MagickBooleanType |
292 | 0 | status; |
293 | |
|
294 | 0 | const Quantum |
295 | 0 | *p; |
296 | |
|
297 | 0 | ssize_t |
298 | 0 | x; |
299 | |
|
300 | 0 | size_t |
301 | 0 | bit, |
302 | 0 | byte; |
303 | |
|
304 | 0 | ssize_t |
305 | 0 | y; |
306 | | |
307 | | /* |
308 | | Open output image file. |
309 | | */ |
310 | 0 | assert(image_info != (const ImageInfo *) NULL); |
311 | 0 | assert(image_info->signature == MagickCoreSignature); |
312 | 0 | assert(image != (Image *) NULL); |
313 | 0 | assert(image->signature == MagickCoreSignature); |
314 | 0 | assert(exception != (ExceptionInfo *) NULL); |
315 | 0 | assert(exception->signature == MagickCoreSignature); |
316 | 0 | if (IsEventLogging() != MagickFalse) |
317 | 0 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
318 | 0 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
319 | 0 | if (status == MagickFalse) |
320 | 0 | return(status); |
321 | 0 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
322 | 0 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
323 | | /* |
324 | | Convert image to a bi-level image. |
325 | | */ |
326 | 0 | (void) SetImageType(image,BilevelType,exception); |
327 | 0 | for (y=0; y < (ssize_t) image->rows; y++) |
328 | 0 | { |
329 | 0 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
330 | 0 | if (p == (const Quantum *) NULL) |
331 | 0 | break; |
332 | 0 | bit=0; |
333 | 0 | byte=0; |
334 | 0 | for (x=0; x < (ssize_t) image->columns; x++) |
335 | 0 | { |
336 | 0 | byte>>=1; |
337 | 0 | if (image->endian == LSBEndian) |
338 | 0 | { |
339 | 0 | if (GetPixelLuma(image,p) < ((double) QuantumRange/2.0)) |
340 | 0 | byte|=0x80; |
341 | 0 | } |
342 | 0 | else |
343 | 0 | if (GetPixelLuma(image,p) >= ((double) QuantumRange/2.0)) |
344 | 0 | byte|=0x80; |
345 | 0 | bit++; |
346 | 0 | if (bit == 8) |
347 | 0 | { |
348 | 0 | (void) WriteBlobByte(image,(unsigned char) byte); |
349 | 0 | bit=0; |
350 | 0 | byte=0; |
351 | 0 | } |
352 | 0 | p+=(ptrdiff_t) GetPixelChannels(image); |
353 | 0 | } |
354 | 0 | if (bit != 0) |
355 | 0 | (void) WriteBlobByte(image,(unsigned char) (byte >> (8-bit))); |
356 | 0 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
357 | 0 | image->rows); |
358 | 0 | if (status == MagickFalse) |
359 | 0 | break; |
360 | 0 | } |
361 | 0 | if (CloseBlob(image) == MagickFalse) |
362 | 0 | status=MagickFalse; |
363 | 0 | return(status); |
364 | 0 | } |