626fc5d1c7c2395c37a2c85c2a595ff4904f6b05
[openjpeg.git] / src / bin / jpip / opj_jpip_addxml.c
1 /*
2  * $Id: addXMLinJP2.c 46 2011-02-17 14:50:55Z kaori $
3  *
4  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2014, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30
31 /*! \file
32  *  \brief addXMLinJP2 is a program to embed metadata into JP2 file
33  *
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
39  *  
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
42  *  <xmlbox>\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
47  *  </xmlbox>
48  */
49
50
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <assert.h>
55
56
57 /**
58  * Open JP2 file with the check of JP2 header
59  *
60  * @param[in] filename file name string
61  * @return             file descriptor
62  */
63 FILE * open_jp2file( const char filename[]);
64
65
66 /**
67  * read xml file without any format check for the moment
68  *
69  * @param[in]  filename file name string
70  * @param[out] fsize    file byte size
71  * @return              pointer to the xml file content buffer
72  */
73 char * read_xmlfile( const char filename[], long *fsize);
74
75 int main(int argc, char *argv[])
76 {
77   FILE *fp;
78   char *xmldata, type[]="xml ";
79   long fsize, boxsize;
80
81   if( argc<3){
82     fprintf( stderr, "USAGE: %s modifing.jp2 adding.xml\n", argv[0] );
83     return -1;
84   }
85
86   fp = open_jp2file( argv[1]);
87   if( !fp)
88     return -1;
89   
90   xmldata = read_xmlfile( argv[2], &fsize);
91   if( fsize < 0 ) return -1;
92   boxsize = fsize + 8;
93
94   fputc( (boxsize>>24)&0xff, fp);
95   fputc( (boxsize>>16)&0xff, fp);
96   fputc( (boxsize>>8)&0xff, fp);
97   fputc( boxsize&0xff, fp);
98   fwrite( type, 4, 1, fp);
99   fwrite( xmldata, (size_t)fsize, 1, fp);
100   
101   free( xmldata);
102   fclose(fp);
103   
104   return 0;
105 }
106
107 FILE * open_jp2file( const char filename[])
108 {
109   FILE *fp;
110   char *data;
111   
112   if( !(fp = fopen( filename, "a+b"))){
113     fprintf( stderr, "Original JP2 %s not found\n", filename);
114     return NULL;
115   }
116   /* Check resource is a JP family file. */
117   if( fseek( fp, 0, SEEK_SET)==-1){
118     fclose(fp);
119     fprintf( stderr, "Original JP2 %s broken (fseek error)\n", filename);
120     return NULL;
121   }
122   
123   data = (char *)malloc( 12); /* size of header */
124   if( fread( data, 12, 1, fp) != 1){
125     free( data);
126     fclose(fp);
127     fprintf( stderr, "Original JP2 %s broken (read error)\n", filename);
128     return NULL;
129   }
130     
131   if( *data || *(data + 1) || *(data + 2) ||
132       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
133     free( data);
134     fclose(fp);
135     fprintf( stderr, "No JPEG 2000 Signature box in target %s\n", filename);
136     return NULL;
137   }
138   free( data);
139   return fp;
140 }
141
142 char * read_xmlfile( const char filename[], long *fsize)
143 {
144   FILE *fp;
145   char *data;
146   
147   /*  fprintf( stderr, "open %s\n", filename);*/
148   if(!(fp = fopen( filename, "r"))){
149     fprintf( stderr, "XML file %s not found\n", filename);
150     return NULL;
151   }
152
153   if( fseek( fp, 0, SEEK_END) == -1){
154     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
155     fclose( fp);
156     return NULL;
157   }
158     
159   if( (*fsize = ftell( fp)) == -1){
160     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
161     fclose( fp);
162     return NULL;
163   }
164   assert( *fsize >= 0 );
165
166   if( fseek( fp, 0, SEEK_SET) == -1){
167     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
168     fclose( fp);
169     return NULL;
170   }
171
172   data = (char *)malloc( (size_t)*fsize);
173   
174   if( fread( data, (size_t)*fsize, 1, fp) != 1){
175     fprintf( stderr, "XML file %s broken (read error)\n", filename);
176     free( data);
177     fclose(fp);
178     return NULL;
179   }
180
181   fclose( fp);
182
183   return data;
184 }