first cut at PBM support.

Thu, 10 Apr 2003 08:47:30 +0000

author
eric
date
Thu, 10 Apr 2003 08:47:30 +0000
changeset 157
160d624271cc
parent 156
745483d15215
child 158
e5452e27f518

first cut at PBM support.

Makefile file | annotate | diff | revisions
tumble.c file | annotate | diff | revisions
tumble_input.h file | annotate | diff | revisions
tumble_pbm.c file | annotate | diff | revisions
     1.1 diff -r 745483d15215 -r 160d624271cc Makefile
     1.2 --- a/Makefile	Tue Mar 25 09:38:08 2003 +0000
     1.3 +++ b/Makefile	Thu Apr 10 08:47:30 2003 +0000
     1.4 @@ -1,6 +1,6 @@
     1.5  # tumble: build a PDF file from image files
     1.6  # Makefile
     1.7 -# $Id: Makefile,v 1.39 2003/03/25 01:38:08 eric Exp $
     1.8 +# $Id: Makefile,v 1.40 2003/04/10 00:47:30 eric Exp $
     1.9  # Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    1.10  #
    1.11  # This program is free software; you can redistribute it and/or modify
    1.12 @@ -30,7 +30,7 @@
    1.13  
    1.14  CFLAGS = -Wall
    1.15  LDFLAGS =
    1.16 -LDLIBS = -ltiff -ljpeg -lz -lm
    1.17 +LDLIBS = -ltiff -ljpeg -lpbm -lz -lm
    1.18  
    1.19  ifdef DEBUG
    1.20  CFLAGS := $(CFLAGS) -g
    1.21 @@ -57,14 +57,14 @@
    1.22  # let me know why so I can improve this Makefile.
    1.23  # -----------------------------------------------------------------------------
    1.24  
    1.25 -VERSION = 0.32
    1.26 +VERSION = 0.33
    1.27  
    1.28  PACKAGE = tumble
    1.29  
    1.30  TARGETS = tumble
    1.31  
    1.32  CSRCS = tumble.c semantics.c \
    1.33 -	tumble_input.c tumble_tiff.c tumble_jpeg.c \
    1.34 +	tumble_input.c tumble_tiff.c tumble_jpeg.c tumble_pbm.c \
    1.35  	bitblt.c bitblt_table_gen.c bitblt_g4.c g4_table_gen.c \
    1.36  	pdf.c pdf_util.c pdf_prim.c pdf_name_tree.c \
    1.37  	pdf_bookmark.c pdf_page_label.c \
    1.38 @@ -92,7 +92,7 @@
    1.39  
    1.40  
    1.41  tumble: tumble.o semantics.o \
    1.42 -		tumble_input.o tumble_tiff.o tumble_jpeg.o \
    1.43 +		tumble_input.o tumble_tiff.o tumble_jpeg.o tumble_pbm.o \
    1.44  		scanner.o parser.tab.o \
    1.45  		bitblt.o bitblt_g4.o bitblt_tables.o g4_tables.o \
    1.46  		pdf.o pdf_util.o pdf_prim.o pdf_name_tree.o \
     2.1 diff -r 745483d15215 -r 160d624271cc tumble.c
     2.2 --- a/tumble.c	Tue Mar 25 09:38:08 2003 +0000
     2.3 +++ b/tumble.c	Thu Apr 10 08:47:30 2003 +0000
     2.4 @@ -2,7 +2,7 @@
     2.5   * tumble: build a PDF file from image files
     2.6   *
     2.7   * Main program
     2.8 - * $Id: tumble.c,v 1.41 2003/03/20 08:23:37 eric Exp $
     2.9 + * $Id: tumble.c,v 1.42 2003/04/10 00:47:30 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 @@ -366,6 +366,7 @@
    2.14  
    2.15    init_tiff_handler ();
    2.16    init_jpeg_handler ();
    2.17 +  init_pbm_handler ();
    2.18  
    2.19    while (--argc)
    2.20      {
     3.1 diff -r 745483d15215 -r 160d624271cc tumble_input.h
     3.2 --- a/tumble_input.h	Tue Mar 25 09:38:08 2003 +0000
     3.3 +++ b/tumble_input.h	Thu Apr 10 08:47:30 2003 +0000
     3.4 @@ -1,7 +1,7 @@
     3.5  /*
     3.6   * tumble: build a PDF file from image files
     3.7   *
     3.8 - * $Id: tumble_input.h,v 1.2 2003/03/20 06:55:27 eric Exp $
     3.9 + * $Id: tumble_input.h,v 1.3 2003/04/10 00:47:30 eric Exp $
    3.10   * Copyright 2003 Eric Smith <eric@brouhaha.com>
    3.11   *
    3.12   * This program is free software; you can redistribute it and/or modify
    3.13 @@ -64,3 +64,4 @@
    3.14  
    3.15  void init_tiff_handler (void);
    3.16  void init_jpeg_handler (void);
    3.17 +void init_pbm_handler  (void);
     4.1 diff -r 745483d15215 -r 160d624271cc tumble_pbm.c
     4.2 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 +++ b/tumble_pbm.c	Thu Apr 10 08:47:30 2003 +0000
     4.4 @@ -0,0 +1,230 @@
     4.5 +/*
     4.6 + * tumble: build a PDF file from image files
     4.7 + *
     4.8 + * $Id: tumble_pbm.c,v 1.1 2003/04/10 00:47:30 eric Exp $
     4.9 + * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
    4.10 + *
    4.11 + * This program is free software; you can redistribute it and/or modify
    4.12 + * it under the terms of the GNU General Public License version 2 as
    4.13 + * published by the Free Software Foundation.  Note that permission is
    4.14 + * not granted to redistribute this program under the terms of any
    4.15 + * other version of the General Public License.
    4.16 + *
    4.17 + * This program is distributed in the hope that it will be useful,
    4.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.20 + * GNU General Public License for more details.
    4.21 + *
    4.22 + * You should have received a copy of the GNU General Public License
    4.23 + * along with this program; if not, write to the Free Software
    4.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    4.25 + */
    4.26 +
    4.27 +
    4.28 +#include <stdbool.h>
    4.29 +#include <stdint.h>
    4.30 +#include <stdio.h>
    4.31 +#include <stdlib.h>
    4.32 +#include <strings.h>  /* strcasecmp() is a BSDism */
    4.33 +
    4.34 +#include <pbm.h>
    4.35 +/*
    4.36 + * pbm_readpbmrow_packed always uses big-endian bit ordering.
    4.37 + * On little-endian processors (such as the x86), we want little-endian
    4.38 + * bit order, so we must reverse the bits ourselves after we read in the
    4.39 + * file.
    4.40 + */
    4.41 +#define PBM_REVERSE_BITS
    4.42 +
    4.43 +
    4.44 +#include "semantics.h"
    4.45 +#include "tumble.h"
    4.46 +#include "bitblt.h"
    4.47 +#include "pdf.h"
    4.48 +#include "tumble_input.h"
    4.49 +
    4.50 +
    4.51 +typedef struct
    4.52 +{
    4.53 +  FILE *f;
    4.54 +  int rows;
    4.55 +  int cols;
    4.56 +  int format;
    4.57 +} pbm_info_t;
    4.58 +
    4.59 +static pbm_info_t pbm;
    4.60 +
    4.61 +
    4.62 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
    4.63 +
    4.64 +
    4.65 +static bool match_pbm_suffix (char *suffix)
    4.66 +{
    4.67 +  return (strcasecmp (suffix, ".pbm") == 0);
    4.68 +}
    4.69 +
    4.70 +
    4.71 +static bool close_pbm_input_file (void)
    4.72 +{
    4.73 +  pbm.f = NULL;
    4.74 +  return (1);
    4.75 +}
    4.76 +
    4.77 +
    4.78 +static bool open_pbm_input_file (FILE *f, char *name)
    4.79 +{
    4.80 +  uint8_t buf [2];
    4.81 +  size_t l;
    4.82 +
    4.83 +  l = fread (& buf [0], 1, sizeof (buf), f);
    4.84 +  if (l != sizeof (buf))
    4.85 +    return (0);
    4.86 +
    4.87 +  rewind (f);
    4.88 +
    4.89 +  if (! (((buf [0] == 'P') && (buf [1] == '1')) ||
    4.90 +	 ((buf [0] == 'P') && (buf [1] == '4'))))
    4.91 +    return (0);
    4.92 +
    4.93 +  pbm.f = f;
    4.94 +
    4.95 +  pbm_readpbminit (f, & pbm.cols, & pbm.rows, & pbm.format);
    4.96 +
    4.97 +  return (1);
    4.98 +}
    4.99 +
   4.100 +
   4.101 +static bool last_pbm_input_page (void)
   4.102 +{
   4.103 +  /* only handle single-page PBM files for now */
   4.104 +  return (1);
   4.105 +}
   4.106 +
   4.107 +
   4.108 +static bool get_pbm_image_info (int image,
   4.109 +				input_attributes_t input_attributes,
   4.110 +				image_info_t *image_info)
   4.111 +{
   4.112 +  double x_resolution = 300;
   4.113 +  double y_resolution = 300;
   4.114 +
   4.115 +  /* $$$ need to handle rotation! */
   4.116 +  if (input_attributes.has_resolution)
   4.117 +    {
   4.118 +      x_resolution = input_attributes.x_resolution;
   4.119 +      y_resolution = input_attributes.y_resolution;
   4.120 +    }
   4.121 +
   4.122 +  image_info->width_points = (pbm.cols / x_resolution) * POINTS_PER_INCH;
   4.123 +  image_info->height_points = (pbm.rows / y_resolution) * POINTS_PER_INCH;
   4.124 +
   4.125 +  if ((image_info->height_points > PAGE_MAX_POINTS) || 
   4.126 +      (image_info->width_points > PAGE_MAX_POINTS))
   4.127 +    {
   4.128 +      fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   4.129 +      return (0);
   4.130 +    }
   4.131 +
   4.132 +  return (1);
   4.133 +}
   4.134 +
   4.135 +
   4.136 +static bool process_pbm_image (int image,  /* range 1 .. n */
   4.137 +			       input_attributes_t input_attributes,
   4.138 +			       image_info_t *image_info,
   4.139 +			       pdf_page_handle page)
   4.140 +{
   4.141 +  bool result = 0;
   4.142 +  Rect rect;
   4.143 +  Bitmap *bitmap = NULL;
   4.144 +
   4.145 +  int row;
   4.146 +
   4.147 +  rect.min.x = 0;
   4.148 +  rect.min.y = 0;
   4.149 +
   4.150 +  if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   4.151 +    {
   4.152 +      rect.max.x = image_info->height_samples;
   4.153 +      rect.max.y = image_info->width_samples;
   4.154 +    }
   4.155 +  else
   4.156 +    {
   4.157 +      rect.max.x = image_info->width_samples;
   4.158 +      rect.max.y = image_info->height_samples;
   4.159 +    }
   4.160 +
   4.161 +  bitmap = create_bitmap (& rect);
   4.162 +
   4.163 +  if (! bitmap)
   4.164 +    {
   4.165 +      fprintf (stderr, "can't allocate bitmap\n");
   4.166 +      fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples);
   4.167 +      goto fail;
   4.168 +    }
   4.169 +
   4.170 +  for (row = 0; row < rect.max.y; row++)
   4.171 +    {
   4.172 +      pbm_readpbmrow_packed (pbm.f,
   4.173 +			     (unsigned char *) (bitmap->bits + row * bitmap->row_words),
   4.174 +			     pbm.cols,
   4.175 +			     pbm.format);
   4.176 +      }
   4.177 +
   4.178 +#ifdef PBM_REVERSE_BITS
   4.179 +  reverse_bits ((uint8_t *) bitmap->bits,
   4.180 +		rect.max.y * bitmap->row_words * sizeof (word_t));
   4.181 +#endif /* PBM_REVERSE_BITS */
   4.182 +
   4.183 +  /* $$$ need to invert bits here */
   4.184 +
   4.185 +#if 0
   4.186 +  if (input_attributes.has_page_size)
   4.187 +    bitmap = resize_bitmap (bitmap,
   4.188 +			    x_resolution,
   4.189 +			    y_resolution,
   4.190 +			    input_attributes);
   4.191 +#endif
   4.192 +
   4.193 +#if 0
   4.194 +  rotate_bitmap (bitmap,
   4.195 +		 input_attributes);
   4.196 +#endif
   4.197 +
   4.198 +  pdf_write_g4_fax_image (page,
   4.199 +			  0, 0,  /* x, y */
   4.200 +			  image_info->width_points, image_info->height_points,
   4.201 +			  bitmap,
   4.202 +			  0, /* ImageMask */
   4.203 +			  0, 0, 0,  /* r, g, b */
   4.204 +			  0); /* BlackIs1 */
   4.205 +
   4.206 +  result = 1;
   4.207 +
   4.208 + fail:
   4.209 +  if (bitmap)
   4.210 +    free_bitmap (bitmap);
   4.211 +  return (result);
   4.212 +}
   4.213 +
   4.214 +
   4.215 +input_handler_t pbm_handler =
   4.216 +  {
   4.217 +    match_pbm_suffix,
   4.218 +    open_pbm_input_file,
   4.219 +    close_pbm_input_file,
   4.220 +    last_pbm_input_page,
   4.221 +    get_pbm_image_info,
   4.222 +    process_pbm_image
   4.223 +  };
   4.224 +
   4.225 +
   4.226 +void init_pbm_handler (void)
   4.227 +{
   4.228 +  /* why should we let libpbm look at the real args? */
   4.229 +  int fake_argc = 1;
   4.230 +  char *fake_argv [] = { "tumble" };
   4.231 +
   4.232 +  pbm_init (& fake_argc, fake_argv);
   4.233 +  install_input_handler (& pbm_handler);
   4.234 +}