Skip to content

Commit ad14a6f

Browse files
committed
original import
0 parents  commit ad14a6f

35 files changed

+13043
-0
lines changed

Diff for: Makefile

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
CC = gcc
3+
4+
CCFLAGS = -O1
5+
6+
# -DDEBUG
7+
# -DDICTIONARY
8+
9+
CODEROBJ = main.o encode.o dictionary.o opt_dict_mixed.o \
10+
opt_dict_class.o opt_dict_tree.o equi_class.o mark.o match.o \
11+
symbol_region.o generic_region.o read_write.o decode.o \
12+
entropy.o mq.o mq_tbl.o huff_tbl.o misc.o
13+
14+
15+
doc_coder: $(CODEROBJ)
16+
$(CC) $(CCFLAGS) -o doc_coder $(CODEROBJ) -lm
17+
18+
19+
clean:
20+
/bin/rm -f $(CODEROBJ) doc_coder
21+
22+
23+
# dependencies for header files
24+
main.o: main.c doc_coder.h entropy.h mq.h Makefile
25+
$(CC) $(CCFLAGS) -c main.c
26+
encode.o: encode.c doc_coder.h encode.h dictionary.h entropy.h mq.h Makefile
27+
$(CC) $(CCFLAGS) -c encode.c
28+
dictionary.o: dictionary.c doc_coder.h dictionary.h opt_dict.h entropy.h mq.h Makefile
29+
$(CC) $(CCFLAGS) -c dictionary.c
30+
opt_dict_mixed.o:opt_dict_mixed.c doc_coder.h dictionary.h opt_dict.h Makefile
31+
$(CC) $(CCFLAGS) -c opt_dict_mixed.c
32+
opt_dict_tree.o:opt_dict_tree.c doc_coder.h dictionary.h opt_dict.h Makefile
33+
$(CC) $(CCFLAGS) -c opt_dict_tree.c
34+
opt_dict_class.o:opt_dict_class.c doc_coder.h dictionary.h opt_dict.h Makefile
35+
$(CC) $(CCFLAGS) -c opt_dict_class.c
36+
equi_class.o: equi_class.c doc_coder.h dictionary.h opt_dict.h Makefile
37+
$(CC) $(CCFLAGS) -c equi_class.c
38+
symbol_region.o:symbol_region.c doc_coder.h dictionary.h entropy.h mq.h Makefile
39+
$(CC) $(CCFLAGS) -c symbol_region.c
40+
generic_region.o:generic_region.c doc_coder.h dictionary.h entropy.h mq.h Makefile
41+
$(CC) $(CCFLAGS) -c generic_region.c
42+
mark.o: mark.c doc_coder.h mark.h Makefile
43+
$(CC) $(CCFLAGS) -c mark.c
44+
match.o: match.c doc_coder.h dictionary.h Makefile
45+
$(CC) $(CCFLAGS) -c match.c
46+
entropy.o: entropy.c doc_coder.h entropy.h dictionary.h Makefile
47+
$(CC) $(CCFLAGS) -c entropy.c
48+
mq.o: mq.c doc_coder.h entropy.h mq.h Makefile
49+
$(CC) $(CCFLAGS) -c mq.c
50+
mq_tbl.o: mq_tbl.c Makefile
51+
$(CC) $(CCFLAGS) -c mq_tbl.c
52+
huff_tbl.o: huff_tbl.c entropy.h Makefile
53+
$(CC) $(CCFLAGS) -c huff_tbl.c
54+
decode.o: decode.c doc_coder.h Makefile
55+
$(CC) $(CCFLAGS) -c decode.c
56+
read_write.o: read_write.c dictionary.h doc_coder.h Makefile
57+
$(CC) $(CCFLAGS) -c read_write.c
58+
misc.o: misc.c doc_coder.h Makefile
59+
$(CC) $(CCFLAGS) -c misc.c
60+

Diff for: README

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
JBIG2 encoder by Yan Ye and Pamela Cosman imported from
2+
http://www.code.ucsd.edu/cosman/jb2.zip
3+
4+
It can only convert pbm images. By default the images to be converted have to
5+
be present in ../data, so make sure the directory exists, and the image are
6+
there. Contrary to what -help says, it doesn't take a second argument.
7+
For instance, if you want to convert the test.pbm image present in the test
8+
directory, you can do:
9+
10+
./doc_coder -p ./test test
11+
This will generate a test.jb2 file.
12+

Diff for: cut.c

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/* Copyright 2004 The Regents of the University of California */
2+
/* All Rights Reserved */
3+
4+
/* Permission to copy, modify and distribute any part of this JBIG2 codec for */
5+
/* educational, research and non-profit purposes, without fee, and without a */
6+
/* written agreement is hereby granted, provided that the above copyright */
7+
/* notice, this paragraph and the following three paragraphs appear in all */
8+
/* copies. */
9+
10+
/* Those desiring to incorporate this JBIG2 codec into commercial products */
11+
/* or use for commercial purposes should contact the Technology Transfer */
12+
/* Office, University of California, San Diego, 9500 Gilman Drive, Mail Code */
13+
/* 0910, La Jolla, CA 92093-0910, Ph: (858) 534-5815, FAX: (858) 534-7345, */
14+
/* E-MAIL:[email protected]. */
15+
16+
/* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR */
17+
/* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING */
18+
/* LOST PROFITS, ARISING OUT OF THE USE OF THIS JBIG2 CODEC, EVEN IF THE */
19+
/* UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
20+
21+
/* THE JBIG2 CODEC PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE */
22+
/* UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, */
23+
/* UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF CALIFORNIA MAKES */
24+
/* NO REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER IMPLIED OR */
25+
/* EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
26+
/* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE */
27+
/* JBIG2 CODEC WILL NOT INFRINGE ANY PATENT, TRADEMARK OR OTHER RIGHTS. */
28+
29+
#include <stdio.h>
30+
#include <stdlib.h>
31+
#include <string.h>
32+
33+
#define PATH ""
34+
35+
typedef struct {
36+
int width, height;
37+
unsigned char *content;
38+
} BinImage;
39+
40+
typedef struct {
41+
int x, y;
42+
} PixelCoord;
43+
44+
typedef struct{
45+
PixelCoord ul;
46+
int width, height;
47+
} Rectangle;
48+
49+
void error(char *);
50+
void read_pbm_image(BinImage *, char *);
51+
void cut_rectangle(BinImage *, BinImage *, Rectangle *);
52+
void write_pbm_image(BinImage *, char *);
53+
54+
void main(int argc, char **argv)
55+
{
56+
char ofn[100], nfn[100];
57+
BinImage bim, rim;
58+
Rectangle rect;
59+
60+
if(argc != 2)
61+
error("main: wrong command line architecture");
62+
63+
/* read color image */
64+
strcpy(ofn, argv[1]);
65+
read_pbm_image(&bim, ofn);
66+
67+
printf("input upper-left corner of rectangle:");
68+
scanf("%d %d", &rect.ul.x, &rect.ul.y);
69+
70+
printf("input size of rectangle:");
71+
scanf("%d %d", &rect.width, &rect.height);
72+
73+
if(rect.ul.x + rect.width > bim.width) {
74+
printf("Rectangle specified is out of range horizontally, "
75+
"will shrink its width\n");
76+
rect.width = bim.width-rect.ul.x;
77+
}
78+
79+
if(rect.ul.y + rect.height > bim.height) {
80+
printf("Rectangle specified is out of range vertically, "
81+
"will shrink its height\n");
82+
rect.height = bim.height-rect.ul.y;
83+
}
84+
85+
/* binarize manually */
86+
cut_rectangle(&bim, &rim, &rect);
87+
88+
/* write binarized image */
89+
printf("input PBM file name:");
90+
scanf("%s", nfn);
91+
if(strcmp(nfn+strlen(nfn)-4, ".pbm"))
92+
strcat(nfn, ".pbm");
93+
94+
write_pbm_image(&rim, nfn);
95+
}
96+
97+
void read_pbm_image(BinImage *im, char *fn)
98+
{
99+
FILE *fp;
100+
char line[2000];
101+
register int x, y;
102+
int buffer, bits_to_go;
103+
int bytes_per_line;
104+
unsigned char *ptr;
105+
106+
strcpy(line, PATH);
107+
strcat(line, fn);
108+
fp = fopen(line, "r");
109+
if(!fp) error("read_pbm_image: Cannot open input PBM image file!");
110+
111+
/* PBM file identifier */
112+
fgets(line, 2000, fp);
113+
if(strncmp(line, "P4", strlen("P4")))
114+
error("read_pbm_image: Input file is not in PBM format\n");
115+
116+
/* skip possible comments */
117+
fgets(line, 2000, fp);
118+
while(line[0] == '#' || line[0] == '\n')
119+
fgets(line, 2000, fp);
120+
121+
/* image size */
122+
sscanf(line, "%d %d", &im->width, &im->height);
123+
124+
/* skip possible comments */
125+
fgets(line, 2000, fp);
126+
while(line[0] == '#' || line[0] == '\n')
127+
fgets(line, 2000, fp);
128+
129+
/* read data */
130+
im->content = (unsigned char *)
131+
malloc(sizeof(unsigned char)*im->width*im->height);
132+
if(!im->content) error("read_pbm_image: Cannot allocate memory");
133+
memset(im->content, 0, im->width*im->height);
134+
135+
bytes_per_line = im->width >> 3;
136+
if(im->width % 8) bytes_per_line++;
137+
fseek(fp, -bytes_per_line*im->height, SEEK_END);
138+
139+
bits_to_go = 0; ptr = im->content;
140+
for(y = 0; y < im->height; y++) {
141+
for(x = 0; x < im->width; x++) {
142+
if(!bits_to_go) {
143+
buffer = fgetc(fp);
144+
bits_to_go = 8;
145+
}
146+
if(buffer & 0x80) ptr[x] = 1;
147+
buffer <<= 1; bits_to_go--;
148+
}
149+
bits_to_go = 0; ptr += im->width;
150+
}
151+
}
152+
153+
void cut_rectangle(BinImage *oim, BinImage *nim, Rectangle *rect)
154+
{
155+
register int y;
156+
unsigned char *optr, *nptr;
157+
158+
nim->width = rect->width; nim->height = rect->height;
159+
nim->content = (unsigned char *)
160+
malloc(sizeof(unsigned char)*nim->width*nim->height);
161+
if(!nim->content)
162+
error("cut_rectangle: Cannot allocate memory");
163+
164+
optr = oim->content + oim->width*rect->ul.y+rect->ul.x;
165+
nptr = nim->content;
166+
167+
for(y = 0; y < rect->height; y++) {
168+
memcpy(nptr, optr, rect->width);
169+
optr += oim->width;
170+
nptr += nim->width;
171+
}
172+
}
173+
174+
void write_pbm_image(BinImage *bim, char *fn)
175+
{
176+
FILE *fp;
177+
char line[1000];
178+
register int x, y;
179+
int buffer;
180+
int bits_to_go;
181+
unsigned char *bptr;
182+
183+
strcpy(line, PATH);
184+
strcat(line, fn);
185+
fp = fopen(line, "w");
186+
if(!fp) error("write_pbm_image: Cannot open PBM image");
187+
188+
/* PBM image identifier */
189+
fprintf(fp, "P4\n");
190+
191+
/* image size */
192+
fprintf(fp, "%d %d\n", bim->width, bim->height);
193+
194+
/* image content */
195+
bptr = bim->content;
196+
for(y = 0; y < bim->height; y++) {
197+
bits_to_go = 8; buffer = 0;
198+
for(x = 0; x < bim->width; x++) {
199+
if(bptr[x]) buffer = (buffer << 1) | 1;
200+
else buffer <<= 1;
201+
bits_to_go--;
202+
if(!bits_to_go) {
203+
fputc(buffer, fp);
204+
bits_to_go = 8; buffer = 0;
205+
}
206+
}
207+
if(bits_to_go != 8) {
208+
buffer <<= bits_to_go;
209+
fputc(buffer, fp);
210+
}
211+
bptr += bim->width;
212+
}
213+
214+
fclose(fp);
215+
}
216+
217+
void error(char *msg)
218+
{
219+
printf("%s\n", msg);
220+
exit(-1);
221+
}

0 commit comments

Comments
 (0)