-
Notifications
You must be signed in to change notification settings - Fork 2
/
read_write.c
1318 lines (1122 loc) · 35.6 KB
/
read_write.c
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2004 The Regents of the University of California */
/* All Rights Reserved */
/* Permission to copy, modify and distribute any part of this JBIG2 codec for */
/* educational, research and non-profit purposes, without fee, and without a */
/* written agreement is hereby granted, provided that the above copyright */
/* notice, this paragraph and the following three paragraphs appear in all */
/* copies. */
/* Those desiring to incorporate this JBIG2 codec into commercial products */
/* or use for commercial purposes should contact the Technology Transfer */
/* Office, University of California, San Diego, 9500 Gilman Drive, Mail Code */
/* 0910, La Jolla, CA 92093-0910, Ph: (858) 534-5815, FAX: (858) 534-7345, */
/* E-MAIL:[email protected]. */
/* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR */
/* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING */
/* LOST PROFITS, ARISING OUT OF THE USE OF THIS JBIG2 CODEC, EVEN IF THE */
/* UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
/* THE JBIG2 CODEC PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND THE */
/* UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, */
/* UPDATES, ENHANCEMENTS, OR MODIFICATIONS. THE UNIVERSITY OF CALIFORNIA MAKES */
/* NO REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER IMPLIED OR */
/* EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, OR THAT THE USE OF THE */
/* JBIG2 CODEC WILL NOT INFRINGE ANY PATENT, TRADEMARK OR OTHER RIGHTS. */
#include "doc_coder.h"
#include "dictionary.h"
#include "entropy.h"
#include <math.h>
char JBIG2_ID_STRING[] = {0x97, 0x4a, 0x42, 0x32, 0x0d, 0x0a, 0x1a, 0x0a};
static int out_buffer;
static int bits_written;
void output_bits(int, int);
void write_a_bit(int);
void output_byte(int);
void output_short(int);
void output_int(int);
void start_writing(void), finish_writing(void);
void read_in_doc_buffer(void);
void init_doc_buffer(int);
void set_up_buffer_memory(PixelMap *);
void adjust_doc_buffer(void);
int doc_buffer_blank(void);
void save_doc_buffer(void);
int write_raw_bitmap(char *, int);
int write_coded_bitstream(char *, int);
void write_mark_to_buffer(Mark *, PixelCoord);
void write_pixel_to_buffer(int, int, char);
char read_pixel_from_buffer(int, int);
void write_mark_on_template(Mark *, char *, int, int, PixelCoord);
void write_pgm_image(char *, int, int, char *);
void write_pbm_image(char *, int, int, char *);
void write_dictionary();
void write_all_marks();
void write_regions();
void write_cleanup_image(void);
void write_mark_image(Mark *);
void read_dictionary(void);
void read_all_marks(void);
void read_regions(void);
void read_pbm_file_header(void);
void buffer_coded_bitstream(ARITH_CODED_BITSTREAM *,
CODED_SEGMENT_DATA *);
void write_JBIG2_header(void);
void write_segment_header(SegHeader *);
void write_region_seg_info_field(RegionInfoField *);
void write_gen_reg_seg_header(GenRegionDataHeader *);
void write_gen_ref_reg_seg_header(GenRefRegionDataHeader *);
void write_sym_reg_seg_header(SymRegionDataHeader *);
void write_sym_dict_seg_header(SymDictDataHeader *);
extern Codec *codec;
extern PixelMap *doc_buffer;
extern PixelMap *ori_buffer;
extern PixelMap *cleanup;
extern Dictionary *dictionary;
extern MarkList *all_marks;
extern int cur_coding_strip;
extern CodingStrip *coding_strips;
extern void copy_data_with_margin(char *, int, int, int, int, int, int, char *);
extern void error(char *);
/* Subroutine: int read_in_doc_buffer();
Function: read the image from file to doc_buffer
Input: none
Output: none
*/
void read_in_doc_buffer()
{
register int x, y;
char *data_ptr;
int bits_to_go;
int buffer;
int bytes_per_line;
/* move the file pointer to the correct start of data */
bytes_per_line = codec->doc_width>>3;
if(codec->doc_width % 8) bytes_per_line++;
init_doc_buffer(codec->cur_split == codec->split_num-1);
fseek(codec->fp_in, -bytes_per_line*codec->doc_height, SEEK_END);
fseek(codec->fp_in, bytes_per_line*doc_buffer->top_y, SEEK_CUR);
/* read in pixel values from document image */
data_ptr = doc_buffer->data; bits_to_go = 0;
for(y = 0; y < doc_buffer->height; y++) {
for(x = 0; x < doc_buffer->width; x++) {
if(!bits_to_go) {
buffer = fgetc(codec->fp_in);
bits_to_go = 8;
}
if(buffer & 0x80) data_ptr[x] = 1;
buffer <<= 1; bits_to_go--;
}
bits_to_go = 0;
data_ptr += doc_buffer->width+2;
}
}
void set_doc_buffer_size(int);
/* Subroutine: void init_doc_buffer()
Function: allocate memory for doc_buffer pointer and set up internal data
buffer for it
Input: if this is the last part of one page
Output: none
*/
void init_doc_buffer(int last)
{
set_doc_buffer_size(last);
set_up_buffer_memory(doc_buffer);
}
int find_buffer_height(void);
/* Subroutine: void set_doc_buffer_size()
Function: decide size of doc_buffer
Input: if this's the last stripe
Output: none
*/
void set_doc_buffer_size(last)
{
doc_buffer->width = codec->doc_width;
if(codec->split_num == 1)
doc_buffer->height = codec->doc_height;
else if(!last) doc_buffer->height = find_buffer_height();
else doc_buffer->height = codec->doc_height-doc_buffer->top_y;
}
int black_pixels_in_line(char *, int);
/* Subroutine: int find_buffer_height()
Function: search within a certain range of the nominal buffer height
for a white line. If a white line exists, return its position;
otherwise, return the position of a line which has the least
amount of black. The returned height value is to be used
as the true buffer height
Input: none
Output: buffer height
*/
int find_buffer_height()
{
register int i;
char *line_buffer;
int bytes_per_line;
int range;
int cur, min, posi;
int nom_height; /* nominal height */
bytes_per_line = codec->doc_width>>3;
if(codec->doc_width % 8) bytes_per_line++;
nom_height = codec->doc_height/codec->split_num;
range = nom_height>>4; /* 1/16(6.25%)*doc_buffer->height */
line_buffer = (char *)malloc(sizeof(char)*bytes_per_line);
if(!line_buffer)
error("find_buffer_height: cannot allocate memory\n");
/* search downward fist */
fseek(codec->fp_in, -bytes_per_line*codec->doc_height, SEEK_END);
fseek(codec->fp_in, bytes_per_line*doc_buffer->top_y, SEEK_CUR);
fseek(codec->fp_in, bytes_per_line*nom_height, SEEK_CUR);
min = doc_buffer->width;
for(i = 0; i <= range; i++) {
fread(line_buffer, sizeof(char), bytes_per_line, codec->fp_in);
cur = black_pixels_in_line(line_buffer, bytes_per_line);
if(cur == 0) {
min = cur; posi = i; break;
}
else if(cur < min) {
min = cur; posi = i;
}
}
/* if necessary, search upward now */
if(min > 0) {
fseek(codec->fp_in, -bytes_per_line*codec->doc_height, SEEK_END);
fseek(codec->fp_in, bytes_per_line*doc_buffer->top_y, SEEK_CUR);
fseek(codec->fp_in, bytes_per_line*(nom_height-1), SEEK_CUR);
for(i = -1; i >= -range; i--) {
fseek(codec->fp_in, bytes_per_line*(-2), SEEK_CUR);
fread(line_buffer, sizeof(char), bytes_per_line, codec->fp_in);
cur = black_pixels_in_line(line_buffer, bytes_per_line);
if(cur == 0) {
min = cur; posi = i; break;
}
else if(cur < min) {
min = cur; posi = i;
}
}
}
free((void *)line_buffer);
return nom_height+posi;
}
int black_pixels_in_byte(char);
/* Subroutine: int black_pixels_in_line()
Function: count number of black pixels in the current text line
Input: line pointer and bytes in line
Output: number of black pixels
*/
int black_pixels_in_line(char *line, int bytes_in_line)
{
register int i;
int count;
for(count = 0, i = 0; i < bytes_in_line; i++) {
if(line[i]) count += black_pixels_in_byte(line[i]);
}
return count;
}
/* Subroutine: int black_pixels_in_byte()
Function: count number of black pixels in the input byte
Input: byte value
Output: number of black pixels
*/
int black_pixels_in_byte(char byte)
{
register int i;
int count;
for(i = 0, count = 0; i < 8; i++)
if((byte >> i) & 0x01) count++;
return count;
}
/* Subroutine: void set_up_buffer_memory()
Function: set up memory for encoding buffer, which is in 8 bits/pixel
format, the format that can be handled more easily;
Input: buffer pointer
Output: none;
*/
void set_up_buffer_memory(PixelMap *buffer)
{
char *data_ptr;
int ew, eh;
ew = buffer->width+2; eh = buffer->height+2;
/* need four more lines(two horizontal and two vertical) to store *
* the image, because the encoding buffer needs border */
data_ptr = (char *)malloc(sizeof(char)*ew*eh);
if(!data_ptr) error("set_up_buffer: Cannot allocate memory\n");
memset(data_ptr, 0, ew*eh*sizeof(char));
/* point the buffer to correct starting position */
buffer->data = data_ptr+(buffer->width+2)+1;
}
#ifdef NEVER
int is_white_line(char *, int);
/* Subroutine: void adjust_doc_buffer()
Function: adjust the doc_buffer height just read. Search from bottom up
for a white line, when reached, use this line as the doc_buffer
true height
Input: none
Output: none
*/
void adjust_doc_buffer()
{
register char *line;
register int y;
/* find the first white line from the bottom up */
line = doc_buffer->data+(doc_buffer->width+2)*(doc_buffer->height-1);
for(y = doc_buffer->height-1; y >= 0; y--, line -= (doc_buffer->width+2))
if(is_white_line(line, doc_buffer->width)) break;
if(y != -1) {
/* set the line below the last white white to allow for necessary margin */
if(y+1 < doc_buffer->height)
memset(line+doc_buffer->width+2, 0, doc_buffer->width);
/* adjust the doc_buffer height to that empty line */
doc_buffer->height = y+1;
}
else
printf("adjust_doc_buffer: cannot adjust buffer size because no white line is found\n");
/* error("adjust_doc_buffer: no white line in buffer, don't know how to proceed\n"); */
}
#endif
/* Subroutine: void save_doc_buffer()
Function: save doc_buffer content into ori_buffer
Input: none
Output: none
*/
void save_doc_buffer()
{
register int i;
int width, height;
width = ori_buffer->width = doc_buffer->width;
height = ori_buffer->height = doc_buffer->height;
ori_buffer->data = (char *)malloc(sizeof(char)*width*height);
if(!ori_buffer->data)
error("save_doc_buffer: Cannot allocate memory\n");
for(i = 0; i < height; i++)
memcpy(ori_buffer->data+i*width, doc_buffer->data+i*(width+2),
width*sizeof(char));
}
/* Subroutine: int is_white_line()
Function: decide if the input line of pixels are all white
Input: line pointer and its width
Output: binary decision
*/
int is_white_line(char *line, int width)
{
register int x;
for(x = 0; x < width; x++)
if(line[x]) return FALSE;
return TRUE;
}
/* Subroutine: int doc_buffer_blank()
Function: decide if doc_buffer is entire blank (white)
Input: none
Output: binary decision
*/
int doc_buffer_blank()
{
register int i, j;
register char *ptr;
ptr = doc_buffer->data;
for(i = 0; i < doc_buffer->height; i++, ptr += (doc_buffer->width+2))
for(j = 0; j < doc_buffer->width; j++)
if(ptr[j]) return FALSE;
return TRUE;
}
/* Subroutine: void write_raw_bitmap()
Function: output to the file the raw bitmap rather than arithmetic coded
bitmap when arithmetic coding doesn't compress
Input: bitmap in char format and its size
Output: bits used
*/
int write_raw_bitmap(char *data, int size)
{
register int i, bits_used;
bits_used = 0;
for(i = 0; i < size; i++) {
if(data[i]) write_a_bit(1);
else write_a_bit(0);
bits_used++;
}
return bits_used;
}
/* Subroutine: void write_coded_bitstream()
Function: output to the file the arithmetic coded bitstream
Input: binary bitstream with coded data, and total # bits
Output: bits used
*/
int write_coded_bitstream(char *data, int size)
{
int last_byte, cleanup;
last_byte = size >> 3; cleanup = size%8;
fwrite(data, sizeof(char), last_byte, codec->fp_out);
if(cleanup)
output_bits((int)(data[last_byte]>>(8-cleanup)), cleanup);
return size;
}
/* Subroutine: void start_writing()
Function: initialize variables before start outputting bits
Input: none
Output: none
*/
void start_writing()
{
bits_written = 0;
out_buffer = 0;
}
/* Subroutine: void output_byte()
Function: write a byte
Input: byte value
Output: none
*/
void output_byte(int byte)
{
fputc(byte, codec->fp_out);
}
/* Subroutine: void output_byte()
Function: write two bytes
Input: short value
Output: none
*/
void output_short(int shrt)
{
fputc(((shrt >> 8) & 0xff), codec->fp_out);
fputc((shrt & 0xff), codec->fp_out);
}
/* Subroutine: void output_byte()
Function: write 4 bytes
Input: integer value
Output: none
*/
void output_int(int inte)
{
fputc(((inte >> 24) & 0xff), codec->fp_out);
fputc(((inte >> 16) & 0xff), codec->fp_out);
fputc(((inte >> 8) & 0xff), codec->fp_out);
fputc((inte & 0xff), codec->fp_out);
}
/* Subroutine: void output_bits()
Function: output the designated number of bits from the given value
Input: the value and the number of bits to be output
Output: none
*/
void output_bits(int value, int length)
{
register int i;
register int mask;
value <<= (32-length);
mask = 0x80000000;
for(i = 0; i < length; i++) {
if(value & mask) write_a_bit(1);
else write_a_bit(0);
value <<= 1;
}
}
/* Subroutine: void write_a_bit();
Function: writes an output bit to output buffer, and if buffer is full,
write it to file
Input: the bit to be output
Output: none
*/
void write_a_bit(int bit)
{
out_buffer <<= 1;
if(bit) out_buffer |= 0x01;
bits_written++;
if(bits_written == 8) {
fputc(out_buffer, codec->fp_out);
out_buffer = 0;
bits_written = 0;
}
}
/* Subroutine: void finish_writing()
Function: finish outputting by writing bits left in the out_buffer
that haven't been written yet
Input: none
Output: none
*/
void finish_writing()
{
if(bits_written > 0) {
out_buffer <<= (8-bits_written);
fputc(out_buffer, codec->fp_out);
}
}
/* Subroutine: void write_cleanup_image()
Function; this function writes the current image content in
doc_buffer into an output file
Input: none
Output: none
*/
void write_cleanup_image(void)
{
char fn[200], suffix[100];
printf("Input residual image file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
write_pbm_image(cleanup->data, cleanup->width, cleanup->height, fn);
}
/* Subroutine: void read_dictionary()
Function: read dictionary from an external file(after dict. opt.)
Input: none
Output: none
*/
void read_dictionary()
{
FILE *fp;
register int i;
char suffix[100], fn[100];
DictionaryEntry *cur_entry;
int list_entry, total_mark_num;
float mismatch_thres;
printf("Input dictionary file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "r");
if(!fp) error("read_dictionary: cannot open dictionary file\n");
fread(&mismatch_thres, sizeof(float), 1, fp);
if(mismatch_thres != codec->mismatch_thres)
error("wrong external dictionary, mismatch thres is not correct\n");
fread(&total_mark_num, sizeof(int), 1, fp);
if(!codec->lossy && total_mark_num != dictionary->total_mark_num)
error("wrong external dictionary, number of marks is not correct\n");
dictionary->total_mark_num = total_mark_num;
for(i = 0; i < total_mark_num; i++) {
cur_entry = dictionary->entries + i;
fread(&(cur_entry->index), sizeof(int), 1, fp);
fread(&(cur_entry->ref_index), sizeof(int), 1, fp);
fread(&(cur_entry->singleton), sizeof(int), 1, fp);
if(codec->lossy) cur_entry->perfect = FALSE;
fread(&list_entry, sizeof(int), 1, fp);
cur_entry->mark = all_marks->marks + list_entry;
if(cur_entry->ref_index != -1) {
fread(&list_entry, sizeof(int), 1, fp);
cur_entry->ref_mark = all_marks->marks + list_entry;
}
}
fclose(fp);
}
/* Subroutine: void read_all_marks()
Function: read MarkList from an external file(after dict. opt.)
Input: void
Output: void
*/
void read_all_marks()
{
register int i;
FILE *fp;
Mark *cur_mark;
char suffix[100], fn[1000];
int total_mark_num;
PixelCoord ref;
int width, height;
printf("Input mark file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "r");
if(!fp) error("read_all_marks: cannot open mark file\n");
fread(&total_mark_num, sizeof(int), 1, fp);
if(!codec->lossy && total_mark_num != all_marks->mark_num)
error("read_all_marks: wrong mark file, number of marks doesn't agree\n");
all_marks->mark_num = total_mark_num;
for(i = 0; i < total_mark_num; i++) {
cur_mark = all_marks->marks + i;
fread(&(ref.x), sizeof(int), 1, fp);
fread(&(ref.y), sizeof(int), 1, fp);
fread(&(width), sizeof(int), 1, fp);
fread(&(height), sizeof(int), 1, fp);
if(!codec->lossy) {
if(ref.x != cur_mark->ref.x || ref.y != cur_mark->ref.y ||
width != cur_mark->width || height != cur_mark->height)
error("read_all_marks: wrong mark file\n");
}
else {
if(width != cur_mark->width || height != cur_mark->height) {
free((void *)cur_mark->data);
cur_mark->data = (char *)malloc(sizeof(char)*width*height);
if(!cur_mark->data)
error("read_all_marks: cannot allocate memory\n");
cur_mark->width = width; cur_mark->height = height;
}
}
fread(cur_mark->data, sizeof(char), width*height, fp);
}
fclose(fp);
}
/* Subroutine: void read_regions()
Function: read the coding strips from a file
Input: none
Output: none
*/
void read_regions()
{
register int i, j;
FILE *fp;
char fn[100], suffix[100];
MarkRegInfo *cur_mark;
CodingStrip *cur_strip;
printf("Input region file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "r");
if(!fp) error("read_regions: cannot open region file\n");
fread(&(codec->strip_height), sizeof(int), 1, fp);
fread(&(cur_coding_strip), sizeof(int), 1, fp);
cur_strip = coding_strips;
for(i = 0; i < cur_coding_strip; i++) {
fread(&(cur_strip->strip_x_posi), sizeof(int), 1, fp);
fread(&(cur_strip->strip_top_y), sizeof(int), 1, fp);
fread(&(cur_strip->num_marks), sizeof(int), 1, fp);
cur_mark = cur_strip->marks;
for(j = 0; j < cur_strip->num_marks; j++) {
fread(&(cur_mark->dict_index), sizeof(int), 1, fp);
fread(&(cur_mark->list_entry), sizeof(int), 1, fp);
cur_mark++;
}
cur_strip++;
}
fclose(fp);
}
/* Subroutine: void write_dictionary()
Function: write the complete dictionary information(before splitting it)
for other programs' use
Input: none
Output: none
*/
void write_dictionary()
{
register int i;
FILE *fp;
char fn[1000], suffix[100];
DictionaryEntry *cur_entry;
int list_entry;
printf("Input dictionary file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "w");
if(!fp) error("write_dictionary: cannot open dictionary file\n");
fwrite(&(codec->mismatch_thres), sizeof(float), 1, fp);
fwrite(&(dictionary->total_mark_num), sizeof(int), 1, fp);
for(i = 0; i < dictionary->total_mark_num; i++) {
cur_entry = dictionary->entries + i;
fwrite(&(cur_entry->index), sizeof(int), 1, fp);
fwrite(&(cur_entry->ref_index), sizeof(int), 1, fp);
fwrite(&(cur_entry->singleton), sizeof(int), 1, fp);
list_entry = cur_entry->mark-all_marks->marks;
fwrite(&list_entry, sizeof(int), 1, fp);
if(cur_entry->ref_index != -1) {
list_entry = cur_entry->ref_mark-all_marks->marks;
fwrite(&list_entry, sizeof(int), 1, fp);
}
}
fclose(fp);
}
/* Subroutine: void write_all_marks()
Function: write the complete information of extracted marks
for other programs' use
Input: none
Output: none
*/
void write_all_marks()
{
register int i;
FILE *fp;
char fn[1000], suffix[100];
Mark *cur_mark;
printf("Input mark file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "w");
if(!fp) error("write_all_marks: cannot open mark file\n");
fwrite(&(all_marks->mark_num), sizeof(int), 1, fp);
for(i = 0; i < all_marks->mark_num; i++) {
cur_mark = all_marks->marks + i;
fwrite(&(cur_mark->ref.x), sizeof(int), 1, fp);
fwrite(&(cur_mark->ref.y), sizeof(int), 1, fp);
fwrite(&(cur_mark->width), sizeof(int), 1, fp);
fwrite(&(cur_mark->height), sizeof(int), 1, fp);
fwrite(cur_mark->data, sizeof(char), cur_mark->width*cur_mark->height, fp);
}
fclose(fp);
}
/* Subroutine: void write_regions()
Function: write the coding strips into a file for later use
Input: none
Output: none
*/
void write_regions()
{
register int i, j;
FILE *fp;
char fn[1000], suffix[100];
MarkRegInfo *cur_mark;
CodingStrip *cur_strip;
printf("Input region file suffix:");
scanf("%s", suffix);
sprintf(fn, "%s%s/%s.%s", codec->report.data_path,
codec->report.file_header, codec->report.file_header, suffix);
fp = fopen(fn, "w");
if(!fp) error("write_regions: cannot open mark file\n");
fwrite(&(codec->strip_height), sizeof(int), 1, fp);
fwrite(&(cur_coding_strip), sizeof(int), 1, fp);
cur_strip = coding_strips;
for(i = 0; i < cur_coding_strip; i++) {
fwrite(&(cur_strip->strip_x_posi), sizeof(int), 1, fp);
fwrite(&(cur_strip->strip_top_y), sizeof(int), 1, fp);
fwrite(&(cur_strip->num_marks), sizeof(int), 1, fp);
cur_mark = cur_strip->marks;
for(j = 0; j < cur_strip->num_marks; j++) {
fwrite(&(cur_mark->dict_index), sizeof(int), 1, fp);
fwrite(&(cur_mark->list_entry), sizeof(int), 1, fp);
cur_mark++;
}
cur_strip++;
}
fclose(fp);
}
/* Subroutine: void write_pgm_image()
Function: write the image stored in "data" into file specified by "fn"
in PGM format
Input: image data, its size, and PGM filename
Output: none
*/
void write_pgm_image(char *data, int width, int height, char *fn)
{
FILE *fp;
char buffer[1000];
fp = fopen(fn, "w");
if(!fp)
error("write_pgm_image: cannot open pgm file\n");
sprintf(buffer, "P5\n");
fprintf(fp, buffer);
sprintf(buffer, "%d %d\n", width, height);
fprintf(fp, buffer);
sprintf(buffer, "255\n");
fprintf(fp, buffer);
fwrite(data, sizeof(unsigned char), width*height, fp);
fclose(fp);
}
/* Subroutine: void write_pbm_image()
Function: write the image stored in "data" into file specified by "fn"
in PBM format
Input: image data, its size, and PBM filename
Output: none
*/
void write_pbm_image(char *data, int width, int height, char *fn)
{
FILE *fp;
register int x, y;
int buffer;
int bits_to_go;
char *bptr;
fp = fopen(fn, "w");
if(!fp)
error("write_pbm_image: Cannot open PBM image");
/* PBM image identifier */
fprintf(fp, "P4\n");
/* image size */
fprintf(fp, "%d %d\n", width, height);
/* image content */
bptr = data;
for(y = 0; y < height; y++) {
bits_to_go = 8; buffer = 0;
for(x = 0; x < width; x++) {
if(bptr[x]) buffer = (buffer << 1) | 1;
else buffer <<= 1;
bits_to_go--;
if(!bits_to_go) {
fputc(buffer, fp);
bits_to_go = 8; buffer = 0;
}
}
if(bits_to_go != 8) {
buffer <<= bits_to_go;
fputc(buffer, fp);
}
bptr += width;
}
fclose(fp);
}
/* Subroutine: void write_matlab_file()
Function: write the input histogram into a MATLAB .mat file
Input: histogram and MATLAB file name
Output: none
*/
void write_matlab_file(int *hist, int row, int col, char *filename)
{
FILE *fp;
char dataname[1000];
typedef struct {
long type;
long row;
long col;
long imaginary;
long namelen;
} MatFile;
MatFile mat_file;
strcpy(dataname, "hist");
mat_file.type = 20;
mat_file.row = row;
mat_file.col = col;
mat_file.imaginary = 0;
mat_file.namelen = strlen(dataname)+1;
fp = fopen(filename, "wb");
if(!fp) error("write_matlab_file: cannot open *.mat file\n");
fwrite(&mat_file, sizeof(MatFile), 1, fp);
fwrite(dataname, sizeof(char), mat_file.namelen, fp);
fwrite(hist, sizeof(int), row*col, fp);
fclose(fp);
}
/* Subroutine: void write_mark_to_buffer()
Function: writes the given mark to "doc_buffer"
Input: the mark and its position(upleft corner) in buffer
Output: none
*/
void write_mark_to_buffer(Mark *mark, PixelCoord upleft)
{
register int i, j;
char *dptr;
int width, height;
dptr = mark->data;
width = mark->width; height = mark->height;
for(i = 0; i < height; i++) {
for(j = 0; j < width; j++)
if(dptr[j]) write_pixel_to_buffer(upleft.x+j, upleft.y+i, dptr[j]);
dptr += width;
}
}
/* Subroutine: void write_pixel_to_buffer()
Function: writes a pixel value into "doc_buffer"
Input: the position and the new value
Output: none
*/
void write_pixel_to_buffer(int x, int y, char value)
{
doc_buffer->data[y*(doc_buffer->width+2) + x] = value;
}
/* Subroutine: char read_pixel_from_buffer()
Function: read a pixel from doc_buffer, returns its value
Input: (x, y) coordinates in doc_buffer
Output: the pixel value
*/
char read_pixel_from_buffer(int x, int y)
{
return (char)(doc_buffer->data[y*(doc_buffer->width+2) + x]);
}
/* Subroutine: void write_mark_on_template()
Function: write the bitmap of a mark onto a template buffer at the given
position
Input: input mark, the template buffer and its size, the starting
position
Output: none
*/
void write_mark_on_template(Mark *mark, char *template_buffer, int tw, int th,
PixelCoord posi)
{
register int x, y;
char *tptr, *mptr;
int x1, y1, x2, y2;
int lm, rm, tm, bm;
int mw, mh;
char *ndata;
mw = mark->width; mh = mark->height;
x1 = posi.x; x2 = posi.x+mw;
y1 = posi.y; y2 = posi.y+mh;
if(x1 < 0 || y1 < 0 || x2 > tw || y2 > th) {
printf("write_mark_on_template: part of the mark will be cut\n");
lm = x1 < 0 ? x1:0; rm = x2 < tw ? 0:tw-x2;
tm = y1 < 0 ? y1:0; bm = y2 < th ? 0:th-y2;
ndata = (char *)malloc(sizeof(char)*(mw+lm+rm)*(mh+tm+bm));
if(!ndata) error("write_mark_on_template: Cannot allocate memory\n");
copy_data_with_margin(mark->data, mw, mh, lm, rm, tm, bm, ndata);
x1 = x1 > 0 ? x1:0; y1 = y1 > 0 ? y1:0;
x2 = x2 < tw ? x2:tw; y2 = y2 < th ? y2:th;
mptr = ndata; tptr = template_buffer + y1*tw;
for(y = y1; y < y2; y++) {
for(x = x1; x < x2; x++)
if(mptr[x-x1]) tptr[x] = mptr[x-x1];
mptr += mw+lm+rm; tptr += tw;
}
free((void *)ndata);
}
else {
tptr = template_buffer + y1*tw; mptr = mark->data;
for(y = y1; y < y2; y++) {
for(x = x1; x < x2; x++)
if(mptr[x-x1]) tptr[x] = mptr[x-x1];
tptr += tw; mptr += mw;
}
}
}
/* Subroutine: void write_mark_image()
Function: write the input mark image into "temp.pbm", only for debugging
purpose
Input: the mark
Output: none
*/