[trunk] Add small utilities to extract JP2 files from PDF
[openjpeg.git] / tests / pdf2jp2.c
1 /*
2  * Copyright (c) 2014, Mathieu Malaterre <mathieu.malaterre@voxxl.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 /*
28  * Extract all JP2 files contained within a PDF file.
29  *
30  * Technically you could simply use mutool, eg:
31  *
32  * $ mutool show -be -o obj58.jp2 Bug691816.pdf 58
33  *
34  * to extract a given JP2 file from within a PDF
35  * However it happens sometimes that the PDF is itself corrupted, this tools is
36  * a lame PDF parser which only extract stream contained in JPXDecode box
37  * only work on linux since I need memmem function
38  */
39
40 #define _GNU_SOURCE
41 #include <string.h>
42 #include <stdio.h>
43 #include <stddef.h>
44 #include <assert.h>
45
46 int main(int argc, char *argv[])
47 {
48 #define NUMJP2 32
49   int i, c = 0;
50   long offets[NUMJP2];
51   char buffer[512];
52 #define BUFLEN 4096
53   int cont = 1;
54   FILE *f;
55   size_t nread;
56   char haystack[BUFLEN];
57   const char needle[] = "JPXDecode";
58
59   const size_t nlen = strlen( needle );
60   const size_t flen = BUFLEN - nlen;
61   char *fpos = haystack + nlen;
62   const char *filename;
63   if( argc < 2 ) return 1;
64
65   filename = argv[1];
66
67   memset( haystack, 0, nlen );
68
69   f = fopen( filename, "rb" );
70   while( cont )
71     {
72     const char *ret;
73     size_t hlen;
74     nread = fread(fpos, 1, flen, f);
75     hlen = nlen + nread;
76     ret = memmem( haystack, hlen, needle, nlen);
77     if( ret )
78       {
79       const long cpos = ftell(f);
80       const ptrdiff_t diff = ret - haystack;
81       assert( diff >= 0 );
82       /*fprintf( stdout, "Found it: %lx\n", (ptrdiff_t)cpos - (ptrdiff_t)hlen + diff);*/
83       offets[c++] = (ptrdiff_t)cpos - (ptrdiff_t)hlen + diff;
84       }
85     cont = (nread == flen);
86     memcpy( haystack, haystack + nread, nlen );
87     }
88
89   assert( feof( f ) );
90
91   for( i = 0; i < c; ++i )
92     {
93     int s, len = 0;
94     char *r;
95     const int ret = fseek(f, offets[i], SEEK_SET);
96     assert( ret == 0 );
97     r = fgets(buffer, sizeof(buffer), f);
98     assert( r );
99     /*fprintf( stderr, "DEBUG: %s", r );*/
100     s = sscanf(r, "JPXDecode]/Length  %d/Width %*d/BitsPerComponent %*d/Height %*d", &len);
101     assert( s == 1 );
102       {
103       FILE *jp2;
104       int j;
105       char jp2fn[512];
106       sprintf( jp2fn, "%s.%d.jp2", filename, i );
107       jp2 = fopen( jp2fn, "wb" );
108       for( j = 0; j < len; ++j )
109         {
110         int v = fgetc(f);
111         int ret2 = fputc(v, jp2);
112         assert( ret2 != EOF );
113         }
114       fclose( jp2 );
115 #if 0
116       /* TODO need to check we reached endstream */
117       r = fgets(buffer, sizeof(buffer), f);
118       fprintf( stderr, "DEBUG: [%s]", r );
119       r = fgets(buffer, sizeof(buffer), f);
120       fprintf( stderr, "DEBUG: [%s]", r );
121 #endif
122       }
123     }
124   fclose(f);
125
126   return 0;
127 }