2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % BBBB RRRR AAA IIIII L L EEEEE %
6 % B B R R A A I L L E %
7 % BBBB RRRR AAAAA I L L EEE %
8 % B B R R A A I L L E %
9 % BBBB R R A A IIIII LLLLL LLLLL EEEEE %
12 % Read/Write Braille Format %
18 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
19 % dedicated to making software imaging solutions freely available. %
21 % You may not use this file except in compliance with the License. You may %
22 % obtain a copy of the License at %
24 % http://www.imagemagick.org/script/license.php %
26 % Unless required by applicable law or agreed to in writing, software %
27 % distributed under the License is distributed on an "AS IS" BASIS, %
28 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
29 % See the License for the specific language governing permissions and %
30 % limitations under the License. %
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 #include "MagickCore/studio.h"
41 #include "MagickCore/blob.h"
42 #include "MagickCore/blob-private.h"
43 #include "MagickCore/cache.h"
44 #include "MagickCore/color-private.h"
45 #include "MagickCore/colorspace.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/module.h"
55 #include "MagickCore/monitor.h"
56 #include "MagickCore/monitor-private.h"
57 #include "MagickCore/pixel-accessor.h"
58 #include "MagickCore/property.h"
59 #include "MagickCore/quantize.h"
60 #include "MagickCore/static.h"
61 #include "MagickCore/string_.h"
62 #include "MagickCore/utility.h"
67 static MagickBooleanType
68 WriteBRAILLEImage(const ImageInfo *,Image *,ExceptionInfo *);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75 % R e g i s t e r B R A I L L E I m a g e %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % RegisterBRAILLEImage() adds values for the Braille format to
82 % the list of supported formats. The values include the image format
83 % tag, a method to read and/or write the format, whether the format
84 % supports the saving of more than one frame to the same file or blob,
85 % whether the format supports native in-memory I/O, and a brief
86 % description of the format.
88 % The format of the RegisterBRAILLEImage method is:
90 % size_t RegisterBRAILLEImage(void)
93 ModuleExport size_t RegisterBRAILLEImage(void)
98 entry=SetMagickInfo("BRF");
99 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
100 entry->adjoin=MagickFalse;
101 entry->description=AcquireString("BRF ASCII Braille format");
102 entry->module=AcquireString("BRAILLE");
103 (void) RegisterMagickInfo(entry);
104 entry=SetMagickInfo("UBRL");
105 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
106 entry->adjoin=MagickFalse;
107 entry->description=AcquireString("Unicode Text format");
108 entry->module=AcquireString("BRAILLE");
109 (void) RegisterMagickInfo(entry);
110 entry=SetMagickInfo("ISOBRL");
111 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
112 entry->adjoin=MagickFalse;
113 entry->description=AcquireString("ISO/TR 11548-1 format");
114 entry->module=AcquireString("BRAILLE");
115 (void) RegisterMagickInfo(entry);
116 return(MagickImageCoderSignature);
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % U n r e g i s t e r B R A I L L E I m a g e %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % UnregisterBRAILLEImage() removes format registrations made by the
131 % BRAILLE module from the list of supported formats.
133 % The format of the UnregisterBRAILLEImage method is:
135 % UnregisterBRAILLEImage(void)
138 ModuleExport void UnregisterBRAILLEImage(void)
140 (void) UnregisterMagickInfo("BRF");
141 (void) UnregisterMagickInfo("UBRL");
142 (void) UnregisterMagickInfo("ISOBRL");
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % W r i t e B R A I L L E I m a g e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % WriteBRAILLEImage() writes an image to a file in the Braille format.
158 % The format of the WriteBRAILLEImage method is:
160 % MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
161 % Image *image,ExceptionInfo *exception)
163 % A description of each parameter follows.
165 % o image_info: The image info.
167 % o image: The image.
169 % o exception: return any errors or warnings in this structure.
172 static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
173 Image *image,ExceptionInfo *exception)
176 buffer[MaxTextExtent];
191 register const Quantum
204 Open output image file.
206 assert(image_info != (const ImageInfo *) NULL);
207 assert(image_info->signature == MagickSignature);
208 assert(image != (Image *) NULL);
209 assert(image->signature == MagickSignature);
210 if (LocaleCompare(image_info->magick, "UBRL") == 0)
213 if (LocaleCompare(image_info->magick, "ISOBRL") == 0)
217 if (image->debug != MagickFalse)
218 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
219 assert(exception != (ExceptionInfo *) NULL);
220 assert(exception->signature == MagickSignature);
221 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
222 if (status == MagickFalse)
226 value=GetImageProperty(image,"label",exception);
227 if (value != (const char *) NULL)
229 (void) FormatLocaleString(buffer,MaxTextExtent,"Title: %s\n", value);
230 (void) WriteBlobString(image,buffer);
232 if (image->page.x != 0)
234 (void) FormatLocaleString(buffer,MaxTextExtent,"X: %.20g\n",(double)
236 (void) WriteBlobString(image,buffer);
238 if (image->page.y != 0)
240 (void) FormatLocaleString(buffer,MaxTextExtent,"Y: %.20g\n",(double)
242 (void) WriteBlobString(image,buffer);
244 (void) FormatLocaleString(buffer,MaxTextExtent,"Width: %.20g\n",(double)
245 (image->columns+(image->columns % 2)));
246 (void) WriteBlobString(image,buffer);
247 (void) FormatLocaleString(buffer,MaxTextExtent,"Height: %.20g\n",(double)
249 (void) WriteBlobString(image,buffer);
250 (void) WriteBlobString(image,"\n");
252 (void) SetImageType(image,BilevelType,exception);
254 if (image->storage_class == PseudoClass) {
255 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) >=
256 (Quantum) (QuantumRange/2));
257 if (image->colors == 2)
258 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) >=
259 GetPixelInfoIntensity(&image->colormap[1]));
261 for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) cell_height)
263 if ((y+cell_height) > image->rows)
264 cell_height = (size_t) (image->rows-y);
266 p=GetVirtualPixels(image,0,y,image->columns,cell_height,exception);
267 if (p == (const Quantum *) NULL)
269 for (x=0; x < (ssize_t) image->columns; x+=2)
271 unsigned char cell = 0;
272 int two_columns = x+1 < (ssize_t) image->columns;
276 #define do_cell(dx,dy,bit) do { \
277 if (image->storage_class == PseudoClass) \
278 cell |= (GetPixelIndex(image,p+x+dx+dy*image->columns) == polarity) << bit; \
280 cell |= (GetPixelGreen(image,p+x+dx+dy*image->columns) == 0) << bit; \
308 unsigned char utf8[3];
310 utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
311 utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
312 utf8[2] = 0x80|(cell&0x3f);
313 (void) WriteBlob(image,3,utf8);
315 else if (iso_11548_1)
317 /* ISO/TR 11548-1 binary */
318 (void) WriteBlobByte(image,cell);
323 static const unsigned char iso_to_brf[64] = {
324 ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
325 '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
326 '"', 'E', '3', 'H', '9', 'O', '6', 'R',
327 '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
328 ',', '*', '5', '<', '-', 'U', '8', 'V',
329 '.', '%', '[', '$', '+', 'X', '!', '&',
330 ';', ':', '4', '\\', '0', 'Z', '7', '(',
331 '_', '?', 'W', ']', '#', 'Y', ')', '='
333 (void) WriteBlobByte(image,iso_to_brf[cell]);
336 if (iso_11548_1 == 0)
337 (void) WriteBlobByte(image,'\n');
338 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
340 if (status == MagickFalse)
343 (void) CloseBlob(image);