pdf_g4.c

changeset 73
6e306105c128
parent 67
7add7411c1c2
child 78
74b6b230f85d
     1.1 diff -r cddd6226b509 -r 6e306105c128 pdf_g4.c
     1.2 --- a/pdf_g4.c	Sun Feb 23 17:40:41 2003 +0000
     1.3 +++ b/pdf_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: pdf_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $
     1.9 + * $Id: pdf_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