root / ImageMagick / trunk / coders / braille.c

Revision 12033, 11.8 kB (checked in by cristy, 6 days ago)
Line 
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-2008 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
37/*
38  Include declarations.
39*/
40#include "magick/studio.h"
41#include "magick/blob.h"
42#include "magick/blob-private.h"
43#include "magick/color-private.h"
44#include "magick/colorspace.h"
45#include "magick/constitute.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/image.h"
49#include "magick/image-private.h"
50#include "magick/list.h"
51#include "magick/magick.h"
52#include "magick/memory_.h"
53#include "magick/module.h"
54#include "magick/monitor.h"
55#include "magick/monitor-private.h"
56#include "magick/property.h"
57#include "magick/quantize.h"
58#include "magick/static.h"
59#include "magick/string_.h"
60#include "magick/utility.h"
61
62/*
63  Forward declarations.
64*/
65static MagickBooleanType
66  WriteBRAILLEImage(const ImageInfo *,Image *);
67
68/*
69%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70%                                                                             %
71%                                                                             %
72%                                                                             %
73%   R e g i s t e r B R A I L L E I m a g e                                   %
74%                                                                             %
75%                                                                             %
76%                                                                             %
77%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78%
79%  RegisterBRAILLEImage() adds values for the Braille format to
80%  the list of supported formats.  The values include the image format
81%  tag, a method to read and/or write the format, whether the format
82%  supports the saving of more than one frame to the same file or blob,
83%  whether the format supports native in-memory I/O, and a brief
84%  description of the format.
85%
86%  The format of the RegisterBRAILLEImage method is:
87%
88%      unsigned long RegisterBRAILLEImage(void)
89%
90*/
91ModuleExport unsigned long RegisterBRAILLEImage(void)
92{
93  MagickInfo
94    *entry;
95
96  entry=SetMagickInfo("BRF");
97  entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
98  entry->adjoin=MagickFalse;
99  entry->description=AcquireString("BRF ASCII Braille format");
100  entry->module=AcquireString("BRAILLE");
101  (void) RegisterMagickInfo(entry);
102  entry=SetMagickInfo("UBRL");
103  entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
104  entry->adjoin=MagickFalse;
105  entry->description=AcquireString("Unicode Text format");
106  entry->module=AcquireString("BRAILLE");
107  (void) RegisterMagickInfo(entry);
108  entry=SetMagickInfo("ISOBRL");
109  entry->encoder=(EncodeImageHandler *) WriteBRAILLEImage;
110  entry->adjoin=MagickFalse;
111  entry->description=AcquireString("ISO/TR 11548-1 format");
112  entry->module=AcquireString("BRAILLE");
113  (void) RegisterMagickInfo(entry);
114  return(MagickImageCoderSignature);
115}
116
117/*
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119%                                                                             %
120%                                                                             %
121%                                                                             %
122%   U n r e g i s t e r B R A I L L E I m a g e                               %
123%                                                                             %
124%                                                                             %
125%                                                                             %
126%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127%
128%  UnregisterBRAILLEImage() removes format registrations made by the
129%  BRAILLE module from the list of supported formats.
130%
131%  The format of the UnregisterBRAILLEImage method is:
132%
133%      UnregisterBRAILLEImage(void)
134%
135*/
136ModuleExport void UnregisterBRAILLEImage(void)
137{
138  (void) UnregisterMagickInfo("BRF");
139  (void) UnregisterMagickInfo("UBRL");
140  (void) UnregisterMagickInfo("ISOBRL");
141}
142
143/*
144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145%                                                                             %
146%                                                                             %
147%                                                                             %
148%   W r i t e B R A I L L E I m a g e                                         %
149%                                                                             %
150%                                                                             %
151%                                                                             %
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153%
154%  WriteBRAILLEImage() writes an image to a file in the Braille format.
155%
156%  The format of the WriteBRAILLEImage method is:
157%
158%      MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
159%        Image *image)
160%
161%  A description of each parameter follows.
162%
163%    o image_info: The image info.
164%
165%    o image:  The image.
166%
167*/
168static MagickBooleanType WriteBRAILLEImage(const ImageInfo *image_info,
169  Image *image)
170{
171  char
172    buffer[MaxTextExtent];
173
174  const char
175    *value;
176
177  long
178    y;
179
180  MagickBooleanType
181    status;
182
183  register const PixelPacket
184    *p;
185
186  IndexPacket
187    polarity, *indexes;
188
189  register long
190    x;
191
192  unsigned long
193    cell_height = 4;
194  int
195    unicode = 0, iso_11548_1 = 0;
196
197  /*
198    Open output image file.
199  */
200  assert(image_info != (const ImageInfo *) NULL);
201  assert(image_info->signature == MagickSignature);
202  assert(image != (Image *) NULL);
203  assert(image->signature == MagickSignature);
204
205  if (LocaleCompare(image_info->magick, "UBRL") == 0)
206    unicode = 1;
207  else
208    if (LocaleCompare(image_info->magick, "ISOBRL") == 0)
209      iso_11548_1 = 1;
210    else
211      cell_height = 3;
212
213  if (image->debug != MagickFalse)
214    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
215  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
216  if (status == MagickFalse)
217    return(status);
218
219  if (!iso_11548_1)
220    {
221      value=GetImageProperty(image,"Label");
222      if (value != (const char *) NULL) {
223        (void) FormatMagickString(buffer,MaxTextExtent,"Title: %s\n", value);
224        (void) WriteBlobString(image,buffer);
225      }
226      if (image->page.x)
227      {
228        (void) FormatMagickString(buffer,MaxTextExtent,"X: %ld\n", image->page.x);
229        (void) WriteBlobString(image,buffer);
230      }
231      if (image->page.y)
232      {
233        (void) FormatMagickString(buffer,MaxTextExtent,"Y: %ld\n", image->page.y);
234        (void) WriteBlobString(image,buffer);
235      }
236      (void) FormatMagickString(buffer,MaxTextExtent,"Width: %lu\n",
237        image->columns+(image->columns % 2));
238      (void) WriteBlobString(image,buffer);
239      (void) FormatMagickString(buffer,MaxTextExtent,"Height: %lu\n", image->rows);
240      (void) WriteBlobString(image,buffer);
241
242      (void) WriteBlobString(image,"\n");
243    }
244
245  (void) SetImageType(image,BilevelType);
246  polarity=(IndexPacket) (PixelIntensityToQuantum(&image->colormap[0]) >=
247    (Quantum) (QuantumRange/2));
248  if (image->colors == 2)
249    polarity=(IndexPacket)
250      (PixelIntensityToQuantum(&image->colormap[0]) >=
251       PixelIntensityToQuantum(&image->colormap[1]));
252  for (y=0; y < (long) image->rows; y+=cell_height)
253  {
254    if ((y+cell_height) > image->rows)
255      cell_height = (unsigned long) (image->rows-y);
256
257    p=AcquireImagePixels(image,0,y,image->columns,cell_height,&image->exception);
258    if (p == (const PixelPacket *) NULL)
259      break;
260    indexes=GetIndexes(image);
261
262    for (x=0; x < (long) image->columns; x+=2)
263    {
264      unsigned char cell = 0;
265      int two_columns = x+1 < (long) image->columns;
266
267      do
268      {
269#define do_cell(dx,dy,bit) \
270        cell |= (indexes[x+dx+dy*image->columns]==polarity)<<bit;
271
272        do_cell(0,0,0);
273        if (two_columns)
274          do_cell(1,0,3);
275        if (cell_height < 2)
276          break;
277
278        do_cell(0,1,1);
279        if (two_columns)
280          do_cell(1,1,4);
281        if (cell_height < 3)
282          break;
283
284        do_cell(0,2,2);
285        if (two_columns)
286          do_cell(1,2,5);
287        if (cell_height < 4)
288          break;
289
290        do_cell(0,3,6);
291        if (two_columns)
292          do_cell(1,3,7);
293      } while(0);
294
295      if (unicode)
296        {
297          unsigned char utf8[3];
298          /* Unicode text */
299          utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
300          utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
301          utf8[2] = 0x80|(cell&0x3f);
302          (void) WriteBlob(image,3,utf8);
303        }
304      else if (iso_11548_1)
305        {
306          /* ISO/TR 11548-1 binary */
307          (void) WriteBlobByte(image,cell);
308        }
309      else
310        {
311          /* BRF */
312          static const unsigned char iso_to_brf[64] = {
313            ' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
314            '@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
315            '"', 'E', '3', 'H', '9', 'O', '6', 'R',
316            '^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
317            ',', '*', '5', '<', '-', 'U', '8', 'V',
318            '.', '%', '[', '$', '+', 'X', '!', '&',
319            ';', ':', '4', '\\', '0', 'Z', '7', '(',
320            '_', '?', 'W', ']', '#', 'Y', ')', '='
321          };
322          (void) WriteBlobByte(image,iso_to_brf[cell]);
323        }
324    }
325    if (!iso_11548_1)
326      (void) WriteBlobByte(image,'\n');
327    image=SyncNextImageInList(image);
328    status=SetImageProgress(image,SaveImageTag,y,image->rows);
329    if (status == MagickFalse)
330      break;
331  }
332  (void) CloseBlob(image);
333  return(MagickTrue);
334}
Note: See TracBrowser for help on using the browser.