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-2010 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 "magick/studio.h"
41 #include "magick/blob.h"
42 #include "magick/blob-private.h"
43 #include "magick/cache.h"
44 #include "magick/color-private.h"
45 #include "magick/colorspace.h"
46 #include "magick/constitute.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/module.h"
55 #include "magick/monitor.h"
56 #include "magick/monitor-private.h"
57 #include "magick/property.h"
58 #include "magick/quantize.h"
59 #include "magick/static.h"
60 #include "magick/string_.h"
61 #include "magick/utility.h"
66 static MagickBooleanType
67 WriteBRAILLEImage(const ImageInfo *,Image *);
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
74 % R e g i s t e r B R A I L L E I m a g e %
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % RegisterBRAILLEImage() adds values for the Braille format to
81 % the list of supported formats. The values include the image format
82 % tag, a method to read and/or write the format, whether the format
83 % supports the saving of more than one frame to the same file or blob,
84 % whether the format supports native in-memory I/O, and a brief
85 % description of the format.
87 % The format of the RegisterBRAILLEImage method is:
89 % unsigned long RegisterBRAILLEImage(void)
92 ModuleExport unsigned long RegisterBRAILLEImage(void)
97 entry=SetMagickInfo("BRF");
98 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
99 entry->adjoin=MagickFalse;
100 entry->description=AcquireString("BRF ASCII Braille format");
101 entry->module=AcquireString("BRAILLE");
102 (void) RegisterMagickInfo(entry);
103 entry=SetMagickInfo("UBRL");
104 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
105 entry->adjoin=MagickFalse;
106 entry->description=AcquireString("Unicode Text format");
107 entry->module=AcquireString("BRAILLE");
108 (void) RegisterMagickInfo(entry);
109 entry=SetMagickInfo("ISOBRL");
110 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
111 entry->adjoin=MagickFalse;
112 entry->description=AcquireString("ISO/TR 11548-1 format");
113 entry->module=AcquireString("BRAILLE");
114 (void) RegisterMagickInfo(entry);
115 return(MagickImageCoderSignature);
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % U n r e g i s t e r B R A I L L E I m a g e %
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 % UnregisterBRAILLEImage() removes format registrations made by the
130 % BRAILLE module from the list of supported formats.
132 % The format of the UnregisterBRAILLEImage method is:
134 % UnregisterBRAILLEImage(void)
137 ModuleExport void UnregisterBRAILLEImage(void)
139 (void) UnregisterMagickInfo("BRF");
140 (void) UnregisterMagickInfo("UBRL");
141 (void) UnregisterMagickInfo("ISOBRL");
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % W r i t e B R A I L L E I m a g e %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % WriteBRAILLEImage() writes an image to a file in the Braille format.
157 % The format of the WriteBRAILLEImage method is:
159 % MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
162 % A description of each parameter follows.
164 % o image_info: The image info.
166 % o image: The image.
169 static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
173 buffer[MaxTextExtent];
188 register const IndexPacket
191 register const PixelPacket
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 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
220 if (status == MagickFalse)
224 value=GetImageProperty(image,"Label");
225 if (value != (const char *) NULL) {
226 (void) FormatMagickString(buffer,MaxTextExtent,"Title: %s\n", value);
227 (void) WriteBlobString(image,buffer);
231 (void) FormatMagickString(buffer,MaxTextExtent,"X: %ld\n",
233 (void) WriteBlobString(image,buffer);
237 (void) FormatMagickString(buffer,MaxTextExtent,"Y: %ld\n",
239 (void) WriteBlobString(image,buffer);
241 (void) FormatMagickString(buffer,MaxTextExtent,"Width: %lu\n",
242 image->columns+(image->columns % 2));
243 (void) WriteBlobString(image,buffer);
244 (void) FormatMagickString(buffer,MaxTextExtent,"Height: %lu\n",
246 (void) WriteBlobString(image,buffer);
247 (void) WriteBlobString(image,"\n");
249 (void) SetImageType(image,BilevelType);
251 if (image->storage_class == PseudoClass) {
252 polarity=(IndexPacket) (PixelIntensityToQuantum(&image->colormap[0]) >=
253 (Quantum) (QuantumRange/2));
254 if (image->colors == 2)
255 polarity=(IndexPacket)
256 (PixelIntensityToQuantum(&image->colormap[0]) >=
257 PixelIntensityToQuantum(&image->colormap[1]));
259 for (y=0; y < (long) image->rows; y+=cell_height)
261 if ((y+cell_height) > image->rows)
262 cell_height = (unsigned long) (image->rows-y);
264 p=GetVirtualPixels(image,0,y,image->columns,cell_height,&image->exception);
265 if (p == (const PixelPacket *) NULL)
267 indexes=GetVirtualIndexQueue(image);
268 for (x=0; x < (long) image->columns; x+=2)
270 unsigned char cell = 0;
271 int two_columns = x+1 < (long) image->columns;
275 #define do_cell(dx,dy,bit) do { \
276 if (image->storage_class == PseudoClass) \
277 cell |= (indexes[x+dx+dy*image->columns] == polarity) << bit; \
279 cell |= (p[x+dx+dy*image->columns].green == 0) << bit; \
307 unsigned char utf8[3];
309 utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
310 utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
311 utf8[2] = 0x80|(cell&0x3f);
312 (void) WriteBlob(image,3,utf8);
314 else if (iso_11548_1)
316 /* ISO/TR 11548-1 binary */
317 (void) WriteBlobByte(image,cell);
322 static const unsigned char iso_to_brf[64] = {
323 ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
324 '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
325 '"', 'E', '3', 'H', '9', 'O', '6', 'R',
326 '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
327 ',', '*', '5', '<', '-', 'U', '8', 'V',
328 '.', '%', '[', '$', '+', 'X', '!', '&',
329 ';', ':', '4', '\\', '0', 'Z', '7', '(',
330 '_', '?', 'W', ']', '#', 'Y', ')', '='
332 (void) WriteBlobByte(image,iso_to_brf[cell]);
336 (void) WriteBlobByte(image,'\n');
337 status=SetImageProgress(image,SaveImageTag,y,image->rows);
338 if (status == MagickFalse)
341 (void) CloseBlob(image);