Reformat whole codebase with astyle.options (#128)
[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
91     xmldata = read_xmlfile(argv[2], &fsize);
92     if (fsize < 0) {
93         return -1;
94     }
95     boxsize = fsize + 8;
96
97     fputc((boxsize >> 24) & 0xff, fp);
98     fputc((boxsize >> 16) & 0xff, fp);
99     fputc((boxsize >> 8) & 0xff, fp);
100     fputc(boxsize & 0xff, fp);
101     fwrite(type, 4, 1, fp);
102     fwrite(xmldata, (size_t)fsize, 1, fp);
103
104     free(xmldata);
105     fclose(fp);
106
107     return 0;
108 }
109
110 FILE * open_jp2file(const char filename[])
111 {
112     FILE *fp;
113     char *data;
114
115     if (!(fp = fopen(filename, "a+b"))) {
116         fprintf(stderr, "Original JP2 %s not found\n", filename);
117         return NULL;
118     }
119     /* Check resource is a JP family file. */
120     if (fseek(fp, 0, SEEK_SET) == -1) {
121         fclose(fp);
122         fprintf(stderr, "Original JP2 %s broken (fseek error)\n", filename);
123         return NULL;
124     }
125
126     data = (char *)malloc(12);  /* size of header */
127     if (fread(data, 12, 1, fp) != 1) {
128         free(data);
129         fclose(fp);
130         fprintf(stderr, "Original JP2 %s broken (read error)\n", filename);
131         return NULL;
132     }
133
134     if (*data || *(data + 1) || *(data + 2) ||
135             *(data + 3) != 12 || strncmp(data + 4, "jP  \r\n\x87\n", 8)) {
136         free(data);
137         fclose(fp);
138         fprintf(stderr, "No JPEG 2000 Signature box in target %s\n", filename);
139         return NULL;
140     }
141     free(data);
142     return fp;
143 }
144
145 char * read_xmlfile(const char filename[], long *fsize)
146 {
147     FILE *fp;
148     char *data;
149
150     /*  fprintf( stderr, "open %s\n", filename);*/
151     if (!(fp = fopen(filename, "r"))) {
152         fprintf(stderr, "XML file %s not found\n", filename);
153         return NULL;
154     }
155
156     if (fseek(fp, 0, SEEK_END) == -1) {
157         fprintf(stderr, "XML file %s broken (seek error)\n", filename);
158         fclose(fp);
159         return NULL;
160     }
161
162     if ((*fsize = ftell(fp)) == -1) {
163         fprintf(stderr, "XML file %s broken (seek error)\n", filename);
164         fclose(fp);
165         return NULL;
166     }
167     assert(*fsize >= 0);
168
169     if (fseek(fp, 0, SEEK_SET) == -1) {
170         fprintf(stderr, "XML file %s broken (seek error)\n", filename);
171         fclose(fp);
172         return NULL;
173     }
174
175     data = (char *)malloc((size_t) * fsize);
176
177     if (fread(data, (size_t)*fsize, 1, fp) != 1) {
178         fprintf(stderr, "XML file %s broken (read error)\n", filename);
179         free(data);
180         fclose(fp);
181         return NULL;
182     }
183
184     fclose(fp);
185
186     return data;
187 }