tumble_jp2.c

changeset 166
301f6f17c364
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tumble_jp2.c	Mon Dec 14 15:51:53 2009 +0000
     1.3 @@ -0,0 +1,275 @@
     1.4 +/*
     1.5 + * tumble: build a PDF file from image files
     1.6 + *
     1.7 + * Copyright 2004 Daniel Gloeckner
     1.8 + *
     1.9 + * Derived from tumble_jpeg.c written 2003 by Eric Smith <eric at brouhaha.com>
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify
    1.12 + * it under the terms of the GNU General Public License version 2 as
    1.13 + * published by the Free Software Foundation.  Note that permission is
    1.14 + * not granted to redistribute this program under the terms of any
    1.15 + * other version of the General Public License.
    1.16 + *
    1.17 + * This program is distributed in the hope that it will be useful,
    1.18 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.20 + * GNU General Public License for more details.
    1.21 + *
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; if not, write to the Free Software
    1.24 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
    1.25 + */
    1.26 +
    1.27 +
    1.28 +#include <stdbool.h>
    1.29 +#include <stdint.h>
    1.30 +#include <stdio.h>
    1.31 +#include <math.h>
    1.32 +#include <strings.h>  /* strcasecmp() is a BSDism */
    1.33 +
    1.34 +
    1.35 +#include "semantics.h"
    1.36 +#include "tumble.h"
    1.37 +#include "bitblt.h"
    1.38 +#include "pdf.h"
    1.39 +#include "tumble_input.h"
    1.40 +
    1.41 +
    1.42 +static bool match_jp2_suffix (char *suffix)
    1.43 +{
    1.44 +  return (strcasecmp (suffix, ".jp2") == 0);
    1.45 +}
    1.46 +
    1.47 +static bool close_jp2_input_file (void)
    1.48 +{
    1.49 +  return (1);
    1.50 +}
    1.51 +
    1.52 +static struct {
    1.53 +  FILE *f;
    1.54 +  uint32_t width,height;
    1.55 +  struct {
    1.56 +    double VR,HR;
    1.57 +  } res[2];
    1.58 +} jp2info;
    1.59 +
    1.60 +struct box {
    1.61 +  char TBox[4];
    1.62 +  bool (*func)(uint64_t size, void *cparam);
    1.63 +  void *cparam;
    1.64 +};
    1.65 +
    1.66 +#define BE32(p) (((p)[0]<<24)+((p)[1]<<16)+((p)[2]<<8)+(p)[3])
    1.67 +#define BE16(p) (((p)[2]<<8)+(p)[3])
    1.68 +
    1.69 +static bool skipbox(uint64_t size, void *cparam)
    1.70 +{
    1.71 +  if(size==~0)
    1.72 +    fseek(jp2info.f,0,SEEK_END);
    1.73 +  else {
    1.74 +    if(cparam)
    1.75 +      size-=*(int *)cparam;
    1.76 +    fseek(jp2info.f,size,SEEK_CUR); /*FIXME: size is 64bit*/
    1.77 +  }
    1.78 +  return 1;
    1.79 +}
    1.80 +
    1.81 +static bool read_res(uint64_t size, void *cparam)
    1.82 +{
    1.83 +  int read=0;
    1.84 +  bool ret=1;
    1.85 +  if(size>=10) {
    1.86 +    int i;
    1.87 +    unsigned char buf[10];
    1.88 +
    1.89 +    ret=0;
    1.90 +    i=(int)cparam;
    1.91 +    read=fread(buf,1,10,jp2info.f);
    1.92 +    if(read==10) {
    1.93 +      uint16_t vrn,vrd,hrn,hrd;
    1.94 +      int8_t vre,hre;
    1.95 +      vrn=BE16(buf);
    1.96 +      vrd=BE16(buf+2);
    1.97 +      hrn=BE16(buf+4);
    1.98 +      hrd=BE16(buf+6);
    1.99 +      vre=((signed char *)buf)[8];
   1.100 +      hre=((signed char *)buf)[9];
   1.101 +      if(vrn && vrd && hrn && hrd) {
   1.102 +	jp2info.res[i].VR=vrn*pow(10.0,vre)/vrd;
   1.103 +	jp2info.res[i].HR=hrn*pow(10.0,hre)/hrd;
   1.104 +	ret=1;
   1.105 +      }
   1.106 +    }
   1.107 +  }
   1.108 +  skipbox(size,&read);
   1.109 +  return ret;
   1.110 +}
   1.111 +
   1.112 +static bool read_ihdr(uint64_t size, void *cparam)
   1.113 +{
   1.114 +  int read=0;
   1.115 +  bool ret=1;
   1.116 +  if(size>=8) {
   1.117 +    unsigned char buf[8];
   1.118 +    read=fread(buf,1,8,jp2info.f);
   1.119 +    if(read==8) {
   1.120 +      jp2info.height=BE32(buf);
   1.121 +      jp2info.width=BE32(buf+4);
   1.122 +    } else
   1.123 +      ret=0;
   1.124 +  }
   1.125 +  skipbox(size,&read);
   1.126 +  return ret;
   1.127 +}
   1.128 +
   1.129 +static bool superbox(uint64_t size, void *cparam)
   1.130 +{ 
   1.131 +  while(size>=8) {
   1.132 +    static unsigned char buf[12];
   1.133 +    int r;
   1.134 +    uint64_t s;
   1.135 +    struct box *l;
   1.136 +
   1.137 +    r=fread(buf+4,1,8,jp2info.f);
   1.138 +    if(r<8)
   1.139 +      return (size==~0 && r==0);
   1.140 + 
   1.141 +    s=BE32(buf+4);
   1.142 +    if(s==1 && size>=16) {
   1.143 +      r=fread(buf,1,8,jp2info.f);
   1.144 +      if(r<8)
   1.145 +        return 0;
   1.146 +      s=((uint64_t)BE32(buf)<<32)+BE32(buf+4);
   1.147 +    }
   1.148 +    if(s && s<8)
   1.149 +      return 0;
   1.150 +    if(size!=~0) {
   1.151 +      if(!s)
   1.152 +        return 0;
   1.153 +      size-=s;
   1.154 +    } else if(!s)
   1.155 +      size=0;
   1.156 +    s=s?s-8:~0;
   1.157 +
   1.158 +    for(l=(struct box *)cparam;l->func;l++)
   1.159 +      if(!memcmp(l->TBox,buf+8,4))
   1.160 +        break;
   1.161 +    if(l->func) {
   1.162 +      if(!l->func(s,l->cparam))
   1.163 +	return 0;
   1.164 +    }else
   1.165 +      if(!skipbox(s,NULL))
   1.166 +	return 0;
   1.167 +  }
   1.168 +  
   1.169 +  return size==0;
   1.170 +}
   1.171 +
   1.172 +static const struct box res_boxes[]={
   1.173 +  {"resc",read_res,(void *)0},
   1.174 +  {"resd",read_res,(void *)1},
   1.175 +  {"",NULL,NULL}
   1.176 +};
   1.177 +
   1.178 +static const struct box jp2h_boxes[]={
   1.179 +  {"ihdr",read_ihdr,NULL},
   1.180 +  {"res ",superbox,(void *)res_boxes},
   1.181 +  {"",NULL,NULL}
   1.182 +};
   1.183 +
   1.184 +static const struct box root[]={
   1.185 +  {"jp2h",superbox,(void *)jp2h_boxes},
   1.186 +  {"",NULL,NULL}
   1.187 +};
   1.188 +
   1.189 +static bool open_jp2_input_file (FILE *f, char *name)
   1.190 +{
   1.191 +  int s;
   1.192 +  const char sig[12]="\0\0\0\xCjP  \r\n\x87\n";
   1.193 +  char buf[12];
   1.194 +
   1.195 +  memset(&jp2info,0,sizeof(jp2info));
   1.196 +  jp2info.f=f;
   1.197 +  
   1.198 +  s=fread(buf,1,12,f);
   1.199 +  rewind(f);
   1.200 +  return (s==12 && !memcmp(buf,sig,12));
   1.201 +}
   1.202 +
   1.203 +
   1.204 +static bool last_jp2_input_page (void)
   1.205 +{
   1.206 +  return 1;
   1.207 +}
   1.208 +
   1.209 +
   1.210 +static bool get_jp2_image_info (int image,
   1.211 +				 input_attributes_t input_attributes,
   1.212 +				 image_info_t *image_info)
   1.213 +{
   1.214 +  int i;
   1.215 +
   1.216 +  if(!superbox(~0,(void *)root))
   1.217 +    return 0;
   1.218 +  rewind(jp2info.f);
   1.219 +  
   1.220 +#ifdef DEBUG_JPEG
   1.221 +  printf ("capture x density: %d pixel/m\n", jp2info.res[0].HR);
   1.222 +  printf ("capture y density: %d pixel/m\n", jp2info.res[0].VR);
   1.223 +  printf ("display x density: %d pixel/m\n", jp2info.res[1].HR);
   1.224 +  printf ("display y density: %d pixel/m\n", jp2info.res[1].VR);
   1.225 +  printf ("width: %d\n", jp2info.width);
   1.226 +  printf ("height: %d\n", jp2info.height);
   1.227 +#endif
   1.228 +
   1.229 +  image_info->color = 1;
   1.230 +  image_info->width_samples = jp2info.width;
   1.231 +  image_info->height_samples = jp2info.height;
   1.232 +
   1.233 +  image_info->width_points = (image_info->width_samples * POINTS_PER_INCH) / 300.0;
   1.234 +  image_info->height_points = (image_info->height_samples * POINTS_PER_INCH) / 300.0;
   1.235 +
   1.236 +  for(i=2;i--;)
   1.237 +    if(jp2info.res[i].VR > 0.0 && jp2info.res[i].HR > 0.0) {
   1.238 +      image_info->width_points = (image_info->width_samples * POINTS_PER_INCH * 10000)/(jp2info.res[i].HR * 254);
   1.239 +      image_info->height_points = (image_info->height_samples * POINTS_PER_INCH * 10000)/(jp2info.res[i].VR * 254);
   1.240 +      break;
   1.241 +    }
   1.242 +
   1.243 +  return 1;
   1.244 +}
   1.245 +
   1.246 +
   1.247 +static bool process_jp2_image (int image,  /* range 1 .. n */
   1.248 +				input_attributes_t input_attributes,
   1.249 +				image_info_t *image_info,
   1.250 +				pdf_page_handle page)
   1.251 +{
   1.252 +  pdf_write_jp2_image (page,
   1.253 +			0, 0,  /* x, y */
   1.254 +			image_info->width_points,
   1.255 +			image_info->height_points,
   1.256 +			image_info->width_samples,
   1.257 +			image_info->height_samples,
   1.258 +			jp2info.f);
   1.259 +
   1.260 +  return (1);
   1.261 +}
   1.262 +
   1.263 +
   1.264 +input_handler_t jp2_handler =
   1.265 +  {
   1.266 +    match_jp2_suffix,
   1.267 +    open_jp2_input_file,
   1.268 +    close_jp2_input_file,
   1.269 +    last_jp2_input_page,
   1.270 +    get_jp2_image_info,
   1.271 +    process_jp2_image
   1.272 +  };
   1.273 +
   1.274 +
   1.275 +void init_jp2_handler (void)
   1.276 +{
   1.277 +  install_input_handler (& jp2_handler);
   1.278 +}