From 7377a4de00eaa90cceeffaaf91be706af6c4a829 Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Mon, 10 Mar 2003 13:08:25 +0000 Subject: [PATCH] Working on G4 encoding. --- bitblt.h | 7 +++-- bitblt_g4.c | 84 ++++++++++++++++++++++++++++++++++++----------------- pdf_util.c | 3 +- t2p.c | 13 +++------ tumble.c | 13 +++------ 5 files changed, 71 insertions(+), 49 deletions(-) diff --git a/bitblt.h b/bitblt.h index 04ff6b8..c38a732 100644 --- a/bitblt.h +++ b/bitblt.h @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * bitblt routines - * $Id: bitblt.h,v 1.13 2003/03/10 01:49:50 eric Exp $ + * $Id: bitblt.h,v 1.14 2003/03/10 05:08:25 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -47,9 +47,12 @@ static inline int32_t rect_height (Rect *r) } +/* word_type should be the largest native type that can be handled + efficiently, so it shouldn't be a 64-bit type on a processor that + doesn't have native 64-bit operations. */ typedef uint32_t word_type; #define BITS_PER_WORD (8 * sizeof (word_type)) -#define ALL_ONES (~ 0U) +#define ALL_ONES (~ 0UL) typedef struct Bitmap diff --git a/bitblt_g4.c b/bitblt_g4.c index a5a831e..4247f8d 100644 --- a/bitblt_g4.c +++ b/bitblt_g4.c @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * G4 compression - * $Id: bitblt_g4.c,v 1.9 2003/03/10 01:49:50 eric Exp $ + * $Id: bitblt_g4.c,v 1.10 2003/03/10 05:08:25 eric Exp $ * Copyright 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -32,6 +32,7 @@ #include "bitblt.h" +#include "pdf_util.h" #include "g4_tables.h" @@ -134,13 +135,24 @@ static void g4_encode_horizontal_run (struct bit_buffer *buf, } -static uint32_t find_transition (uint8_t *data, - uint32_t pos, - uint32_t width) +static inline int g4_get_pixel (uint8_t *buf, uint32_t x) { - if (! data) - return (width); - return (0); /* $$$ */ + return ((buf [x >> 3] >> (x & 7)) & 1); +} + + +static uint32_t g4_find_pixel (uint8_t *buf, + uint32_t pos, + uint32_t width, + bool color) +{ + while (pos < width) + { + if (g4_get_pixel (buf, pos) == color) + return (pos); + pos++; + } + return (width); } @@ -149,22 +161,19 @@ static void g4_encode_row (struct bit_buffer *buf, uint8_t *ref, uint8_t *row) { - int a0, a1, a2; - int b1, b2; + uint32_t a0, a1, a2; + uint32_t b1, b2; + bool a0_c; - a0 = -1; + a0 = 0; + a0_c = 0; + + a1 = g4_find_pixel (row, 0, width, 1); + b1 = g4_find_pixel (ref, 0, width, 1); while (a0 < width) { - /* find a1, a2 */ - a1 = find_transition (row, a0, width); - a2 = find_transition (row, a1, width); - - /* find b1, b2 */ - b1 = find_transition (ref, a0, width); - if (0) /* $$$ b1 color = a0 color */ - b1 = find_transition (ref, b1, width); - b2 = find_transition (ref, b2, width); + b2 = g4_find_pixel (ref, b1 + 1, width, g4_get_pixel (ref, b1)); if (b2 < a1) { @@ -183,22 +192,41 @@ static void g4_encode_row (struct bit_buffer *buf, else { /* horizontal mode - 001 */ + a2 = g4_find_pixel (row, a1, width, a0_c); write_bits (buf, 3, 0x1); - g4_encode_horizontal_run (buf, 0 /* $$$ color (a0) */, a1 - a0); - g4_encode_horizontal_run (buf, 1 /* $$$ color (a1) */, a2 - a1); + g4_encode_horizontal_run (buf, a0_c, a1 - a0); + g4_encode_horizontal_run (buf, ! a0_c, a2 - a1); a0 = a2; } + + if (a0 >= width) + break;; + + a0_c = g4_get_pixel (row, a0); + + a1 = g4_find_pixel (row, a0 + 1, width, ! a0_c); + b1 = g4_find_pixel (ref, a0 + 1, width, a0_c); + b1 = g4_find_pixel (ref, b1 + 1, width, ! a0_c); } } void bitblt_write_g4 (Bitmap *bitmap, FILE *f) { + uint32_t width = (bitmap->rect.max.x - bitmap->rect.min.x) + 1; uint32_t row; struct bit_buffer bb; - word_type *ref_line = NULL; /* reference (previous) row */ - word_type *line = bitmap->bits; + word_type *temp_buffer; + + word_type *cur_line; + word_type *ref_line; /* reference (previous) row */ + + temp_buffer = pdf_calloc ((width + BITS_PER_WORD - 1) / BITS_PER_WORD, + sizeof (word_type)); + + cur_line = bitmap->bits; + ref_line = temp_buffer; memset (& bb, 0, sizeof (bb)); @@ -209,11 +237,11 @@ void bitblt_write_g4 (Bitmap *bitmap, FILE *f) row++) { g4_encode_row (& bb, - (bitmap->rect.max.x - bitmap->rect.min.x) + 1, + width, (uint8_t *) ref_line, - (uint8_t *) line); - ref_line = line; - line += bitmap->row_words; + (uint8_t *) cur_line); + ref_line = cur_line; + cur_line += bitmap->row_words; } @@ -221,6 +249,8 @@ void bitblt_write_g4 (Bitmap *bitmap, FILE *f) write_bits (& bb, 24, 0x001001); flush_bits (& bb); + + free (temp_buffer); } diff --git a/pdf_util.c b/pdf_util.c index e0c30c0..b89c0d3 100644 --- a/pdf_util.c +++ b/pdf_util.c @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * PDF routines - * $Id: pdf_util.c,v 1.4 2003/02/21 02:49:11 eric Exp $ + * $Id: pdf_util.c,v 1.5 2003/03/10 05:08:25 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -32,7 +32,6 @@ #include #include "bitblt.h" -#include "pdf.h" #include "pdf_util.h" diff --git a/t2p.c b/t2p.c index 29b4091..4270439 100644 --- a/t2p.c +++ b/t2p.c @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * Main program - * $Id: t2p.c,v 1.27 2003/03/10 01:58:09 eric Exp $ + * $Id: t2p.c,v 1.28 2003/03/10 05:08:25 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -304,7 +304,7 @@ bool process_page (int image, /* range 1 .. n */ uint16_t planar_config; uint16_t resolution_unit; - double x_resolution, y_resolution; + float x_resolution, y_resolution; double dest_x_resolution, dest_y_resolution; double width_points, height_points; /* really 1/72 inch units rather than @@ -439,20 +439,17 @@ bool process_page (int image, /* range 1 .. n */ image_length * bitmap->row_words * sizeof (word_type)); #endif /* TIFF_REVERSE_BITS */ +#if 0 if (input_attributes.has_page_size) bitmap = resize_bitmap (bitmap, x_resolution, y_resolution, input_attributes); +#endif rotate_bitmap (bitmap, input_attributes); -#ifdef TIFF_REVERSE_BITS - reverse_bits ((uint8_t *) bitmap->bits, - image_length * bitmap->row_words * sizeof (word_type)); -#endif /* TIFF_REVERSE_BITS */ - width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH; height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH; @@ -464,7 +461,6 @@ bool process_page (int image, /* range 1 .. n */ page = pdf_new_page (out->pdf, width_points, height_points); -#if 0 pdf_write_g4_fax_image (page, 0, 0, /* x, y */ width_points, height_points, @@ -472,7 +468,6 @@ bool process_page (int image, /* range 1 .. n */ 0, /* ImageMask */ 0, 0, 0, /* r, g, b */ 0); /* BlackIs1 */ -#endif while (bookmarks) { diff --git a/tumble.c b/tumble.c index 2ef6bad..f2a9f90 100644 --- a/tumble.c +++ b/tumble.c @@ -4,7 +4,7 @@ * will be compressed using ITU-T T.6 (G4) fax encoding. * * Main program - * $Id: tumble.c,v 1.27 2003/03/10 01:58:09 eric Exp $ + * $Id: tumble.c,v 1.28 2003/03/10 05:08:25 eric Exp $ * Copyright 2001, 2002, 2003 Eric Smith * * This program is free software; you can redistribute it and/or modify @@ -304,7 +304,7 @@ bool process_page (int image, /* range 1 .. n */ uint16_t planar_config; uint16_t resolution_unit; - double x_resolution, y_resolution; + float x_resolution, y_resolution; double dest_x_resolution, dest_y_resolution; double width_points, height_points; /* really 1/72 inch units rather than @@ -439,20 +439,17 @@ bool process_page (int image, /* range 1 .. n */ image_length * bitmap->row_words * sizeof (word_type)); #endif /* TIFF_REVERSE_BITS */ +#if 0 if (input_attributes.has_page_size) bitmap = resize_bitmap (bitmap, x_resolution, y_resolution, input_attributes); +#endif rotate_bitmap (bitmap, input_attributes); -#ifdef TIFF_REVERSE_BITS - reverse_bits ((uint8_t *) bitmap->bits, - image_length * bitmap->row_words * sizeof (word_type)); -#endif /* TIFF_REVERSE_BITS */ - width_points = (rect_width (& bitmap->rect) / dest_x_resolution) * POINTS_PER_INCH; height_points = (rect_height (& bitmap->rect) / dest_y_resolution) * POINTS_PER_INCH; @@ -464,7 +461,6 @@ bool process_page (int image, /* range 1 .. n */ page = pdf_new_page (out->pdf, width_points, height_points); -#if 0 pdf_write_g4_fax_image (page, 0, 0, /* x, y */ width_points, height_points, @@ -472,7 +468,6 @@ bool process_page (int image, /* range 1 .. n */ 0, /* ImageMask */ 0, 0, 0, /* r, g, b */ 0); /* BlackIs1 */ -#endif while (bookmarks) {