Wed, 05 Mar 2003 01:58:31 +0000
*** empty log message ***
bitblt_g4.c | file | annotate | diff | revisions | |
pdf_g4.c | file | annotate | diff | revisions |
1.1 diff -r cddd6226b509 -r 6e306105c128 bitblt_g4.c 1.2 --- a/bitblt_g4.c Sun Feb 23 17:40:41 2003 +0000 1.3 +++ b/bitblt_g4.c Wed Mar 05 01:58:31 2003 +0000 1.4 @@ -4,7 +4,7 @@ 1.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 1.6 * 1.7 * PDF routines 1.8 - * $Id: bitblt_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $ 1.9 + * $Id: bitblt_g4.c,v 1.7 2003/03/04 17:58:31 eric Exp $ 1.10 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 1.11 * 1.12 * This program is free software; you can redistribute it and/or modify 1.13 @@ -38,6 +38,9 @@ 1.14 #include "pdf_private.h" 1.15 1.16 1.17 +#include "pdf_g4_tables.h" 1.18 + 1.19 + 1.20 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 1.21 1.22 1.23 @@ -92,6 +95,103 @@ 1.24 } 1.25 1.26 1.27 +static void pdf_g4_encode_horizontal_run (pdf_file_handle pdf_file, 1.28 + struct pdf_obj *stream, 1.29 + bool black, 1.30 + uint32_t run_length) 1.31 +{ 1.32 + uint32_t i; 1.33 + 1.34 + while (run_length >= 2560) 1.35 + { 1.36 + pdf_stream_write_bits (pdf_file, stream, 12, 0x01f); 1.37 + run_length -= 2560; 1.38 + } 1.39 + 1.40 + if (run_length >= 1792) 1.41 + { 1.42 + i = (run_length - 1792) >> 6; 1.43 + pdf_stream_write_bits (pdf_file, stream, 1.44 + g4_long_makeup_code [i].count, 1.45 + g4_long_makeup_code [i].bits); 1.46 + run_length -= (1792 + (i << 6)); 1.47 + } 1.48 + else if (run_length >= 64) 1.49 + { 1.50 + i = (run_length >> 6) - 1; 1.51 + pdf_stream_write_bits (pdf_file, stream, 1.52 + g4_makeup_code [black] [i].count, 1.53 + g4_makeup_code [black] [i].bits); 1.54 + run_length -= (i + 1) << 6; 1.55 + } 1.56 + 1.57 + pdf_stream_write_bits (pdf_file, stream, 1.58 + g4_h_code [black] [run_length].count, 1.59 + g4_h_code [black] [run_length].bits); 1.60 +} 1.61 + 1.62 + 1.63 +uint32_t find_transition (uint8_t *data, 1.64 + uint32_t pos, 1.65 + uint32_t width) 1.66 +{ 1.67 + if (! data) 1.68 + return (width); 1.69 + return (0); /* $$$ */ 1.70 +} 1.71 + 1.72 + 1.73 +static void pdf_g4_encode_row (pdf_file_handle pdf_file, 1.74 + struct pdf_obj *stream, 1.75 + uint32_t width, 1.76 + uint8_t *ref, 1.77 + uint8_t *row) 1.78 +{ 1.79 + int a0, a1, a2; 1.80 + int b1, b2; 1.81 + 1.82 + a0 = -1; 1.83 + 1.84 + while (a0 < width) 1.85 + { 1.86 + /* find a1, a2 */ 1.87 + a1 = find_transition (row, a0, width); 1.88 + a2 = find_transition (row, a1, width); 1.89 + 1.90 + /* find b1, b2 */ 1.91 + b1 = find_transition (ref, a0, width); 1.92 + if (0) /* $$$ b1 color = a0 color */ 1.93 + b1 = find_transition (ref, b1, width); 1.94 + b2 = find_transition (ref, b2, width); 1.95 + 1.96 + if (b2 < a1) 1.97 + { 1.98 + /* pass mode - 0001 */ 1.99 + pdf_stream_write_bits (pdf_file, stream, 4, 0x1); 1.100 + a0 = b2; 1.101 + } 1.102 + else if (abs (a1 - b1) <= 3) 1.103 + { 1.104 + /* vertical mode */ 1.105 + pdf_stream_write_bits (pdf_file, stream, 1.106 + g4_vert_code [3 + a1 - b1].count, 1.107 + g4_vert_code [3 + a1 - b1].bits); 1.108 + a0 = a1; 1.109 + } 1.110 + else 1.111 + { 1.112 + /* horizontal mode - 001 */ 1.113 + pdf_stream_write_bits (pdf_file, stream, 3, 0x1); 1.114 + pdf_g4_encode_horizontal_run (pdf_file, stream, 1.115 + 0 /* $$$ color (a0) */, a1 - a0); 1.116 + pdf_g4_encode_horizontal_run (pdf_file, stream, 1.117 + 1 /* $$$ color (a1) */, a2 - a1); 1.118 + a0 = a2; 1.119 + } 1.120 + } 1.121 +} 1.122 + 1.123 + 1.124 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, 1.125 struct pdf_obj *stream, 1.126 void *app_data) 1.127 @@ -100,46 +200,25 @@ 1.128 1.129 uint32_t row; 1.130 1.131 - /* reference (previous) row */ 1.132 - uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 1.133 - uint32_t ref_run_count; 1.134 - 1.135 - /* row being converted */ 1.136 - uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 1.137 - uint32_t row_run_count; 1.138 - 1.139 - /* initialize reference row - all white */ 1.140 - ref_runs [0] = image->Columns; 1.141 - ref_run_count = 0; 1.142 + word_type *ref_line = NULL; /* reference (previous) row */ 1.143 + word_type *line = image->bitmap->bits; 1.144 1.145 for (row = image->bitmap->rect.min.y; 1.146 row < image->bitmap->rect.max.y; 1.147 row++) 1.148 { 1.149 - row_run_count = get_row_run_lengths (image->bitmap, 1.150 - row, 1.151 - image->bitmap->rect.min.x, 1.152 - image->bitmap->rect.max.x - 1, 1.153 - image->Columns, /* max_runs */ 1.154 - row_runs); 1.155 - pdf_assert (row_run_count > 0); 1.156 - 1.157 - /* $$$ G4 encode the runs here */ 1.158 - 1.159 - /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */ 1.160 - 1.161 - SWAP (uint32_t *, row_runs, ref_runs); 1.162 - ref_run_count = row_run_count; 1.163 + pdf_g4_encode_row (pdf_file, stream, image->Columns, 1.164 + (uint8_t *) ref_line, 1.165 + (uint8_t *) line); 1.166 + ref_line = line; 1.167 + line += image->bitmap->row_words; 1.168 } 1.169 1.170 1.171 - /* $$$ generate and write EOFB code */ 1.172 + /* write EOFB code */ 1.173 pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); 1.174 1.175 pdf_stream_flush_bits (pdf_file, stream); 1.176 - 1.177 - free (ref_runs); 1.178 - free (row_runs); 1.179 } 1.180 1.181
2.1 diff -r cddd6226b509 -r 6e306105c128 pdf_g4.c 2.2 --- a/pdf_g4.c Sun Feb 23 17:40:41 2003 +0000 2.3 +++ b/pdf_g4.c Wed Mar 05 01:58:31 2003 +0000 2.4 @@ -4,7 +4,7 @@ 2.5 * will be compressed using ITU-T T.6 (G4) fax encoding. 2.6 * 2.7 * PDF routines 2.8 - * $Id: pdf_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $ 2.9 + * $Id: pdf_g4.c,v 1.7 2003/03/04 17:58:31 eric Exp $ 2.10 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 2.11 * 2.12 * This program is free software; you can redistribute it and/or modify 2.13 @@ -38,6 +38,9 @@ 2.14 #include "pdf_private.h" 2.15 2.16 2.17 +#include "pdf_g4_tables.h" 2.18 + 2.19 + 2.20 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 2.21 2.22 2.23 @@ -92,6 +95,103 @@ 2.24 } 2.25 2.26 2.27 +static void pdf_g4_encode_horizontal_run (pdf_file_handle pdf_file, 2.28 + struct pdf_obj *stream, 2.29 + bool black, 2.30 + uint32_t run_length) 2.31 +{ 2.32 + uint32_t i; 2.33 + 2.34 + while (run_length >= 2560) 2.35 + { 2.36 + pdf_stream_write_bits (pdf_file, stream, 12, 0x01f); 2.37 + run_length -= 2560; 2.38 + } 2.39 + 2.40 + if (run_length >= 1792) 2.41 + { 2.42 + i = (run_length - 1792) >> 6; 2.43 + pdf_stream_write_bits (pdf_file, stream, 2.44 + g4_long_makeup_code [i].count, 2.45 + g4_long_makeup_code [i].bits); 2.46 + run_length -= (1792 + (i << 6)); 2.47 + } 2.48 + else if (run_length >= 64) 2.49 + { 2.50 + i = (run_length >> 6) - 1; 2.51 + pdf_stream_write_bits (pdf_file, stream, 2.52 + g4_makeup_code [black] [i].count, 2.53 + g4_makeup_code [black] [i].bits); 2.54 + run_length -= (i + 1) << 6; 2.55 + } 2.56 + 2.57 + pdf_stream_write_bits (pdf_file, stream, 2.58 + g4_h_code [black] [run_length].count, 2.59 + g4_h_code [black] [run_length].bits); 2.60 +} 2.61 + 2.62 + 2.63 +uint32_t find_transition (uint8_t *data, 2.64 + uint32_t pos, 2.65 + uint32_t width) 2.66 +{ 2.67 + if (! data) 2.68 + return (width); 2.69 + return (0); /* $$$ */ 2.70 +} 2.71 + 2.72 + 2.73 +static void pdf_g4_encode_row (pdf_file_handle pdf_file, 2.74 + struct pdf_obj *stream, 2.75 + uint32_t width, 2.76 + uint8_t *ref, 2.77 + uint8_t *row) 2.78 +{ 2.79 + int a0, a1, a2; 2.80 + int b1, b2; 2.81 + 2.82 + a0 = -1; 2.83 + 2.84 + while (a0 < width) 2.85 + { 2.86 + /* find a1, a2 */ 2.87 + a1 = find_transition (row, a0, width); 2.88 + a2 = find_transition (row, a1, width); 2.89 + 2.90 + /* find b1, b2 */ 2.91 + b1 = find_transition (ref, a0, width); 2.92 + if (0) /* $$$ b1 color = a0 color */ 2.93 + b1 = find_transition (ref, b1, width); 2.94 + b2 = find_transition (ref, b2, width); 2.95 + 2.96 + if (b2 < a1) 2.97 + { 2.98 + /* pass mode - 0001 */ 2.99 + pdf_stream_write_bits (pdf_file, stream, 4, 0x1); 2.100 + a0 = b2; 2.101 + } 2.102 + else if (abs (a1 - b1) <= 3) 2.103 + { 2.104 + /* vertical mode */ 2.105 + pdf_stream_write_bits (pdf_file, stream, 2.106 + g4_vert_code [3 + a1 - b1].count, 2.107 + g4_vert_code [3 + a1 - b1].bits); 2.108 + a0 = a1; 2.109 + } 2.110 + else 2.111 + { 2.112 + /* horizontal mode - 001 */ 2.113 + pdf_stream_write_bits (pdf_file, stream, 3, 0x1); 2.114 + pdf_g4_encode_horizontal_run (pdf_file, stream, 2.115 + 0 /* $$$ color (a0) */, a1 - a0); 2.116 + pdf_g4_encode_horizontal_run (pdf_file, stream, 2.117 + 1 /* $$$ color (a1) */, a2 - a1); 2.118 + a0 = a2; 2.119 + } 2.120 + } 2.121 +} 2.122 + 2.123 + 2.124 void pdf_write_g4_fax_image_callback (pdf_file_handle pdf_file, 2.125 struct pdf_obj *stream, 2.126 void *app_data) 2.127 @@ -100,46 +200,25 @@ 2.128 2.129 uint32_t row; 2.130 2.131 - /* reference (previous) row */ 2.132 - uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 2.133 - uint32_t ref_run_count; 2.134 - 2.135 - /* row being converted */ 2.136 - uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 2.137 - uint32_t row_run_count; 2.138 - 2.139 - /* initialize reference row - all white */ 2.140 - ref_runs [0] = image->Columns; 2.141 - ref_run_count = 0; 2.142 + word_type *ref_line = NULL; /* reference (previous) row */ 2.143 + word_type *line = image->bitmap->bits; 2.144 2.145 for (row = image->bitmap->rect.min.y; 2.146 row < image->bitmap->rect.max.y; 2.147 row++) 2.148 { 2.149 - row_run_count = get_row_run_lengths (image->bitmap, 2.150 - row, 2.151 - image->bitmap->rect.min.x, 2.152 - image->bitmap->rect.max.x - 1, 2.153 - image->Columns, /* max_runs */ 2.154 - row_runs); 2.155 - pdf_assert (row_run_count > 0); 2.156 - 2.157 - /* $$$ G4 encode the runs here */ 2.158 - 2.159 - /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */ 2.160 - 2.161 - SWAP (uint32_t *, row_runs, ref_runs); 2.162 - ref_run_count = row_run_count; 2.163 + pdf_g4_encode_row (pdf_file, stream, image->Columns, 2.164 + (uint8_t *) ref_line, 2.165 + (uint8_t *) line); 2.166 + ref_line = line; 2.167 + line += image->bitmap->row_words; 2.168 } 2.169 2.170 2.171 - /* $$$ generate and write EOFB code */ 2.172 + /* write EOFB code */ 2.173 pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); 2.174 2.175 pdf_stream_flush_bits (pdf_file, stream); 2.176 - 2.177 - free (ref_runs); 2.178 - free (row_runs); 2.179 } 2.180 2.181