pdf_g4.c

changeset 67
7add7411c1c2
parent 66
6e0551b59dba
child 73
6e306105c128
     1.1 diff -r 6e0551b59dba -r 7add7411c1c2 pdf_g4.c
     1.2 --- a/pdf_g4.c	Fri Feb 21 09:25:47 2003 +0000
     1.3 +++ b/pdf_g4.c	Fri Feb 21 10:49:11 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.5 2003/02/21 01:25:47 eric Exp $
     1.9 + * $Id: pdf_g4.c,v 1.6 2003/02/21 02:49:11 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 @@ -27,6 +27,7 @@
    1.14  #include <stdbool.h>
    1.15  #include <stdint.h>
    1.16  #include <stdio.h>
    1.17 +#include <stdlib.h>
    1.18  #include <string.h>
    1.19  
    1.20  
    1.21 @@ -37,6 +38,9 @@
    1.22  #include "pdf_private.h"
    1.23  
    1.24  
    1.25 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
    1.26 +
    1.27 +
    1.28  struct pdf_g4_image
    1.29  {
    1.30    double width, height;
    1.31 @@ -94,28 +98,48 @@
    1.32  {
    1.33    struct pdf_g4_image *image = app_data;
    1.34  
    1.35 -#if 0
    1.36 -  pdf_stream_write_data (pdf_file, stream, image->data, image->len);
    1.37 -#else
    1.38 -  unsigned long row = 0;
    1.39 -  word_type *ref;
    1.40 -  word_type *raw;
    1.41 +  uint32_t row;
    1.42 +
    1.43 +  /* reference (previous) row */
    1.44 +  uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t));
    1.45 +  uint32_t ref_run_count;
    1.46  
    1.47 -  ref = NULL;
    1.48 -  raw = image->bitmap->bits;
    1.49 +  /* row being converted */
    1.50 +  uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t));
    1.51 +  uint32_t row_run_count;
    1.52 +
    1.53 +  /* initialize reference row - all white */
    1.54 +  ref_runs [0] = image->Columns;
    1.55 +  ref_run_count = 0;
    1.56  
    1.57 -  while (row < image->Rows)
    1.58 +  for (row = image->bitmap->rect.min.y;
    1.59 +       row < image->bitmap->rect.max.y;
    1.60 +       row++)
    1.61      {
    1.62 -      pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw,
    1.63 -			     image->bitmap->row_words * sizeof (word_type));
    1.64 +      row_run_count = get_row_run_lengths (image->bitmap,
    1.65 +					   row,
    1.66 +					   image->bitmap->rect.min.x,
    1.67 +					   image->bitmap->rect.max.x - 1,
    1.68 +					   image->Columns,  /* max_runs */
    1.69 +					   row_runs);
    1.70 +      pdf_assert (row_run_count > 0);
    1.71 +
    1.72 +      /* $$$ G4 encode the runs here */
    1.73  
    1.74 -      row++;
    1.75 -      ref = raw;
    1.76 -      raw += image->bitmap->row_words;
    1.77 +      /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */
    1.78 +
    1.79 +      SWAP (uint32_t *, row_runs, ref_runs);
    1.80 +      ref_run_count = row_run_count;
    1.81      }
    1.82 +
    1.83 +  
    1.84    /* $$$ generate and write EOFB code */
    1.85 -  /* $$$ flush any remaining buffered bits */
    1.86 -#endif
    1.87 +  pdf_stream_write_bits (pdf_file, stream, 24, 0x001001);
    1.88 +
    1.89 +  pdf_stream_flush_bits (pdf_file, stream);
    1.90 +
    1.91 +  free (ref_runs);
    1.92 +  free (row_runs);
    1.93  }
    1.94  
    1.95  
    1.96 @@ -139,7 +163,7 @@
    1.97  
    1.98    struct pdf_obj *content_stream;
    1.99  
   1.100 -  image = pdf_calloc (sizeof (struct pdf_g4_image));
   1.101 +  image = pdf_calloc (1, sizeof (struct pdf_g4_image));
   1.102  
   1.103    image->width = width;
   1.104    image->height = height;