2 * $Id: addXMLinJP2.c 46 2011-02-17 14:50:55Z kaori $
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
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
58 * Open JP2 file with the check of JP2 header
60 * @param[in] filename file name string
61 * @return file descriptor
63 FILE * open_jp2file(const char filename[]);
67 * read xml file without any format check for the moment
69 * @param[in] filename file name string
70 * @param[out] fsize file byte size
71 * @return pointer to the xml file content buffer
73 char * read_xmlfile(const char filename[], long *fsize);
75 int main(int argc, char *argv[])
78 char *xmldata, type[] = "xml ";
82 fprintf(stderr, "USAGE: %s modifying.jp2 adding.xml\n", argv[0]);
86 fp = open_jp2file(argv[1]);
91 xmldata = read_xmlfile(argv[2], &fsize);
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);
110 FILE * open_jp2file(const char filename[])
115 if (!(fp = fopen(filename, "a+b"))) {
116 fprintf(stderr, "Original JP2 %s not found\n", filename);
119 /* Check resource is a JP family file. */
120 if (fseek(fp, 0, SEEK_SET) == -1) {
122 fprintf(stderr, "Original JP2 %s broken (fseek error)\n", filename);
126 data = (char *)malloc(12); /* size of header */
127 if (fread(data, 12, 1, fp) != 1) {
130 fprintf(stderr, "Original JP2 %s broken (read error)\n", filename);
134 if (*data || *(data + 1) || *(data + 2) ||
135 *(data + 3) != 12 || strncmp(data + 4, "jP \r\n\x87\n", 8)) {
138 fprintf(stderr, "No JPEG 2000 Signature box in target %s\n", filename);
145 char * read_xmlfile(const char filename[], long *fsize)
150 /* fprintf( stderr, "open %s\n", filename);*/
151 if (!(fp = fopen(filename, "r"))) {
152 fprintf(stderr, "XML file %s not found\n", filename);
156 if (fseek(fp, 0, SEEK_END) == -1) {
157 fprintf(stderr, "XML file %s broken (seek error)\n", filename);
162 if ((*fsize = ftell(fp)) == -1) {
163 fprintf(stderr, "XML file %s broken (seek error)\n", filename);
169 if (fseek(fp, 0, SEEK_SET) == -1) {
170 fprintf(stderr, "XML file %s broken (seek error)\n", filename);
175 data = (char *)malloc((size_t) * fsize);
177 if (fread(data, (size_t)*fsize, 1, fp) != 1) {
178 fprintf(stderr, "XML file %s broken (read error)\n", filename);