Debian Codegraph @

converter/pbm/pbmtodjvurle.c

raw
1 /*
2 Convert a PBM image into the DjVu Bitonal RLE format
3 described in the csepdjvu(1) documentation
4
5 Copyright (c) 2004 Scott Pakin <[email protected]>
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 1. Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15 2. Redistributions in binary form must reproduce the above copyright
16 notice, this list of conditions and the following disclaimer in the
17 documentation and/or other materials provided with the distribution.
18 3. The name of the author may not be used to endorse or promote products
19 derived from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36
37 #include "pbm.h"
38
39
40 /* Write a byte to a file and check for errors. */
41 static void
42 writebyte(FILE * const ofP,
43 unsigned char const c) {
44
45 if (fputc (c, ofP) == EOF)
46 pm_error ("failed to write to the RLE file. Errno=%d (%s)",
47 errno, strerror(errno));
48 }
49
50
51
52 /* Write a run length to the RLE file. */
53 static void
54 write_rle(FILE * const rlefile,
55 uint32_t const tallyArg) {
56
57 uint32_t remainingTally;
58
59 remainingTally = tallyArg; /* initial value */
60
61 do {
62 /* Output a single run. */
63 if (remainingTally < 192) {
64 /* Single-byte runs */
65 writebyte (rlefile, remainingTally);
66 remainingTally >>= 8;
67 }
68 else {
69 /* Two-byte runs */
70 writebyte (rlefile, ((remainingTally>>8) & 0x3F) + 0xC0);
71 writebyte (rlefile, remainingTally & 0xFF);
72 remainingTally >>= 14;
73 }
74
75 /* Very large runs need to be split into smaller runs. We
76 therefore need to toggle back to the same color we had for the
77 previous smaller run.
78 */
79 if (remainingTally > 0)
80 writebyte (rlefile, 0);
81 }
82 while (remainingTally > 0);
83 }
84
85
86
87 int
88 main (int argc, const char * argv[]) {
89
90 FILE * const rlefile = stdout; /* Generated Bitonal RLE file */
91
92 FILE * pbmfile; /* PBM file to convert */
93 int numcols, numrows; /* Width and height in pixels of the PBM file */
94 int format; /* Original image type before conversion to PBM */
95 bit * pbmrow; /* One row of the PBM file */
96 unsigned int row;
97 const char * pbmfilename; /* Name of input file */
98
99 pm_proginit(&argc, argv);
100
101 if (argc-1 < 1)
102 pbmfilename = "-";
103 else if (argc-1 == 1)
104 pbmfilename = argv[1];
105 else
106 pm_error("Program takes at most 1 argument -- the input file name. "
107 "You specified %d", argc-1);
108
109 pbmfile = pm_openr(pbmfilename);
110
111 /* Write an RLE header. */
112 pbm_readpbminit(pbmfile, &numcols, &numrows, &format);
113 fprintf(rlefile, "R4\n");
114 fprintf(rlefile, "%d %d\n", numcols, numrows);
115
116 /* Write the RLE data. */
117 pbmrow = pbm_allocrow(numcols);
118 for (row = 0; row < numrows; ++row) {
119 unsigned int col;
120 uint32_t pixeltally; /* Run length of the current color */
121 bit prevpixel; /* Previous pixel seen */
122
123 pbm_readpbmrow(pbmfile, pbmrow, numcols, format);
124 prevpixel = PBM_WHITE; /* Bitonal RLE rows always start with white */
125 pixeltally = 0;
126 for (col = 0; col < numcols; ++col) {
127 bit newpixel = pbmrow[col]; /* Current pixel color */
128
129 if (newpixel == prevpixel)
130 ++pixeltally;
131 else {
132 write_rle(rlefile, pixeltally);
133 pixeltally = 1;
134 prevpixel = newpixel;
135 }
136 }
137 write_rle(rlefile, pixeltally);
138 }
139
140 pbm_freerow(pbmrow);
141 if (rlefile != stdout)
142 pm_close(rlefile);
143 if (pbmfile != stdin)
144 pm_close(pbmfile);
145
146 return 0;
147 }
148
149
150