]> granicus.if.org Git - imagemagick/blob - coders/braille.c
ec930d6ed0a5b6b9ac5ec949f3d91e7ae6591f79
[imagemagick] / coders / braille.c
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-2012 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 %    http://www.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 \f
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 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteBRAILLEImage(const ImageInfo *,Image *,ExceptionInfo *);
70 \f
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 */
94 ModuleExport size_t RegisterBRAILLEImage(void)
95 {
96   MagickInfo
97     *entry;
98
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);
118 }
119 \f
120 /*
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 %                                                                             %
123 %                                                                             %
124 %                                                                             %
125 %   U n r e g i s t e r B R A I L L E I m a g e                               %
126 %                                                                             %
127 %                                                                             %
128 %                                                                             %
129 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 %
131 %  UnregisterBRAILLEImage() removes format registrations made by the
132 %  BRAILLE module from the list of supported formats.
133 %
134 %  The format of the UnregisterBRAILLEImage method is:
135 %
136 %      UnregisterBRAILLEImage(void)
137 %
138 */
139 ModuleExport void UnregisterBRAILLEImage(void)
140 {
141   (void) UnregisterMagickInfo("BRF");
142   (void) UnregisterMagickInfo("UBRL");
143   (void) UnregisterMagickInfo("ISOBRL");
144 }
145 \f
146 /*
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %                                                                             %
149 %                                                                             %
150 %                                                                             %
151 %   W r i t e B R A I L L E I m a g e                                         %
152 %                                                                             %
153 %                                                                             %
154 %                                                                             %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 %
157 %  WriteBRAILLEImage() writes an image to a file in the Braille format.
158 %
159 %  The format of the WriteBRAILLEImage method is:
160 %
161 %      MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
162 %        Image *image,ExceptionInfo *exception)
163 %
164 %  A description of each parameter follows.
165 %
166 %    o image_info: The image info.
167 %
168 %    o image:  The image.
169 %
170 %    o exception: return any errors or warnings in this structure.
171 %
172 */
173 static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
174   Image *image,ExceptionInfo *exception)
175 {
176   char
177     buffer[MaxTextExtent];
178
179   const char
180     *value;
181
182   int
183     unicode = 0,
184     iso_11548_1 = 0;
185
186   MagickBooleanType
187     status;
188
189   Quantum
190     polarity;
191
192   register const Quantum
193     *p;
194
195   register ssize_t
196     x;
197
198   size_t
199     cell_height = 4;
200
201   ssize_t
202     y;
203
204   /*
205     Open output image file.
206   */
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)
212     unicode=1;
213   else
214     if (LocaleCompare(image_info->magick, "ISOBRL") == 0)
215       iso_11548_1=1;
216     else
217       cell_height=3;
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)
224     return(status);
225   if (!iso_11548_1)
226     {
227       value=GetImageProperty(image,"label",exception);
228       if (value != (const char *) NULL)
229         {
230           (void) FormatLocaleString(buffer,MaxTextExtent,"Title: %s\n", value);
231           (void) WriteBlobString(image,buffer);
232         }
233       if (image->page.x != 0)
234         {
235           (void) FormatLocaleString(buffer,MaxTextExtent,"X: %.20g\n",(double) 
236             image->page.x);
237           (void) WriteBlobString(image,buffer);
238         }
239       if (image->page.y != 0)
240         {
241           (void) FormatLocaleString(buffer,MaxTextExtent,"Y: %.20g\n",(double) 
242             image->page.y);
243           (void) WriteBlobString(image,buffer);
244         }
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)
249         image->rows);
250       (void) WriteBlobString(image,buffer);
251       (void) WriteBlobString(image,"\n");
252     }
253   (void) SetImageType(image,BilevelType,exception);
254   polarity = 0;
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]));
261   }
262   for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) cell_height)
263   {
264     if ((y+cell_height) > image->rows)
265       cell_height = (size_t) (image->rows-y);
266
267     p=GetVirtualPixels(image,0,y,image->columns,cell_height,exception);
268     if (p == (const Quantum *) NULL)
269       break;
270     for (x=0; x < (ssize_t) image->columns; x+=2)
271     {
272       unsigned char cell = 0;
273       int two_columns = x+1 < (ssize_t) image->columns;
274
275       do
276       {
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; \
280         else \
281           cell |= (GetPixelGreen(image,p+x+dx+dy*image->columns) == 0) << bit; \
282 } while (0) 
283
284         do_cell(0,0,0);
285         if (two_columns)
286           do_cell(1,0,3);
287         if (cell_height < 2)
288           break;
289
290         do_cell(0,1,1);
291         if (two_columns)
292           do_cell(1,1,4);
293         if (cell_height < 3)
294           break;
295
296         do_cell(0,2,2);
297         if (two_columns)
298           do_cell(1,2,5);
299         if (cell_height < 4)
300           break;
301
302         do_cell(0,3,6);
303         if (two_columns)
304           do_cell(1,3,7);
305       } while(0);
306
307       if (unicode)
308         {
309           unsigned char utf8[3];
310           /* Unicode text */
311           utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
312           utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
313           utf8[2] = 0x80|(cell&0x3f);
314           (void) WriteBlob(image,3,utf8);
315         }
316       else if (iso_11548_1)
317         {
318           /* ISO/TR 11548-1 binary */
319           (void) WriteBlobByte(image,cell);
320         }
321       else
322         {
323           /* BRF */
324           static const unsigned char iso_to_brf[64] = {
325             ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
326             '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
327             '"', 'E', '3', 'H', '9', 'O', '6', 'R',
328             '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
329             ',', '*', '5', '<', '-', 'U', '8', 'V',
330             '.', '%', '[', '$', '+', 'X', '!', '&',
331             ';', ':', '4', '\\', '0', 'Z', '7', '(',
332             '_', '?', 'W', ']', '#', 'Y', ')', '='
333           };
334           (void) WriteBlobByte(image,iso_to_brf[cell]);
335         }
336     }
337     if (iso_11548_1 == 0)
338       (void) WriteBlobByte(image,'\n');
339     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
340       image->rows);
341     if (status == MagickFalse)
342       break;
343   }
344   (void) CloseBlob(image);
345   return(MagickTrue);
346 }