1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
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 %
10 % %
11 % %
12 % Read/Write Braille Format %
13 % %
14 % Samuel Thibault %
15 % February 2008 %
16 % %
17 % %
18 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
19 % dedicated to making software imaging solutions freely available. %
20 % %
21 % You may not use this file except in compliance with the License. You may %
22 % obtain a copy of the License at %
23 % %
24 % https://imagemagick.org/script/license.php %
25 % %
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. %
31 % %
32 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33 %
34 %
35 */
36
37 /*
38 Include declarations.
39 */
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"
64
65 /*
66 Forward declarations.
67 */
68 static MagickBooleanType
69 WriteBRAILLEImage(const ImageInfo *,Image *,ExceptionInfo *);
70
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 % %
74 % %
75 % %
76 % R e g i s t e r B R A I L L E I m a g e %
77 % %
78 % %
79 % %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
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.
88 %
89 % The format of the RegisterBRAILLEImage method is:
90 %
91 % size_t RegisterBRAILLEImage(void)
92 %
93 */
RegisterBRAILLEImage(void)94 ModuleExport size_t RegisterBRAILLEImage(void)
95 {
96 MagickInfo
97 *entry;
98
99 entry=AcquireMagickInfo("BRAILLE","BRF","BRF ASCII Braille format");
100 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
101 entry->flags^=CoderAdjoinFlag;
102 (void) RegisterMagickInfo(entry);
103 entry=AcquireMagickInfo("BRAILLE","UBRL","Unicode Text format");
104 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
105 entry->flags^=CoderAdjoinFlag;
106 (void) RegisterMagickInfo(entry);
107 entry=AcquireMagickInfo("BRAILLE","UBRL6","Unicode Text format 6dot");
108 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
109 entry->flags^=CoderAdjoinFlag;
110 (void) RegisterMagickInfo(entry);
111 entry=AcquireMagickInfo("BRAILLE","ISOBRL","ISO/TR 11548-1 format");
112 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
113 entry->flags^=CoderAdjoinFlag;
114 (void) RegisterMagickInfo(entry);
115 entry=AcquireMagickInfo("BRAILLE","ISOBRL6","ISO/TR 11548-1 format 6dot");
116 entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
117 entry->flags^=CoderAdjoinFlag;
118 (void) RegisterMagickInfo(entry);
119 return(MagickImageCoderSignature);
120 }
121
122 /*
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % %
125 % %
126 % %
127 % U n r e g i s t e r B R A I L L E I m a g e %
128 % %
129 % %
130 % %
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
132 %
133 % UnregisterBRAILLEImage() removes format registrations made by the
134 % BRAILLE module from the list of supported formats.
135 %
136 % The format of the UnregisterBRAILLEImage method is:
137 %
138 % UnregisterBRAILLEImage(void)
139 %
140 */
UnregisterBRAILLEImage(void)141 ModuleExport void UnregisterBRAILLEImage(void)
142 {
143 (void) UnregisterMagickInfo("BRF");
144 (void) UnregisterMagickInfo("UBRL");
145 (void) UnregisterMagickInfo("UBRL6");
146 (void) UnregisterMagickInfo("ISOBRL");
147 (void) UnregisterMagickInfo("ISOBRL6");
148 }
149
150 /*
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % %
153 % %
154 % %
155 % W r i t e B R A I L L E I m a g e %
156 % %
157 % %
158 % %
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 %
161 % WriteBRAILLEImage() writes an image to a file in the Braille format.
162 %
163 % The format of the WriteBRAILLEImage method is:
164 %
165 % MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
166 % Image *image,ExceptionInfo *exception)
167 %
168 % A description of each parameter follows.
169 %
170 % o image_info: The image info.
171 %
172 % o image: The image.
173 %
174 % o exception: return any errors or warnings in this structure.
175 %
176 */
WriteBRAILLEImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)177 static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
178 Image *image,ExceptionInfo *exception)
179 {
180 #define do_cell(dx,dy,bit) \
181 { \
182 if (image->storage_class == PseudoClass) \
183 cell|=(GetPixelIndex(image,p+x+dx+dy*image->columns) == polarity) << bit; \
184 else \
185 cell|=(GetPixelGreen(image,p+x+dx+dy*image->columns) == 0) << bit; \
186 }
187
188 char
189 buffer[MagickPathExtent];
190
191 const char
192 *value;
193
194 int
195 unicode = 0,
196 iso_11548_1 = 0;
197
198 MagickBooleanType
199 status;
200
201 Quantum
202 polarity;
203
204 const Quantum
205 *p;
206
207 ssize_t
208 x;
209
210 size_t
211 cell_height = 4;
212
213 ssize_t
214 y;
215
216 /*
217 Open output image file.
218 */
219 assert(image_info != (const ImageInfo *) NULL);
220 assert(image_info->signature == MagickCoreSignature);
221 assert(image != (Image *) NULL);
222 assert(image->signature == MagickCoreSignature);
223 if (LocaleCompare(image_info->magick,"UBRL") == 0)
224 unicode=1;
225 else if (LocaleCompare(image_info->magick,"UBRL6") == 0)
226 {
227 unicode=1;
228 cell_height=3;
229 }
230 else
231 if (LocaleCompare(image_info->magick,"ISOBRL") == 0)
232 iso_11548_1=1;
233 else
234 if (LocaleCompare(image_info->magick,"ISOBRL6") == 0)
235 {
236 iso_11548_1=1;
237 cell_height=3;
238 }
239 else
240 cell_height=3;
241 if (image->debug != MagickFalse)
242 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
243 assert(exception != (ExceptionInfo *) NULL);
244 assert(exception->signature == MagickCoreSignature);
245 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
246 if (status == MagickFalse)
247 return(status);
248 if (!iso_11548_1)
249 {
250 value=GetImageProperty(image,"label",exception);
251 if (value != (const char *) NULL)
252 {
253 (void) FormatLocaleString(buffer,MagickPathExtent,"Title: %s\n",
254 value);
255 (void) WriteBlobString(image,buffer);
256 }
257 if (image->page.x != 0)
258 {
259 (void) FormatLocaleString(buffer,MagickPathExtent,"X: %.20g\n",
260 (double) image->page.x);
261 (void) WriteBlobString(image,buffer);
262 }
263 if (image->page.y != 0)
264 {
265 (void) FormatLocaleString(buffer,MagickPathExtent,"Y: %.20g\n",
266 (double) image->page.y);
267 (void) WriteBlobString(image,buffer);
268 }
269 (void) FormatLocaleString(buffer,MagickPathExtent,"Width: %.20g\n",
270 (double) (image->columns+(image->columns % 2)));
271 (void) WriteBlobString(image,buffer);
272 (void) FormatLocaleString(buffer,MagickPathExtent,"Height: %.20g\n",
273 (double) image->rows);
274 (void) WriteBlobString(image,buffer);
275 (void) WriteBlobString(image,"\n");
276 }
277 (void) SetImageType(image,BilevelType,exception);
278 polarity=0;
279 if (image->storage_class == PseudoClass)
280 {
281 polarity=(Quantum) (GetPixelInfoIntensity(image,&image->colormap[0]) >=
282 (QuantumRange/2.0));
283 if (image->colors == 2)
284 polarity=(Quantum) (GetPixelInfoIntensity(image,&image->colormap[0]) >=
285 GetPixelInfoIntensity(image,&image->colormap[1]));
286 }
287 for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) cell_height)
288 {
289 if ((y+cell_height) > image->rows)
290 cell_height=(size_t) (image->rows-y);
291 p=GetVirtualPixels(image,0,y,image->columns,cell_height,exception);
292 if (p == (const Quantum *) NULL)
293 break;
294 for (x=0; x < (ssize_t) image->columns; x+=2)
295 {
296 MagickBooleanType
297 two_columns;
298
299 unsigned char
300 cell = 0;
301
302 two_columns=(x+1 < (ssize_t) image->columns) ? MagickTrue : MagickFalse;
303 do_cell(0,0,0)
304 if (two_columns != MagickFalse)
305 do_cell(1,0,3)
306 if (cell_height > 1)
307 {
308 do_cell(0,1,1)
309 if (two_columns != MagickFalse)
310 do_cell(1,1,4)
311 if (cell_height > 2)
312 {
313 do_cell(0,2,2)
314 if (two_columns != MagickFalse)
315 do_cell(1,2,5)
316 if (cell_height > 3)
317 {
318 do_cell(0,3,6)
319 if (two_columns != MagickFalse)
320 do_cell(1,3,7)
321 }
322 }
323 }
324 if (unicode != 0)
325 {
326 unsigned char
327 utf8[3];
328
329 /*
330 Unicode text.
331 */
332 utf8[0]=(unsigned char) (0xe0|((0x28 >> 4) & 0x0f));
333 utf8[1]=0x80|((0x28<<2) & 0x3f)|(cell >> 6);
334 utf8[2]=0x80|(cell & 0x3f);
335 (void) WriteBlob(image,3,utf8);
336 }
337 else if (iso_11548_1)
338 {
339 /*
340 ISO/TR 11548-1 binary.
341 */
342 (void) WriteBlobByte(image,cell);
343 }
344 else
345 {
346 static const unsigned char
347 iso_to_brf[64] =
348 {
349 ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
350 '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
351 '"', 'E', '3', 'H', '9', 'O', '6', 'R',
352 '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
353 ',', '*', '5', '<', '-', 'U', '8', 'V',
354 '.', '%', '[', '$', '+', 'X', '!', '&',
355 ';', ':', '4', '\\', '0', 'Z', '7', '(',
356 '_', '?', 'W', ']', '#', 'Y', ')', '='
357 };
358
359 /*
360 BRF.
361 */
362 (void) WriteBlobByte(image,iso_to_brf[cell]);
363 }
364 }
365 if (iso_11548_1 == 0)
366 (void) WriteBlobByte(image,'\n');
367 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
368 image->rows);
369 if (status == MagickFalse)
370 break;
371 }
372 (void) CloseBlob(image);
373 return(MagickTrue);
374 }
375