2 * Copyright (c) 2014, Mathieu Malaterre <mathieu.malaterre@voxxl.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 * Extract all JP2 files contained within a PDF file.
30 * Technically you could simply use mutool, eg:
32 * $ mutool show -be -o obj58.jp2 Bug691816.pdf 58
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
46 int main(int argc, char *argv[])
56 char haystack[BUFLEN];
57 const char needle[] = "JPXDecode";
59 const size_t nlen = strlen( needle );
60 const size_t flen = BUFLEN - nlen;
61 char *fpos = haystack + nlen;
63 if( argc < 2 ) return 1;
67 memset( haystack, 0, nlen );
69 f = fopen( filename, "rb" );
74 nread = fread(fpos, 1, flen, f);
76 ret = memmem( haystack, hlen, needle, nlen);
79 const long cpos = ftell(f);
80 const ptrdiff_t diff = ret - haystack;
82 /*fprintf( stdout, "Found it: %lx\n", (ptrdiff_t)cpos - (ptrdiff_t)hlen + diff);*/
83 offets[c++] = (ptrdiff_t)cpos - (ptrdiff_t)hlen + diff;
85 cont = (nread == flen);
86 memcpy( haystack, haystack + nread, nlen );
91 for( i = 0; i < c; ++i )
95 const int ret = fseek(f, offets[i], SEEK_SET);
97 r = fgets(buffer, sizeof(buffer), f);
99 /*fprintf( stderr, "DEBUG: %s", r );*/
100 s = sscanf(r, "JPXDecode]/Length %d/Width %*d/BitsPerComponent %*d/Height %*d", &len);
106 sprintf( jp2fn, "%s.%d.jp2", filename, i );
107 jp2 = fopen( jp2fn, "wb" );
108 for( j = 0; j < len; ++j )
111 int ret2 = fputc(v, jp2);
112 assert( ret2 != EOF );
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 );