[trunk] FolderReorgProposal task: rename MJ2/JPIP CLI tools
[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-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
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
55
56 /**
57  * Open JP2 file with the check of JP2 header
58  *
59  * @param[in] filename file name string
60  * @return             file descriptor
61  */
62 FILE * open_jp2file( const char filename[]);
63
64
65 /**
66  * read xml file without any format check for the moment
67  *
68  * @param[in]  filename file name string
69  * @param[out] fsize    file byte size
70  * @return              pointer to the xml file content buffer
71  */
72 char * read_xmlfile( const char filename[], long *fsize);
73
74 int main(int argc, char *argv[])
75 {
76   FILE *fp;
77   char *xmldata, type[]="xml ";
78   long fsize, boxsize;
79
80   if( argc<3){
81     fprintf( stderr, "USAGE: ./addXMLinJP2 modifing.jp2 adding.xml\n");
82     return -1;
83   }
84
85   fp = open_jp2file( argv[1]);
86   if( !fp)
87     return -1;
88   
89   xmldata = read_xmlfile( argv[2], &fsize);
90   boxsize = fsize + 8;
91
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);
98   
99   free( xmldata);
100   fclose(fp);
101   
102   return 0;
103 }
104
105 FILE * open_jp2file( const char filename[])
106 {
107   FILE *fp;
108   char *data;
109   
110   if( !(fp = fopen( filename, "a+b"))){
111     fprintf( stderr, "Original JP2 %s not found\n", filename);
112     return NULL;
113   }
114   /* Check resource is a JP family file. */
115   if( fseek( fp, 0, SEEK_SET)==-1){
116     fclose(fp);
117     fprintf( stderr, "Original JP2 %s broken (fseek error)\n", filename);
118     return NULL;
119   }
120   
121   data = (char *)malloc( 12); /* size of header */
122   if( fread( data, 12, 1, fp) != 1){
123     free( data);
124     fclose(fp);
125     fprintf( stderr, "Original JP2 %s broken (read error)\n", filename);
126     return NULL;
127   }
128     
129   if( *data || *(data + 1) || *(data + 2) ||
130       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
131     free( data);
132     fclose(fp);
133     fprintf( stderr, "No JPEG 2000 Signature box in target %s\n", filename);
134     return NULL;
135   }
136   free( data);
137   return fp;
138 }
139
140 char * read_xmlfile( const char filename[], long *fsize)
141 {
142   FILE *fp;
143   char *data;
144   
145   /*  fprintf( stderr, "open %s\n", filename);*/
146   if(!(fp = fopen( filename, "r"))){
147     fprintf( stderr, "XML file %s not found\n", filename);
148     return NULL;
149   }
150
151   if( fseek( fp, 0, SEEK_END) == -1){
152     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
153     fclose( fp);
154     return NULL;
155   }
156     
157   if( (*fsize = ftell( fp)) == -1){
158     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
159     fclose( fp);
160     return NULL;
161   }
162
163   if( fseek( fp, 0, SEEK_SET) == -1){
164     fprintf( stderr, "XML file %s broken (seek error)\n", filename);
165     fclose( fp);
166     return NULL;
167   }
168
169   data = (char *)malloc( *fsize);
170   
171   if( fread( data, *fsize, 1, fp) != 1){
172     fprintf( stderr, "XML file %s broken (read error)\n", filename);
173     free( data);
174     fclose(fp);
175     return NULL;
176   }
177
178   fclose( fp);
179
180   return data;
181 }