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