bitblt_g4.c

changeset 94
7664a3f112ba
parent 91
e63762afae80
child 95
851a04fa5324
     1.1 --- a/bitblt_g4.c	Mon Mar 10 09:58:34 2003 +0000
     1.2 +++ b/bitblt_g4.c	Mon Mar 10 13:08:25 2003 +0000
     1.3 @@ -4,7 +4,7 @@
     1.4   *      will be compressed using ITU-T T.6 (G4) fax encoding.
     1.5   *
     1.6   * G4 compression
     1.7 - * $Id: bitblt_g4.c,v 1.9 2003/03/10 01:49:50 eric Exp $
     1.8 + * $Id: bitblt_g4.c,v 1.10 2003/03/10 05:08:25 eric Exp $
     1.9   * Copyright 2003 Eric Smith <eric@brouhaha.com>
    1.10   *
    1.11   * This program is free software; you can redistribute it and/or modify
    1.12 @@ -32,6 +32,7 @@
    1.13  
    1.14  
    1.15  #include "bitblt.h"
    1.16 +#include "pdf_util.h"
    1.17  
    1.18  
    1.19  #include "g4_tables.h"
    1.20 @@ -134,13 +135,24 @@
    1.21  }
    1.22  
    1.23  
    1.24 -static uint32_t find_transition (uint8_t *data,
    1.25 -				 uint32_t pos,
    1.26 -				 uint32_t width)
    1.27 +static inline int g4_get_pixel (uint8_t *buf, uint32_t x)
    1.28  {
    1.29 -  if (! data)
    1.30 -    return (width);
    1.31 -  return (0);  /* $$$ */
    1.32 +  return ((buf [x >> 3] >> (x & 7)) & 1);
    1.33 +}
    1.34 +
    1.35 +
    1.36 +static uint32_t g4_find_pixel (uint8_t *buf,
    1.37 +			       uint32_t pos,
    1.38 +			       uint32_t width,
    1.39 +			       bool color)
    1.40 +{
    1.41 +  while (pos < width)
    1.42 +    {
    1.43 +      if (g4_get_pixel (buf, pos) == color)
    1.44 +	return (pos);
    1.45 +      pos++;
    1.46 +    }
    1.47 +  return (width);
    1.48  }
    1.49  
    1.50  
    1.51 @@ -149,22 +161,19 @@
    1.52  			   uint8_t *ref,
    1.53  			   uint8_t *row)
    1.54  {
    1.55 -  int a0, a1, a2;
    1.56 -  int b1, b2;
    1.57 +  uint32_t a0, a1, a2;
    1.58 +  uint32_t b1, b2;
    1.59 +  bool a0_c;
    1.60  
    1.61 -  a0 = -1;
    1.62 +  a0 = 0;
    1.63 +  a0_c = 0;
    1.64 +
    1.65 +  a1 = g4_find_pixel (row, 0, width, 1);
    1.66 +  b1 = g4_find_pixel (ref, 0, width, 1);
    1.67    
    1.68    while (a0 < width)
    1.69      {
    1.70 -      /* find a1, a2 */
    1.71 -      a1 = find_transition (row, a0, width);
    1.72 -      a2 = find_transition (row, a1, width);
    1.73 -
    1.74 -      /* find b1, b2 */
    1.75 -      b1 = find_transition (ref, a0, width);
    1.76 -      if (0) /* $$$ b1 color = a0 color */
    1.77 -	b1 = find_transition (ref, b1, width);
    1.78 -      b2 = find_transition (ref, b2, width);
    1.79 +      b2 = g4_find_pixel (ref, b1 + 1, width, g4_get_pixel (ref, b1));
    1.80  
    1.81        if (b2 < a1)
    1.82  	{
    1.83 @@ -183,22 +192,41 @@
    1.84        else
    1.85  	{
    1.86  	  /* horizontal mode - 001 */
    1.87 +	  a2 = g4_find_pixel (row, a1, width, a0_c);
    1.88  	  write_bits (buf, 3, 0x1);
    1.89 -	  g4_encode_horizontal_run (buf, 0 /* $$$ color (a0) */, a1 - a0);
    1.90 -	  g4_encode_horizontal_run (buf, 1 /* $$$ color (a1) */, a2 - a1);
    1.91 +	  g4_encode_horizontal_run (buf,   a0_c, a1 - a0);
    1.92 +	  g4_encode_horizontal_run (buf, ! a0_c, a2 - a1);
    1.93  	  a0 = a2;
    1.94  	}
    1.95 +
    1.96 +      if (a0 >= width)
    1.97 +	break;;
    1.98 +
    1.99 +      a0_c = g4_get_pixel (row, a0);
   1.100 +
   1.101 +      a1 = g4_find_pixel (row, a0 + 1, width, ! a0_c);
   1.102 +      b1 = g4_find_pixel (ref, a0 + 1, width, a0_c);
   1.103 +      b1 = g4_find_pixel (ref, b1 + 1, width, ! a0_c);
   1.104      }
   1.105  }
   1.106  
   1.107  
   1.108  void bitblt_write_g4 (Bitmap *bitmap, FILE *f)
   1.109  {
   1.110 +  uint32_t width = (bitmap->rect.max.x - bitmap->rect.min.x) + 1;
   1.111    uint32_t row;
   1.112    struct bit_buffer bb;
   1.113  
   1.114 -  word_type *ref_line = NULL;  /* reference (previous) row */
   1.115 -  word_type *line = bitmap->bits;
   1.116 +  word_type *temp_buffer;
   1.117 +
   1.118 +  word_type *cur_line;
   1.119 +  word_type *ref_line;  /* reference (previous) row */
   1.120 +
   1.121 +  temp_buffer = pdf_calloc ((width + BITS_PER_WORD - 1) / BITS_PER_WORD,
   1.122 +			    sizeof (word_type));
   1.123 +
   1.124 +  cur_line = bitmap->bits;
   1.125 +  ref_line = temp_buffer;
   1.126  
   1.127    memset (& bb, 0, sizeof (bb));
   1.128  
   1.129 @@ -209,11 +237,11 @@
   1.130         row++)
   1.131      {
   1.132        g4_encode_row (& bb,
   1.133 -		     (bitmap->rect.max.x - bitmap->rect.min.x) + 1,
   1.134 +		     width,
   1.135  		     (uint8_t *) ref_line,
   1.136 -		     (uint8_t *) line);
   1.137 -      ref_line = line;
   1.138 -      line += bitmap->row_words;
   1.139 +		     (uint8_t *) cur_line);
   1.140 +      ref_line = cur_line;
   1.141 +      cur_line += bitmap->row_words;
   1.142      }
   1.143  
   1.144    
   1.145 @@ -221,6 +249,8 @@
   1.146    write_bits (& bb, 24, 0x001001);
   1.147  
   1.148    flush_bits (& bb);
   1.149 +
   1.150 +  free (temp_buffer);
   1.151  }
   1.152  
   1.153