|
| 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 | + |
| 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