tumble_pbm.c

Sun, 21 Dec 2003 23:52:11 +0000

author
eric
date
Sun, 21 Dec 2003 23:52:11 +0000
changeset 164
b9e376e53fdd
parent 162
fe3ef27156c4
permissions
-rw-r--r--

fix svn:keywords property

     1 /*
     2  * tumble: build a PDF file from image files
     3  *
     4  * $Id: tumble_pbm.c,v 1.1 2003/04/10 00:47:30 eric Exp $
     5  * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com>
     6  *
     7  * This program is free software; you can redistribute it and/or modify
     8  * it under the terms of the GNU General Public License version 2 as
     9  * published by the Free Software Foundation.  Note that permission is
    10  * not granted to redistribute this program under the terms of any
    11  * other version of the General Public License.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  *
    18  * You should have received a copy of the GNU General Public License
    19  * along with this program; if not, write to the Free Software
    20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    21  */
    24 #include <stdint.h>
    25 #include <stdio.h>
    26 #include <stdlib.h>
    27 #include <strings.h>  /* strcasecmp() is a BSDism */
    29 #include <pbm.h>
    30 /*
    31  * pbm_readpbmrow_packed always uses big-endian bit ordering.
    32  * On little-endian processors (such as the x86), we want little-endian
    33  * bit order, so we must reverse the bits ourselves after we read in the
    34  * file.
    35  */
    36 #define PBM_REVERSE_BITS
    38 #include <stdbool.h>
    41 #include "semantics.h"
    42 #include "tumble.h"
    43 #include "bitblt.h"
    44 #include "pdf.h"
    45 #include "tumble_input.h"
    48 typedef struct
    49 {
    50   FILE *f;
    51   int rows;
    52   int cols;
    53   int format;
    54 } pbm_info_t;
    56 static pbm_info_t pbm;
    59 #define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0)
    62 static bool match_pbm_suffix (char *suffix)
    63 {
    64   return (strcasecmp (suffix, ".pbm") == 0);
    65 }
    68 static bool close_pbm_input_file (void)
    69 {
    70   pbm.f = NULL;
    71   return (1);
    72 }
    75 static bool open_pbm_input_file (FILE *f, char *name)
    76 {
    77   uint8_t buf [2];
    78   size_t l;
    80   l = fread (& buf [0], 1, sizeof (buf), f);
    81   if (l != sizeof (buf))
    82     return (0);
    84   rewind (f);
    86   if (! (((buf [0] == 'P') && (buf [1] == '1')) ||
    87 	 ((buf [0] == 'P') && (buf [1] == '4'))))
    88     return (0);
    90   pbm.f = f;
    92   pbm_readpbminit (f, & pbm.cols, & pbm.rows, & pbm.format);
    94   return (1);
    95 }
    98 static bool last_pbm_input_page (void)
    99 {
   100   /* only handle single-page PBM files for now */
   101   return (1);
   102 }
   105 static bool get_pbm_image_info (int image,
   106 				input_attributes_t input_attributes,
   107 				image_info_t *image_info)
   108 {
   109   double x_resolution = 300;
   110   double y_resolution = 300;
   112   /* $$$ need to handle rotation! */
   113   if (input_attributes.has_resolution)
   114     {
   115       x_resolution = input_attributes.x_resolution;
   116       y_resolution = input_attributes.y_resolution;
   117     }
   119   image_info->width_points = (pbm.cols / x_resolution) * POINTS_PER_INCH;
   120   image_info->height_points = (pbm.rows / y_resolution) * POINTS_PER_INCH;
   122   if ((image_info->height_points > PAGE_MAX_POINTS) || 
   123       (image_info->width_points > PAGE_MAX_POINTS))
   124     {
   125       fprintf (stdout, "image too large (max %d inches on a side\n", PAGE_MAX_INCHES);
   126       return (0);
   127     }
   129   return (1);
   130 }
   133 static bool process_pbm_image (int image,  /* range 1 .. n */
   134 			       input_attributes_t input_attributes,
   135 			       image_info_t *image_info,
   136 			       pdf_page_handle page)
   137 {
   138   bool result = 0;
   139   Rect rect;
   140   Bitmap *bitmap = NULL;
   142   int row;
   144   rect.min.x = 0;
   145   rect.min.y = 0;
   147   if ((input_attributes.rotation == 90) || (input_attributes.rotation == 270))
   148     {
   149       rect.max.x = image_info->height_samples;
   150       rect.max.y = image_info->width_samples;
   151     }
   152   else
   153     {
   154       rect.max.x = image_info->width_samples;
   155       rect.max.y = image_info->height_samples;
   156     }
   158   bitmap = create_bitmap (& rect);
   160   if (! bitmap)
   161     {
   162       fprintf (stderr, "can't allocate bitmap\n");
   163       fprintf (stderr, "width %d height %d\n", image_info->width_samples, image_info->height_samples);
   164       goto fail;
   165     }
   167   for (row = 0; row < rect.max.y; row++)
   168     {
   169       pbm_readpbmrow_packed (pbm.f,
   170 			     (unsigned char *) (bitmap->bits + row * bitmap->row_words),
   171 			     pbm.cols,
   172 			     pbm.format);
   173       }
   175 #ifdef PBM_REVERSE_BITS
   176   reverse_bits ((uint8_t *) bitmap->bits,
   177 		rect.max.y * bitmap->row_words * sizeof (word_t));
   178 #endif /* PBM_REVERSE_BITS */
   180   /* $$$ need to invert bits here */
   182 #if 0
   183   if (input_attributes.has_page_size)
   184     bitmap = resize_bitmap (bitmap,
   185 			    x_resolution,
   186 			    y_resolution,
   187 			    input_attributes);
   188 #endif
   190 #if 0
   191   rotate_bitmap (bitmap,
   192 		 input_attributes);
   193 #endif
   195   pdf_write_g4_fax_image (page,
   196 			  0, 0,  /* x, y */
   197 			  image_info->width_points, image_info->height_points,
   198 			  bitmap,
   199 			  0, /* ImageMask */
   200 			  0, 0, 0,  /* r, g, b */
   201 			  0); /* BlackIs1 */
   203   result = 1;
   205  fail:
   206   if (bitmap)
   207     free_bitmap (bitmap);
   208   return (result);
   209 }
   212 input_handler_t pbm_handler =
   213   {
   214     match_pbm_suffix,
   215     open_pbm_input_file,
   216     close_pbm_input_file,
   217     last_pbm_input_page,
   218     get_pbm_image_info,
   219     process_pbm_image
   220   };
   223 void init_pbm_handler (void)
   224 {
   225   /* why should we let libpbm look at the real args? */
   226   int fake_argc = 1;
   227   char *fake_argv [] = { "tumble" };
   229   pbm_init (& fake_argc, fake_argv);
   230   install_input_handler (& pbm_handler);
   231 }