Menu

[d59cce]: / coders / braille.c  Maximize  Restore  History

Download this file

317 lines (295 with data), 10.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
% Copyright (C) 2019 GraphicsMagick Group
%
% This program is covered by multiple licenses, which are described in
% Copyright.txt. You should have received a copy of Copyright.txt with this
% package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% BBBB RRRR AAA IIIII L L EEEEE %
% B B R R A A I L L E %
% BBBB RRRR AAAAA I L L EEE %
% B B R R A A I L L E %
% BBBB R R A A IIIII LLLLL LLLLL EEEEE %
% %
% %
% Read/Write Braille Format %
% %
% Samuel Thibault %
% February 2008 %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/magick.h"
#include "magick/pixel_cache.h"
#include "magick/utility.h"
/*
Forward declarations.
*/
static unsigned int
WriteBRAILLEImage(const ImageInfo *,Image *);
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e g i s t e r B R A I L L E I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RegisterBRAILLEImage() adds values for the Braille format to
% the list of supported formats. The values include the image format
% tag, a method to read and/or write the format, whether the format
% supports the saving of more than one frame to the same file or blob,
% whether the format supports native in-memory I/O, and a brief
% description of the format.
%
% The format of the RegisterBRAILLEImage method is:
%
% size_t RegisterBRAILLEImage(void)
%
*/
ModuleExport void RegisterBRAILLEImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("BRF");
entry->encoder=(EncoderHandler) WriteBRAILLEImage;
entry->adjoin=False;
entry->description="BRF ASCII Braille format";
entry->module="BRAILLE";
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("UBRL");
entry->encoder=(EncoderHandler) WriteBRAILLEImage;
entry->adjoin=False;
entry->description="Unicode Text format";
entry->module="BRAILLE";
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("UBRL6");
entry->encoder=(EncoderHandler) WriteBRAILLEImage;
entry->adjoin=False;
entry->description="Unicode Text format 6dot";
entry->module="BRAILLE";
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("ISOBRL");
entry->encoder=(EncoderHandler) WriteBRAILLEImage;
entry->adjoin=False;
entry->description="ISO/TR 11548-1 format";
entry->module="BRAILLE";
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("ISOBRL6");
entry->encoder=(EncoderHandler) WriteBRAILLEImage;
entry->adjoin=False;
entry->description="ISO/TR 11548-1 format 6dot";
entry->module="BRAILLE";
(void) RegisterMagickInfo(entry);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U n r e g i s t e r B R A I L L E I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% UnregisterBRAILLEImage() removes format registrations made by the
% BRAILLE module from the list of supported formats.
%
% The format of the UnregisterBRAILLEImage method is:
%
% UnregisterBRAILLEImage(void)
%
*/
ModuleExport void UnregisterBRAILLEImage(void)
{
(void) UnregisterMagickInfo("BRF");
(void) UnregisterMagickInfo("UBRL");
(void) UnregisterMagickInfo("UBRL6");
(void) UnregisterMagickInfo("ISOBRL");
(void) UnregisterMagickInfo("ISOBRL6");
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% W r i t e B R A I L L E I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% WriteBRAILLEImage() writes an image to a file in the Braille format.
%
% The format of the WriteBRAILLEImage method is:
%
% unsigned int WriteBRAILLEImage(const ImageInfo *image_info,
% Image *image)
%
% A description of each parameter follows.
%
% o image_info: The image info.
%
% o image: The image.
%
*/
static unsigned int WriteBRAILLEImage(const ImageInfo *image_info,
Image *image)
{
char
buffer[MaxTextExtent];
IndexPacket
polarity;
int
unicode = 0,
iso_11548_1 = 0;
unsigned int
status;
register const IndexPacket
*indexes;
register const PixelPacket
*p;
register unsigned long
x;
unsigned long
cell_height = 4;
unsigned long
y;
/*
Open output image file.
*/
assert(image_info != (const ImageInfo *) NULL);
assert(image != (Image *) NULL);
if (LocaleCompare(image_info->magick,"UBRL") == 0)
unicode=1;
else if (LocaleCompare(image_info->magick,"UBRL6") == 0)
{
unicode=1;
cell_height=3;
}
else if (LocaleCompare(image_info->magick,"ISOBRL") == 0)
iso_11548_1=1;
else if (LocaleCompare(image_info->magick,"ISOBRL6") == 0)
{
iso_11548_1=1;
cell_height=3;
}
else
cell_height=3;
status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
if (status == False)
return(status);
if (!iso_11548_1)
{
if (image->page.x != 0)
{
(void) FormatString(buffer,"X: %.20g\n",(double)
image->page.x);
(void) WriteBlobString(image,buffer);
}
if (image->page.y != 0)
{
(void) FormatString(buffer,"Y: %.20g\n",(double)
image->page.y);
(void) WriteBlobString(image,buffer);
}
(void) FormatString(buffer,"Width: %.20g\n",(double)
image->columns+(image->columns % 2));
(void) WriteBlobString(image,buffer);
(void) FormatString(buffer,"Height: %.20g\n",(double)
image->rows);
(void) WriteBlobString(image,buffer);
(void) WriteBlobString(image,"\n");
}
(void) SetImageType(image,BilevelType);
polarity=PixelIntensityToQuantum(&image->colormap[0]) >= (MaxRGB/2);
if (image->colors == 2)
polarity=PixelIntensityToQuantum(&image->colormap[0]) >=
PixelIntensityToQuantum(&image->colormap[1]);
for (y=0; y < image->rows; y+=cell_height)
{
if ((y+cell_height) > image->rows)
cell_height = (image->rows-y);
p=AcquireImagePixels(image,0,y,image->columns,cell_height,&image->exception);
if (p == (const PixelPacket *) NULL)
break;
indexes=AccessImmutableIndexes(image);
for (x=0; x < image->columns; x+=2)
{
unsigned char cell = 0;
unsigned long two_columns = x+1 < image->columns;
do
{
#define do_cell(dx,dy,bit) do { \
cell |= (indexes[x+dx+dy*image->columns] == polarity) << bit; \
} while (0)
do_cell(0,0,0);
if (two_columns)
do_cell(1,0,3);
if (cell_height < 2)
break;
do_cell(0,1,1);
if (two_columns)
do_cell(1,1,4);
if (cell_height < 3)
break;
do_cell(0,2,2);
if (two_columns)
do_cell(1,2,5);
if (cell_height < 4)
break;
do_cell(0,3,6);
if (two_columns)
do_cell(1,3,7);
} while(0);
if (unicode)
{
unsigned char utf8[3];
/* Unicode text */
utf8[0] = (unsigned char) (0xe0|((0x28>>4)&0x0f));
utf8[1] = 0x80|((0x28<<2)&0x3f)|(cell>>6);
utf8[2] = 0x80|(cell&0x3f);
(void) WriteBlob(image,3,utf8);
}
else if (iso_11548_1)
{
/* ISO/TR 11548-1 binary */
(void) WriteBlobByte(image,cell);
}
else
{
/* BRF */
static const unsigned char iso_to_brf[64] = {
' ', 'A', '1', 'B', '\'', 'K', '2', 'L',
'@', 'C', 'I', 'F', '/', 'M', 'S', 'P',
'"', 'E', '3', 'H', '9', 'O', '6', 'R',
'^', 'D', 'J', 'G', '>', 'N', 'T', 'Q',
',', '*', '5', '<', '-', 'U', '8', 'V',
'.', '%', '[', '$', '+', 'X', '!', '&',
';', ':', '4', '\\', '0', 'Z', '7', '(',
'_', '?', 'W', ']', '#', 'Y', ')', '='
};
(void) WriteBlobByte(image,iso_to_brf[cell]);
}
}
if (iso_11548_1 == 0)
(void) WriteBlobByte(image,'\n');
}
status &= CloseBlob(image);
return(status);
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.