2 * $Id: addXMLinJP2.c 46 2011-02-17 14:50:55Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * \brief addXMLinJP2 is a program to embed metadata into JP2 file
34 * \section impinst Implementing instructions
35 * This program takes two arguments. \n
36 * -# Input/output image file in JP2 format, this JP2 file is being modified
37 * -# Input XML file with metadata contents\n
38 * % ./addXMLinJP2 image.jp2 metadata.xml\n
40 * Currently, this program does not parse XML file, and the XML file contents is directly embedded as a XML Box.\n
41 * The following is an example of XML file contents specifying Region Of Interests with target names.\n
43 * <roi name="island" x="1890" y="1950" w="770" h="310"/>\n
44 * <roi name="ship" x="750" y="330" w="100" h="60"/>\n
45 * <roi name="airport" x="650" y="1800" w="650" h="800"/>\n
46 * <roi name="harbor" x="4200" y="1650" w="130" h="130"/>\n
57 * Open JP2 file with the check of JP2 header
59 * @param[in] filename file name string
60 * @return file descriptor
62 FILE * open_jp2file( const char filename[]);
66 * read xml file without any format check for the moment
68 * @param[in] filename file name string
69 * @param[out] fsize file byte size
70 * @return pointer to the xml file content buffer
72 char * read_xmlfile( const char filename[], long *fsize);
74 int main(int argc, char *argv[])
77 char *xmldata, type[]="xml ";
81 fprintf( stderr, "USAGE: ./addXMLinJP2 modifing.jp2 adding.xml\n");
85 fp = open_jp2file( argv[1]);
89 xmldata = read_xmlfile( argv[2], &fsize);
92 fputc( (boxsize>>24)&0xff, fp);
93 fputc( (boxsize>>16)&0xff, fp);
94 fputc( (boxsize>>8)&0xff, fp);
95 fputc( boxsize&0xff, fp);
96 fwrite( type, 4, 1, fp);
97 fwrite( xmldata, fsize, 1, fp);
105 FILE * open_jp2file( const char filename[])
110 if( !(fp = fopen( filename, "a+b"))){
111 fprintf( stderr, "Original JP2 %s not found\n", filename);
114 /* Check resource is a JP family file. */
115 if( fseek( fp, 0, SEEK_SET)==-1){
117 fprintf( stderr, "Original JP2 %s broken (fseek error)\n", filename);
121 data = (char *)malloc( 12); /* size of header */
122 if( fread( data, 12, 1, fp) != 1){
125 fprintf( stderr, "Original JP2 %s broken (read error)\n", filename);
129 if( *data || *(data + 1) || *(data + 2) ||
130 *(data + 3) != 12 || strncmp (data + 4, "jP \r\n\x87\n", 8)){
133 fprintf( stderr, "No JPEG 2000 Signature box in target %s\n", filename);
140 char * read_xmlfile( const char filename[], long *fsize)
145 /* fprintf( stderr, "open %s\n", filename);*/
146 if(!(fp = fopen( filename, "r"))){
147 fprintf( stderr, "XML file %s not found\n", filename);
151 if( fseek( fp, 0, SEEK_END) == -1){
152 fprintf( stderr, "XML file %s broken (seek error)\n", filename);
157 if( (*fsize = ftell( fp)) == -1){
158 fprintf( stderr, "XML file %s broken (seek error)\n", filename);
163 if( fseek( fp, 0, SEEK_SET) == -1){
164 fprintf( stderr, "XML file %s broken (seek error)\n", filename);
169 data = (char *)malloc( *fsize);
171 if( fread( data, *fsize, 1, fp) != 1){
172 fprintf( stderr, "XML file %s broken (read error)\n", filename);