]> 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   assert(exception != (ExceptionInfo *) NULL);
220   assert(exception->signature == MagickSignature);
221   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
222   if (status == MagickFalse)
223     return(status);
224   if (!iso_11548_1)
225     {
226       value=GetImageProperty(image,"label");
227       if (value != (const char *) NULL)
228         {
229           (void) FormatLocaleString(buffer,MaxTextExtent,"Title: %s\n", value);
230           (void) WriteBlobString(image,buffer);
231         }
232       if (image->page.x != 0)
233         {
234           (void) FormatLocaleString(buffer,MaxTextExtent,"X: %.20g\n",(double) 
235             image->page.x);
236           (void) WriteBlobString(image,buffer);
237         }
238       if (image->page.y != 0)
239         {
240           (void) FormatLocaleString(buffer,MaxTextExtent,"Y: %.20g\n",(double) 
241             image->page.y);
242           (void) WriteBlobString(image,buffer);
243         }
244       (void) FormatLocaleString(buffer,MaxTextExtent,"Width: %.20g\n",(double)
245         (image->columns+(image->columns % 2)));
246       (void) WriteBlobString(image,buffer);
247       (void) FormatLocaleString(buffer,MaxTextExtent,"Height: %.20g\n",(double)
248         image->rows);
249       (void) WriteBlobString(image,buffer);
250       (void) WriteBlobString(image,"\n");
251     }
252   (void) SetImageType(image,BilevelType,exception);
253   polarity = 0;
254   if (image->storage_class == PseudoClass) {
255     polarity=(Quantum) (GetPixelPacketIntensity(&image->colormap[0]) >=
256       (Quantum) (QuantumRange/2));
257     if (image->colors == 2)
258       polarity=(Quantum) (GetPixelPacketIntensity(&image->colormap[0]) >=
259         GetPixelPacketIntensity(&image->colormap[1]));
260   }
261   for (y=0; y < (ssize_t) image->rows; y+=(ssize_t) cell_height)
262   {
263     if ((y+cell_height) > image->rows)
264       cell_height = (size_t) (image->rows-y);
265
266     p=GetVirtualPixels(image,0,y,image->columns,cell_height,exception);
267     if (p == (const Quantum *) NULL)
268       break;
269     for (x=0; x < (ssize_t) image->columns; x+=2)
270     {
271       unsigned char cell = 0;
272       int two_columns = x+1 < (ssize_t) image->columns;
273
274       do
275       {
276 #define do_cell(dx,dy,bit) do { \
277         if (image->storage_class == PseudoClass) \
278           cell |= (GetPixelIndex(image,p+x+dx+dy*image->columns) == polarity) << bit; \
279         else \
280           cell |= (GetPixelGreen(image,p+x+dx+dy*image->columns) == 0) << bit; \
281 } while (0) 
282
283         do_cell(0,0,0);
284         if (two_columns)
285           do_cell(1,0,3);
286         if (cell_height < 2)
287           break;
288
289         do_cell(0,1,1);
290         if (two_columns)
291           do_cell(1,1,4);
292         if (cell_height < 3)
293           break;
294
295         do_cell(0,2,2);
296         if (two_columns)
297           do_cell(1,2,5);
298         if (cell_height < 4)
299           break;
300
301         do_cell(0,3,6);
302         if (two_columns)
303           do_cell(1,3,7);
304       } while(0);
305
306       if (unicode)
307         {
308           unsigned char utf8[3];
309           /* Unicode text */
310           utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
311           utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
312           utf8[2] = 0x80|(cell&0x3f);
313           (void) WriteBlob(image,3,utf8);
314         }
315       else if (iso_11548_1)
316         {
317           /* ISO/TR 11548-1 binary */
318           (void) WriteBlobByte(image,cell);
319         }
320       else
321         {
322           /* BRF */
323           static const unsigned char iso_to_brf[64] = {
324             ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
325             '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
326             '"', 'E', '3', 'H', '9', 'O', '6', 'R',
327             '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
328             ',', '*', '5', '<', '-', 'U', '8', 'V',
329             '.', '%', '[', '$', '+', 'X', '!', '&',
330             ';', ':', '4', '\\', '0', 'Z', '7', '(',
331             '_', '?', 'W', ']', '#', 'Y', ')', '='
332           };
333           (void) WriteBlobByte(image,iso_to_brf[cell]);
334         }
335     }
336     if (iso_11548_1 == 0)
337       (void) WriteBlobByte(image,'\n');
338     status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
339       image->rows);
340     if (status == MagickFalse)
341       break;
342   }
343   (void) CloseBlob(image);
344   return(MagickTrue);
345 }