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