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 <scott+pbm@pakin.org>
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
writebyte(FILE * const ofP,unsigned char const c)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 /* Write a run length to the RLE file. */
52 static void
write_rle(FILE * const rlefile,uint32_t const tallyArg)53 write_rle(FILE *   const rlefile,
54           uint32_t const tallyArg) {
55 
56     uint32_t remainingTally;
57 
58     remainingTally = tallyArg;  /* initial value */
59 
60     do {
61         /* Output a single run. */
62         if (remainingTally < 192) {
63             /* Single-byte runs */
64             writebyte (rlefile, remainingTally);
65             remainingTally >>= 8;
66         }
67         else {
68             /* Two-byte runs */
69             writebyte (rlefile, ((remainingTally>>8) & 0x3F) + 0xC0);
70             writebyte (rlefile, remainingTally & 0xFF);
71             remainingTally >>= 14;
72         }
73 
74         /* Very large runs need to be split into smaller runs.  We
75            therefore need to toggle back to the same color we had for the
76            previous smaller run.
77         */
78         if (remainingTally > 0)
79             writebyte (rlefile, 0);
80     }
81     while (remainingTally > 0);
82 }
83 
84 
85 
86 int
main(int argc,const char * argv[])87 main (int argc, const char * argv[]) {
88 
89     FILE * const rlefile = stdout; /* Generated Bitonal RLE file */
90 
91     FILE * pbmfile;          /* PBM file to convert */
92     int numcols, numrows;    /* Width and height in pixels of the PBM file */
93     int format;              /* Original image type before conversion to PBM */
94     bit * pbmrow;            /* One row of the PBM file */
95     unsigned int row;
96     const char * pbmfilename;  /* Name of input file */
97 
98     pm_proginit(&argc, argv);
99 
100     if (argc-1 < 1)
101         pbmfilename = "-";
102     else if (argc-1 == 1)
103         pbmfilename = argv[1];
104     else
105         pm_error("Program takes at most 1 argument -- the input file name.  "
106                  "You specified %d", argc-1);
107 
108     pbmfile = pm_openr(pbmfilename);
109 
110     /* Write an RLE header. */
111     pbm_readpbminit(pbmfile, &numcols, &numrows, &format);
112     fprintf(rlefile, "R4\n");
113     fprintf(rlefile, "%d %d\n", numcols, numrows);
114 
115     /* Write the RLE data. */
116     pbmrow = pbm_allocrow(numcols);
117     for (row = 0; row < numrows; ++row) {
118         unsigned int col;
119         uint32_t pixeltally;   /* Run length of the current color */
120         bit prevpixel;         /* Previous pixel seen */
121 
122         pbm_readpbmrow(pbmfile, pbmrow, numcols, format);
123         prevpixel = PBM_WHITE;   /* Bitonal RLE rows always start with white */
124         pixeltally = 0;
125         for (col = 0; col < numcols; ++col) {
126             bit newpixel = pbmrow[col];      /* Current pixel color */
127 
128             if (newpixel == prevpixel)
129                 ++pixeltally;
130             else {
131                 write_rle(rlefile, pixeltally);
132                 pixeltally = 1;
133                 prevpixel = newpixel;
134             }
135         }
136         write_rle(rlefile, pixeltally);
137     }
138 
139     pbm_freerow(pbmrow);
140     if (rlefile != stdout)
141         pm_close(rlefile);
142     if (pbmfile != stdin)
143         pm_close(pbmfile);
144 
145     return 0;
146 }
147