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-2014 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/attribute.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/cache.h"
45 #include "MagickCore/color-private.h"
46 #include "MagickCore/colorspace.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/module.h"
56 #include "MagickCore/monitor.h"
57 #include "MagickCore/monitor-private.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/property.h"
60 #include "MagickCore/quantize.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/utility.h"
68 static MagickBooleanType
69 WriteBRAILLEImage(const ImageInfo *,Image *,ExceptionInfo *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e g i s t e r B R A I L L E I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % RegisterBRAILLEImage() adds values for the Braille format to
83 % the list of supported formats. The values include the image format
84 % tag, a method to read and/or write the format, whether the format
85 % supports the saving of more than one frame to the same file or blob,
86 % whether the format supports native in-memory I/O, and a brief
87 % description of the format.
89 % The format of the RegisterBRAILLEImage method is:
91 % size_t RegisterBRAILLEImage(void)
94 ModuleExport size_t RegisterBRAILLEImage(void)
99 entry=SetMagickInfo("BRF");
100 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
101 entry->adjoin=MagickFalse;
102 entry->description=AcquireString("BRF ASCII Braille format");
103 entry->module=AcquireString("BRAILLE");
104 (void) RegisterMagickInfo(entry);
105 entry=SetMagickInfo("UBRL");
106 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
107 entry->adjoin=MagickFalse;
108 entry->description=AcquireString("Unicode Text format");
109 entry->module=AcquireString("BRAILLE");
110 (void) RegisterMagickInfo(entry);
111 entry=SetMagickInfo("ISOBRL");
112 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
113 entry->adjoin=MagickFalse;
114 entry->description=AcquireString("ISO/TR 11548-1 format");
115 entry->module=AcquireString("BRAILLE");
116 (void) RegisterMagickInfo(entry);
117 return(MagickImageCoderSignature);
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % U n r e g i s t e r B R A I L L E I m a g e %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 % UnregisterBRAILLEImage() removes format registrations made by the
132 % BRAILLE module from the list of supported formats.
134 % The format of the UnregisterBRAILLEImage method is:
136 % UnregisterBRAILLEImage(void)
139 ModuleExport void UnregisterBRAILLEImage(void)
141 (void) UnregisterMagickInfo("BRF");
142 (void) UnregisterMagickInfo("UBRL");
143 (void) UnregisterMagickInfo("ISOBRL");
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % W r i t e B R A I L L E I m a g e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % WriteBRAILLEImage() writes an image to a file in the Braille format.
159 % The format of the WriteBRAILLEImage method is:
161 % MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
162 % Image *image,ExceptionInfo *exception)
164 % A description of each parameter follows.
166 % o image_info: The image info.
168 % o image: The image.
170 % o exception: return any errors or warnings in this structure.
173 static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
174 Image *image,ExceptionInfo *exception)
177 buffer[MaxTextExtent];
192 register const Quantum
205 Open output image file.
207 assert(image_info != (const ImageInfo *) NULL);
208 assert(image_info->signature == MagickSignature);
209 assert(image != (Image *) NULL);
210 assert(image->signature == MagickSignature);
211 if (LocaleCompare(image_info->magick, "UBRL") == 0)
214 if (LocaleCompare(image_info->magick, "ISOBRL") == 0)
218 if (image->debug != MagickFalse)
219 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
220 assert(exception != (ExceptionInfo *) NULL);
221 assert(exception->signature == MagickSignature);
222 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
223 if (status == MagickFalse)
227 value=GetImageProperty(image,"label",exception);
228 if (value != (const char *) NULL)
230 (void) FormatLocaleString(buffer,MaxTextExtent,"Title: %s\n", value);
231 (void) WriteBlobString(image,buffer);
233 if (image->page.x != 0)
235 (void) FormatLocaleString(buffer,MaxTextExtent,"X: %.20g\n",(double)
237 (void) WriteBlobString(image,buffer);
239 if (image->page.y != 0)
241 (void) FormatLocaleString(buffer,MaxTextExtent,"Y: %.20g\n",(double)
243 (void) WriteBlobString(image,buffer);
245 (void) FormatLocaleString(buffer,MaxTextExtent,"Width: %.20g\n",(double)
246 (image->columns+(image->columns % 2)));
247 (void) WriteBlobString(image,buffer);
248 (void) FormatLocaleString(buffer,MaxTextExtent,"Height: %.20g\n",(double)
250 (void) WriteBlobString(image,buffer);
251 (void) WriteBlobString(image,"\n");
253 (void) SetImageType(image,BilevelType,exception);
255 if (image->storage_class == PseudoClass) {
256 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) >=
257 (Quantum) (QuantumRange/2));
258 if (image->colors == 2)
259 polarity=(Quantum) (GetPixelInfoIntensity(&image->colormap[0]) >=
260 GetPixelInfoIntensity(&image->colormap[1]));
262 for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) cell_height)
264 if ((y+cell_height) > image->rows)
265 cell_height = (size_t) (image->rows-y);
267 p=GetVirtualPixels(image,0,y,image->columns,cell_height,exception);
268 if (p == (const Quantum *) NULL)
270 for (x=0; x < (ssize_t) image->columns; x+=2)
272 unsigned char cell = 0;
273 int two_columns = x+1 < (ssize_t) image->columns;
277 #define do_cell(dx,dy,bit) do { \
278 if (image->storage_class == PseudoClass) \
279 cell |= (GetPixelIndex(image,p+x+dx+dy*image->columns) == polarity) << bit; \
281 cell |= (GetPixelGreen(image,p+x+dx+dy*image->columns) == 0) << bit; \
282 DisableMSCWarning(4127) \
307 DisableMSCWarning(4127)
313 unsigned char utf8[3];
315 utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
316 utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
317 utf8[2] = 0x80|(cell&0x3f);
318 (void) WriteBlob(image,3,utf8);
320 else if (iso_11548_1)
322 /* ISO/TR 11548-1 binary */
323 (void) WriteBlobByte(image,cell);
328 static const unsigned char iso_to_brf[64] = {
329 ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
330 '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
331 '"', 'E', '3', 'H', '9', 'O', '6', 'R',
332 '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
333 ',', '*', '5', '<', '-', 'U', '8', 'V',
334 '.', '%', '[', '$', '+', 'X', '!', '&',
335 ';', ':', '4', '\\', '0', 'Z', '7', '(',
336 '_', '?', 'W', ']', '#', 'Y', ')', '='
338 (void) WriteBlobByte(image,iso_to_brf[cell]);
341 if (iso_11548_1 == 0)
342 (void) WriteBlobByte(image,'\n');
343 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
345 if (status == MagickFalse)
348 (void) CloseBlob(image);