[trunk] fix name
[openjpeg.git] / applications / jpip / tools / indexer / j2k_to_idxjp2.c
1 /*
2  * $Id$
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 j2k_to_idxjp2 is a program to make jp2 file with index box from j2k file
33  *
34  *  \section impinst Implementing instructions
35  *  This program takes two arguments. \n
36  *   -# Input  J2K image file\n
37  *   -# Output JP2 file name\n
38  *   % ./j2k_to_idxjp2 image.j2k image.jp2\n
39  */
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "openjpeg.h"
45 #include "j2k_to_idxjp2.h"
46
47
48 /* 
49  * Read a binary file
50  *
51  * @param[in]  filename  file name
52  * @param[out] lenoffile pointer to feed file length
53  * @return               byte code
54  */
55 unsigned char * read_binaryfile( char filename[], int *lenoffile);
56
57 int main(int argc, char **argv)
58
59   opj_image_t *image = NULL;
60   opj_codestream_info_t cstr_info;  /* Codestream information structure */
61   unsigned char *j2kstream;
62   int j2klen;
63
64   if (argc < 3){
65     fprintf(stderr,"\nERROR in entry : j2k_to_idxjp2 J2K-file JP2-file\n");
66     return 1;
67   }
68
69   j2kstream = read_binaryfile( argv[1], &j2klen);
70
71   image = decode_j2k( j2kstream, j2klen, &cstr_info);
72   if( !image){
73     free(j2kstream);
74     return -1;
75   }
76   
77   fwrite_idxjp2( argv[2], j2kstream, j2klen, image, cstr_info);
78   
79   free(j2kstream);
80   
81   /* free image data structure */
82   opj_image_destroy(image);
83   
84   return 0;
85 }
86
87 unsigned char * read_binaryfile( char filename[], int *lenoffile)
88 {
89   FILE *fp;
90   unsigned char *bytecode;
91
92   fp = fopen( filename, "rb");
93   if (!fp) {
94     fprintf(stderr, "Failed to open %s for reading !!\n", filename);
95     exit (-1);
96   }
97
98   /* length of the codestream */
99   fseek( fp, 0, SEEK_END);
100   *lenoffile = ftell(fp);
101   fseek( fp, 0, SEEK_SET);
102   
103   bytecode = (unsigned char*)malloc(*lenoffile);
104   fread( bytecode, *lenoffile, 1, fp);
105   fclose(fp);
106
107   return bytecode;
108 }