Compare commits

...

171 Commits
UCL ... opj0-97

Author SHA1 Message Date
no_author
fd3d73f678 This commit was manufactured by cvs2svn to create tag 'opj0-97'. 2005-11-09 10:20:28 +00:00
Antonin Descampe
4458ef921c minor change for readability 2005-11-09 10:20:28 +00:00
Antonin Descampe
c9ac67cd05 minor change to avoid warnings about signedness 2005-11-09 10:17:51 +00:00
Giuseppe Baruffa
cff5df73d9 Initial revision 2005-11-08 17:40:33 +00:00
Antonin Descampe
e398154212 small changes to remove warnings related to signedness 2005-11-08 16:15:40 +00:00
Antonin Descampe
0ea26e7ba6 small change to avoid signed/unsigned warning during compilation 2005-11-04 11:10:38 +00:00
Antonin Descampe
8f3bd54c3d Changes proposed by Mathieu Malaterre from the GDCM project... Thanks a lot Mathieu
- '//' replaced by '/* */'
- inclusion of int.h in int.c
- inclusion of j2k.h in int.h in order to export symbols
- adding (void) var when a variable is declared but not used
- some explicit cast
- CLOCKS_PER_SEC is declared as float in bcc55, so there is a need to cast it to int for the modulo operation
- some variables changed from float -> double
2005-11-01 10:15:34 +00:00
Antonin Descampe
18a9bcb882 changed one of the 9-7 coefficients (value closer to the one in the std) 2005-10-27 07:51:11 +00:00
Antonin Descampe
7ee36c3a4c 1) quantization stepsizes stored as float instead of shifted integers -> fixes a pb of precision when using very small stepsizes. 2) bug fixed when decoding until bitplane 0 -> r-value (1/2) added to the coefficient. 2005-10-27 07:49:29 +00:00
Francois-Olivier Devaux
7f8f47566f Modifications to decoding of JP2H box in order to be compatible with JP2 conformance files 2005-10-20 07:43:55 +00:00
Antonin Descampe
d6a523e091 major change in the dwt-module, thanks to Ive (aka Reiner Wahler): thanks a lot ! See note in dwt.c for more details. 2005-09-21 13:00:51 +00:00
Antonin Descampe
ac634b1639 bug fixed when asking for an index with more than (layer*resolutions*100) packets per tile (2nd correction) 2005-09-21 08:11:27 +00:00
Antonin Descampe
a0513798bb bug fixed when asking for an index with more than (layer*resolutions*100) packets per tile 2005-09-20 15:40:13 +00:00
Antonin Descampe
8f4d6bbeba bug fixed when freeing the memory allocated if ppm-marker or ppt-marker is used 2005-09-13 14:17:09 +00:00
Antonin Descampe
cd98a4f740 bug fixed when freeing the memory allocated if ppm-marker or ppt-marker is used 2005-09-13 14:11:41 +00:00
Francois-Olivier Devaux
e25e7e767e Modifications proposed by Rob Komar. Thanks for your contribution ! 2005-08-01 15:10:04 +00:00
Antonin Descampe
de75303c38 Several bugs fixed when dealing with pgx-files with more than 8 bpp and the fixed_quality feature. 2005-07-18 15:21:56 +00:00
Francois-Olivier Devaux
32ba9920db At lines 577, 692 and 699, zeroing of effective region of code-block rather than the 1024*1024 allocated integers of code-block. This optimization has been proposed by Dmitry Kolyadin. Thanks for your help ! 2005-06-02 15:28:37 +00:00
Francois-Olivier Devaux
7ae6a670fa Conversions from int to unsigned int to ensure correct execution of int_min and int_max at lines 800 to 830 2005-06-02 15:25:25 +00:00
Francois-Olivier Devaux
cf4f8be5f0 Conversions from int to unsigned int to ensure correct execution of int_min at line 626 to 628 2005-06-02 15:14:33 +00:00
Antonin Descampe
ec1b970a32 argv[2] replaced by outfile when writing to output file (thanks to O.Smeesters and V.Mesaros) 2005-05-26 15:18:56 +00:00
Antonin Descampe
ee6d0cfba9 CRLF -> LF 2005-05-26 15:13:08 +00:00
Francois-Olivier Devaux
789f6f8e7a CVS test 2005-05-26 14:25:54 +00:00
Sebastien Lugan
3d2a52cd82 correction CRLF -> LF 2005-05-26 14:20:00 +00:00
Antonin Descampe
384068ce8c test 2005-05-26 12:22:00 +00:00
Francois-Olivier Devaux
7e518596e0 cio_read_to_buf(...) and cio_write_from_buf(...) functions syntax modification 2005-05-26 12:07:46 +00:00
Francois-Olivier Devaux
f9eb8f93c2 Code optimization: t1_flags and t1_data set to zero with memset 2005-05-24 12:15:52 +00:00
Francois-Olivier Devaux
d54925f777 2 functions were added, to fasten buffer transfers:
void cio_read_to_buf(unsigned char* buf, int n)
void cio_write_from_buf(unsigned char* buf, int n)
Code written by Glenn Pearson
2005-05-23 15:26:29 +00:00
Francois-Olivier Devaux
eae26f958e 2 functions were added, to fasten buffer transfers:
void cio_read_to_buf(unsigned char* buf, int n)
void cio_write_from_buf(unsigned char* buf, int n)
2005-05-23 15:25:48 +00:00
Francois-Olivier Devaux
ee7c2c28f1 Variables in Movie and Track structure initialized to avoid problems when freeing memory.
These initializations are made in mj2_read_smhd and mj2_read_hmhd.
2005-05-23 09:48:44 +00:00
Francois-Olivier Devaux
8a2e6ecd8b Useless call of mj2_init_stdmovie function deleted 2005-05-23 09:46:37 +00:00
Francois-Olivier Devaux
1b0a585256 Modified mj2_init_stdmovie. Check for division by zero at line 212: tk->sample[j].sample_delta = tk->timescale / tk->sample_rate;
Thanks to Valentin Mesaros !
2005-05-20 07:59:01 +00:00
Francois-Olivier Devaux
29b6a4307a Release configuration modified 2005-04-27 11:01:28 +00:00
Francois-Olivier Devaux
ad4857df94 Light modifications 2005-04-27 10:51:01 +00:00
Francois-Olivier Devaux
a73e37301a First upload of the DTD file 2005-04-27 10:42:31 +00:00
Francois-Olivier Devaux
255e296113 Light modifications 2005-04-27 10:42:16 +00:00
Francois-Olivier Devaux
e4696a445d Update 2005-04-19 10:10:54 +00:00
Francois-Olivier Devaux
81f688433d bug while freeing memory (mj2_free) fixed 2005-04-19 09:18:51 +00:00
Francois-Olivier Devaux
0130af0ff4 Bug while writing huge MOOV boxes fixed. Thanks to Valentin Mesaros ! 2005-04-19 09:17:55 +00:00
Francois-Olivier Devaux
2cbcbb25b0 Standard movie initialisation slightely modified 2005-04-19 09:17:03 +00:00
Francois-Olivier Devaux
5197135a0a Makefile update 2005-04-19 09:16:22 +00:00
Francois-Olivier Devaux
4c67811f65 Project update 2005-04-15 08:02:18 +00:00
Francois-Olivier Devaux
8e8afafae6 Project settings update 2005-04-06 08:53:59 +00:00
Francois-Olivier Devaux
370cfc43a2 Project settings update 2005-04-06 08:44:32 +00:00
Francois-Olivier Devaux
d101633cfa Project settings update 2005-04-06 08:36:32 +00:00
Francois-Olivier Devaux
128bcc4f62 Relative path modifications 2005-04-06 08:21:31 +00:00
Francois-Olivier Devaux
050737bea0 Visual .NET project 2005-04-01 14:17:42 +00:00
Francois-Olivier Devaux
63e17a19eb New version by Glenn Pearson 2005-04-01 14:16:03 +00:00
Francois-Olivier Devaux
61e85ded8b Line 114: Simple warning message when NO_PACKETS_DECODING is not defined 2005-03-25 15:49:29 +00:00
Francois-Olivier Devaux
1cc9fa6815 Line 944:Memory release of img.comps[i].data when NO_PACKETS_DECODING is not defined 2005-03-25 15:48:50 +00:00
Francois-Olivier Devaux
7bdb0331a6 Removed unused parameter in fprintf at line 618 2005-03-25 13:58:00 +00:00
Francois-Olivier Devaux
623f1d96cf Unused "dest" pointer removed from main() 2005-03-25 13:53:54 +00:00
Francois-Olivier Devaux
1de9535b8c #endif;
replaced by
#endif
2005-03-25 13:52:55 +00:00
Francois-Olivier Devaux
68ebc572fc Makefile for frames_to_mj2, mj2_to_frames, wrap_j2k_in_mj2
and extract_j2k_from_mj2
Thanks to Valentin Mesaros from Octalis !
2005-03-25 13:47:47 +00:00
Francois-Olivier Devaux
5f44ec7d01 Newline at end of file added 2005-03-25 13:46:38 +00:00
Francois-Olivier Devaux
10aee43cbc Replaced bad message error when unknown box is found 2005-03-25 13:44:54 +00:00
Francois-Olivier Devaux
7e2e09e2a9 Replaced unlink() with remove() 2005-03-25 13:44:16 +00:00
Francois-Olivier Devaux
86ce4d4c18 New OpenJPEG MJ2 module: mj2_to_metadata created by Glenn Pearson. The OpenJPEG team would like to thank him for his contributions ! 2005-03-25 09:44:32 +00:00
Francois-Olivier Devaux
361edd25b7 Newline at end of file 2005-03-25 09:30:30 +00:00
Francois-Olivier Devaux
85691a02a6 Initialisation of movie->cl changed in mj2_init_stdmovie 2005-03-21 15:14:16 +00:00
Francois-Olivier Devaux
1f96f0e92a Modifications made to enable the decoding of a J2K file headers without decoding all packets using #NO_PACKETS_DECODING
Used with MJ2_to_metadata
2005-03-16 12:11:40 +00:00
Francois-Olivier Devaux
34a6c13a2d Input file format bug fixed (2) 2005-02-10 16:22:01 +00:00
Francois-Olivier Devaux
542f06007c Input file format bug fixed 2005-02-10 16:16:18 +00:00
Francois-Olivier Devaux
fdbca0fc9d Minor bugs in error messages fixed 2005-02-09 10:26:22 +00:00
Francois-Olivier Devaux
632902d7e3 Added tk->visual_w and tk->visual_h variables, specifying the track's visual presentation size as fixed-point 16.16 values. 2005-02-09 10:08:48 +00:00
Francois-Olivier Devaux
9310f1b4a9 Precision added to tk->balance, tk->volume, movie->volume and movie->rate 2005-02-09 09:56:32 +00:00
Francois-Olivier Devaux
ed0a46eff5 YUV_DFMT added 2005-02-09 09:55:13 +00:00
Francois-Olivier Devaux
8323900f73 Bug with number of layers to decode fixed 2005-02-01 16:30:06 +00:00
Antonin Descampe
c83ae7397d Added layer option '-l' to the command line. This option allows user to
specify a maximum number of quality layers to be decoded.
2005-01-26 09:59:31 +00:00
Antonin Descampe
3b1bee0eec * function getopt added to the decoder (to efficiently manage arguments
in command line)
* reduce_on, reduce_value grouped in a single variable reduce
* up-to-date usage display
* image_type renamed --> decod_format
* JPEG2000_format renamed -->  cod_format
2005-01-26 08:59:49 +00:00
Francois-Olivier Devaux
ba1cf545df Bug with mj2_read_struct fixed (problem when reading MJ2 files starting with the MOOV box fixed) 2005-01-14 08:14:01 +00:00
Antonin Descampe
bfb2f9f58b Luke Lee optimization for fix_mul... Thanks to him 2005-01-03 17:39:30 +00:00
Francois-Olivier Devaux
13b8ecfe44 This tool wraps J2K codestreams into a MJ2 file (designed to
work with other codecs than OpenJPEG)
2004-12-08 12:12:23 +00:00
Francois-Olivier Devaux
9e9e188ebf This tool extracts J2K codestreams from a MJ2 file (designed to
work with other codecs than OpenJPEG)
2004-12-08 12:12:00 +00:00
Francois-Olivier Devaux
5034581e69 Converting to Dos 2004-12-08 12:09:25 +00:00
Francois-Olivier Devaux
85ec5eccf7 Converting to Unix 2004-12-08 12:07:25 +00:00
Francois-Olivier Devaux
9c8fcc3616 This tool wraps J2K codestreams into a MJ2 file (designed to
work with other codecs than OpenJPEG)
2004-12-08 11:24:10 +00:00
Francois-Olivier Devaux
7976b61c89 This tool extracts J2K codestreams from a MJ2 file (designed to
work with other codecs than OpenJPEG)
2004-12-08 11:23:37 +00:00
Francois-Olivier Devaux
43a6b92345 New modular version of the MJ2 library.
The goal of this new version is to make the library more independent from OpenJPEG
2004-12-08 11:22:18 +00:00
Francois-Olivier Devaux
f25acae0bc Modifications in handling of JP2 files to increase modularity of JP2.c
Memory leak fixed with jp2 files
2004-12-08 09:08:13 +00:00
Francois-Olivier Devaux
9225f53d62 Modularity increase in jp2_init_stdjp2 function and jp2c box functions to enable new version of MJ2 module 2004-12-08 09:06:04 +00:00
Antonin Descampe
eb1b017ec7 Index structure adpated for other progression orders than LRCP
(thanks to Stefano Pensa)
2004-11-29 14:48:20 +00:00
Francois-Olivier Devaux
0ddef9b2db Modifications to increase modularity of jp2 coding/decoding 2004-11-26 16:33:18 +00:00
Francois-Olivier Devaux
1bdb07cefe fprintf correctly redirected to stderr or stdout 2004-11-15 12:41:35 +00:00
Francois-Olivier Devaux
0a879e2207 Help message updated 2004-11-05 10:08:06 +00:00
Francois-Olivier Devaux
f3c3eab744 Usage message updated 2004-11-05 10:07:02 +00:00
Francois-Olivier Devaux
08209f1d23 Help message updated 2004-11-05 09:58:42 +00:00
Francois-Olivier Devaux
3ec116ae08 Help message updated 2004-11-05 09:51:30 +00:00
Francois-Olivier Devaux
3b600a986c Stack corruption error corrected with "tmp" variable 2004-11-05 08:59:58 +00:00
Francois-Olivier Devaux
0d4a38f3a5 Bug with -F argument fixed 2004-10-08 07:08:25 +00:00
Francois-Olivier Devaux
2410db2ae7 Memory leak bug fixed in the read_mdat() function 2004-10-06 08:58:27 +00:00
Francois-Olivier Devaux
e1278bc691 Small bug corrected: unsigned int to int conversion at line 417 2004-10-06 07:12:36 +00:00
Francois-Olivier Devaux
899439a906 Bug with tk->imagefile pointer fixed 2004-09-09 13:34:20 +00:00
Francois-Olivier Devaux
02e3a4ec77 Modification of stsc_decompact function 2004-09-08 15:12:21 +00:00
Francois-Olivier Devaux
7250ce0668 Bug with sound and hint tracks fixed 2004-09-06 15:53:36 +00:00
Francois-Olivier Devaux
2ebe5f2c92 One component input image handled 2004-09-06 15:47:09 +00:00
Francois-Olivier Devaux
3c730a804b Order of data written to jp2_struct->cl inversed in jp2_read_ftyp function 2004-08-31 09:17:06 +00:00
Francois-Olivier Devaux
313975debf Pointer values initialized to zero 2004-08-27 14:22:33 +00:00
Francois-Olivier Devaux
0abe8e1997 Fixed bugs. Thanks to Dereck ! 2004-08-24 13:36:45 +00:00
Antonin Descampe
bc5c5115cb * Inserted tool for memory leaks fixing in debug mode
* Fixed some memory leaks in the decoder (some are still present when multiple tiles!)
2004-08-06 14:13:51 +00:00
Antonin Descampe
61ac18531a Help updated 2004-08-04 10:06:50 +00:00
Francois-Olivier Devaux
0ee21bdbfe Bug fixed in lossless mode 2004-08-03 14:20:04 +00:00
Antonin Descampe
d116ca5d10 Better indentation 2004-08-03 14:14:44 +00:00
Antonin Descampe
26a630dad2 Bug fixed in lossless mode 2004-08-03 14:06:10 +00:00
Francois-Olivier Devaux
2c2730280e Deleted system("pause") at the end of main function 2004-08-02 15:23:22 +00:00
Antonin Descampe
2e2ba14c53 Bug fixed in pgx-file reading 2004-07-30 09:22:22 +00:00
Antonin Descampe
9fa68ca811 Bug fixed in MQ decoder initialization (keyword comment : initbug1) 2004-07-21 05:18:57 +00:00
Antonin Descampe
d38080aa00 * Bug fixed in jpt decoding
* Bug fixed in pcrl progression order (profile-1 compliance)
* Change in tcd_dump
2004-07-20 05:40:55 +00:00
Francois-Olivier Devaux
aa6c1d28ad Error message modification 2004-07-16 15:16:07 +00:00
Antonin Descampe
ba4fd784f5 Converting to DOS-formats 2004-07-16 14:52:25 +00:00
Francois-Olivier Devaux
3d67d80d49 First upload of MJ2 module 2004-07-16 14:10:49 +00:00
Francois-Olivier Devaux
25e97927f9 First upload of mj2.h 2004-07-16 14:09:50 +00:00
Francois-Olivier Devaux
e13205349d Initial revision 2004-07-16 14:06:32 +00:00
Francois-Olivier Devaux
cc42bfbaaa Comments added 2004-07-16 13:39:09 +00:00
Francois-Olivier Devaux
c785c1b82b minor changes 2004-07-16 13:17:23 +00:00
Francois-Olivier Devaux
5da1d86a03 Bug on jp2_struct->comps memory allocation fixed 2004-07-16 13:16:39 +00:00
Antonin Descampe
54b04573f9 USAGE-statement updated 2004-07-16 10:39:06 +00:00
Francois-Olivier Devaux
ba0042bfe2 Memory leaks at decoding fixed 2004-07-16 10:31:10 +00:00
Antonin Descampe
70ed4ae986 Previous version reloaded 2004-07-16 10:27:27 +00:00
Francois-Olivier Devaux
47dfe606bd Memory leaks at decoding fixed 2004-07-16 10:22:59 +00:00
Antonin Descampe
cc034349cd Fixed bug (compatibility between index_create and j2k_decode) 2004-07-16 09:57:26 +00:00
Francois-Olivier Devaux
9653207b67 Multiple tile bug fixed (line 438) 2004-07-16 09:52:24 +00:00
Francois-Olivier Devaux
82eea41d24 Memory leaks fixed (line 1592: frees data of all components of current tile) 2004-07-16 09:14:31 +00:00
Antonin Descampe
8d2121606d Minor changes for RESET mode 2004-07-16 09:08:27 +00:00
Francois-Olivier Devaux
e28d3d63ea Comment introcudec (Created by OpenJPEG version 0.9) + Memory leaks fixed 2004-07-16 08:59:15 +00:00
Sebastien Lugan
c81b74d91b Bugfix Makefile / jp2.c & jp2.h 2004-07-15 15:52:44 +00:00
Antonin Descampe
17a559b49e (Sampling + Offset) incompatibility bug fixed 2004-07-14 15:32:56 +00:00
Antonin Descampe
0e9da05873 fixed bug from previous revision 2004-07-14 15:15:07 +00:00
Francois-Olivier Devaux
75cb7a7b2c Memory leak fix + Comment inserted in codestream: "Created by OpenJPEG version 0.9" 2004-07-14 13:00:57 +00:00
Francois-Olivier Devaux
c020831f95 Allocated memory of coding parameters (cp) freed 2004-07-14 10:58:11 +00:00
Antonin Descampe
55e5910d6b * PPM markers handling modified (comment keyword : ppmbug1)
* empty resolution level or subband handling (comment keyword : sizebug1)
* index_on field forwarded to j2k_cp_t structure
2004-07-14 08:52:15 +00:00
Antonin Descampe
de5a22aea0 Structure j2k_option_t deleted and option "-reduce" integrated to j2k_cp_t.
Parameters changed for j2k_decode, jp2_decode, j2k_decode_jpt_stream
2004-07-13 14:37:11 +00:00
Antonin Descampe
bc563fc5ba * Quantization bug fixed when using 9x7 DWT (comment keyword : quantizbug1)
* Multiplication bug fixed when dividing by 8192 (comment keyword : multbug1)
2004-07-13 09:17:17 +00:00
Antonin Descampe
f50f66c0c9 scalar_derived quantization type bug fixed (2) 2004-07-09 14:37:12 +00:00
Antonin Descampe
651f98ad06 Bug fixed for scalar_derived quantization type 2004-07-09 14:04:06 +00:00
Antonin Descampe
132d894726 Bug fixed in pgx-format 2004-07-09 10:12:16 +00:00
Antonin Descampe
af26e8504e Added jp2.h et jp2.c to the project 2004-07-07 09:08:49 +00:00
Francois-Olivier Devaux
d695f75c24 First version of JP2 file format 2004-07-07 08:24:36 +00:00
Francois-Olivier Devaux
b391bfa718 Include jp2.h file 2004-07-07 07:38:15 +00:00
Francois-Olivier Devaux
77ca0769e3 New variable in cp structure for JP2 format 2004-07-07 07:37:44 +00:00
Francois-Olivier Devaux
06496c203e j2k_encode can output data to a buffer or to a file 2004-07-07 07:37:14 +00:00
Francois-Olivier Devaux
da88349b22 Enables the decoding of JP2 files 2004-07-07 07:35:34 +00:00
Francois-Olivier Devaux
a41ed768c9 Enables the creation of JP2 files 2004-07-07 07:35:12 +00:00
Francois-Olivier Devaux
688a82dfd9 Sets the image color space for JP2 format 2004-07-07 07:34:15 +00:00
Francois-Olivier Devaux
3cb27c3912 Fixed bug for rate option 2004-07-06 07:50:05 +00:00
Antonin Descampe
71e60bf86c *.dsp files added for Microsoft Visual Studio users 2004-07-05 15:15:14 +00:00
Antonin Descampe
275b153fc2 small changes in the INDEX-file generation (in order to only use data from index data structures) 2004-05-07 14:41:45 +00:00
Antonin Descampe
a4911967eb * Fixed_quality option added : specifying -q psnr1,psnr2,psnr3,... at the command line when encoding an image generates layers with the corresponding psnr. You have to specify values in the increase order. This option is incompatible with "-r" or "-f" options.
* Old -q option is now available with -f

* The INDEX-file structure has been modified and is now like this :
------------------------------
image_width image_height
progression order
tile_width tile_height
nb_tiles_width nb_tiles_height
nb_components
nb_layers
nb_decompositions
foreach resolution_level {[precinct_width,precinct_height]}
main_header_end
codestream_size
foreach tile {
  tileno start_pos end_header end_pos squarred_error_total nb_pixels mean_squarred_error
}
foreach tile {
  foreach packet {
    packetno tileno layerno resno compno precinctno start_pos end_pos SE_reduction
  }
}
SE max
SE total
-----------------------------
2004-05-07 13:50:47 +00:00
Antonin Descampe
32e7b1529b Various corrections to avoid "signed/unsigned mismatch" warnings during compilation 2004-04-30 09:20:22 +00:00
Antonin Descampe
eb2f910daa Ajout de la possibilit� de taille de precincts diff�rente pour chaque r�solution 2004-04-29 15:32:38 +00:00
Sebastien Lugan
64c34e0d52 Reformatage : indent -kr -i2 -ci2 $(find . -name '*.c') $(find . -name '*.h') 2004-04-29 13:27:24 +00:00
Sebastien Lugan
ec9bd9f6a4 Reformatage : indent -kr $(find . -name '*.c') $(find . -name '*.h') 2004-04-29 13:10:05 +00:00
Yannick Verschueren
4f83e82a74 Change in the IFDEF due to error with Windows users 2004-04-19 13:11:33 +00:00
Yannick Verschueren
c74313aa15 remove obsolete part of code in t2_encode_packet 2004-04-19 13:08:55 +00:00
Yannick Verschueren
ae6227a9cf Remove call to obsolete library unistd.h 2004-04-19 13:07:00 +00:00
Yannick Verschueren
4bbd79b370 Potential error in tcd_init around L543 with p and q 2004-04-19 13:02:49 +00:00
Yannick Verschueren
e5106dd836 BMP problem solves : BMP-Images with dimension multiple of 4 presented problems 2004-03-15 10:42:29 +00:00
Yannick Verschueren
1f7520c88b Update to version 0.9 : option -reduce added on decoder 2004-03-05 14:42:53 +00:00
Yannick Verschueren
8ed5edaefa Error message changed (obsolete pnmtoj2k) 2004-03-05 14:35:02 +00:00
Yannick Verschueren
56fe7488ad Errors with some BMP file solved 2004-03-01 14:52:02 +00:00
Yannick Verschueren
994f0d518e Errors with some BMP file solved 2004-03-01 14:44:53 +00:00
Sebastien Lugan
e798d3f348 Initial revision 2004-02-17 16:33:43 +00:00
Yannick Verschueren
e4dafe5aa2 Automatic creation of building directories during make (OK for version >= 0.8) 2004-02-13 10:04:37 +00:00
Yannick Verschueren
9bf97dc1d0 File necessary for version >= 0.8 2004-02-13 10:02:12 +00:00
Yannick Verschueren
38828e8dcd Update for version 1.0 2004-02-13 09:54:31 +00:00
Yannick Verschueren
8b1431ecce Update for version 1.0 2004-02-13 09:52:31 +00:00
Yannick Verschueren
bb349b2cee Update for version 0.8 2004-02-13 09:47:40 +00:00
Yannick Verschueren
4be829988a Update for version 0.8 2004-02-13 09:38:52 +00:00
Yannick Verschueren
28283c60ea update vor version 0.8 2004-02-13 09:29:59 +00:00
Sebastien Lugan
a9cede2d5e Automatic creation of building directories during make 2004-02-12 16:07:03 +00:00
Sebastien Lugan
64bef6ba0a Initial revision 2003-11-27 14:55:16 +00:00
74 changed files with 19807 additions and 7625 deletions

View File

@@ -1,13 +0,0 @@
# The "checkoutlist" file is used to support additional version controlled
# administrative files in $CVSROOT/CVSROOT, such as template files.
#
# The first entry on a line is a filename which will be checked out from
# the corresponding RCS file in the $CVSROOT/CVSROOT directory.
# The remainder of the line is an error message to use if the file cannot
# be checked out.
#
# File format:
#
# [<whitespace>]<filename><whitespace><error message><end-of-line>
#
# comment lines begin with '#'

View File

@@ -1,15 +0,0 @@
# The "commitinfo" file is used to control pre-commit checks.
# The filter on the right is invoked with the repository and a list
# of files to check. A non-zero exit of the filter program will
# cause the commit to be aborted.
#
# The first entry on a line is a regular expression which is tested
# against the directory that the change is being committed to, relative
# to the $CVSROOT. For the first match that is found, then the remainder
# of the line is the name of the filter to run.
#
# If the repository name does not match any of the regular expressions in this
# file, the "DEFAULT" line is used, if it is specified.
#
# If the name "ALL" appears as a regular expression it is always used
# in addition to the first matching regex or "DEFAULT".

View File

@@ -1,14 +0,0 @@
# Set this to "no" if pserver shouldn't check system users/passwords
#SystemAuth=no
# Put CVS lock files in this directory rather than directly in the repository.
#LockDir=/var/lock/cvs
# Set `TopLevelAdmin' to `yes' to create a CVS directory at the top
# level of the new working directory when using the `cvs checkout'
# command.
#TopLevelAdmin=no
# Set `LogHistory' to `all' or `TOFEWGCMAR' to log all transactions to the
# history file, or a subset as needed (ie `TMAR' logs all write operations)
#LogHistory=TOFEWGCMAR

View File

@@ -1,23 +0,0 @@
# This file affects handling of files based on their names.
#
# The -t/-f options allow one to treat directories of files
# as a single file, or to transform a file in other ways on
# its way in and out of CVS.
#
# The -m option specifies whether CVS attempts to merge files.
#
# The -k option specifies keyword expansion (e.g. -kb for binary).
#
# Format of wrapper file ($CVSROOT/CVSROOT/cvswrappers or .cvswrappers)
#
# wildcard [option value][option value]...
#
# where option is one of
# -f from cvs filter value: path to filter
# -t to cvs filter value: path to filter
# -m update methodology value: MERGE or COPY
# -k expansion mode value: b, o, kkv, &c
#
# and value is a single-quote delimited value.
# For example:
#*.gif -k 'b'

View File

@@ -1,21 +0,0 @@
# The "editinfo" file is used to allow verification of logging
# information. It works best when a template (as specified in the
# rcsinfo file) is provided for the logging procedure. Given a
# template with locations for, a bug-id number, a list of people who
# reviewed the code before it can be checked in, and an external
# process to catalog the differences that were code reviewed, the
# following test can be applied to the code:
#
# Making sure that the entered bug-id number is correct.
# Validating that the code that was reviewed is indeed the code being
# checked in (using the bug-id number or a seperate review
# number to identify this particular code set.).
#
# If any of the above test failed, then the commit would be aborted.
#
# Actions such as mailing a copy of the report to each reviewer are
# better handled by an entry in the loginfo file.
#
# One thing that should be noted is the the ALL keyword is not
# supported. There can be only one entry that matches a given
# repository.

View File

@@ -1,26 +0,0 @@
# The "loginfo" file controls where "cvs commit" log information
# is sent. The first entry on a line is a regular expression which must match
# the directory that the change is being made to, relative to the
# $CVSROOT. If a match is found, then the remainder of the line is a filter
# program that should expect log information on its standard input.
#
# If the repository name does not match any of the regular expressions in this
# file, the "DEFAULT" line is used, if it is specified.
#
# If the name ALL appears as a regular expression it is always used
# in addition to the first matching regex or DEFAULT.
#
# You may specify a format string as part of the
# filter. The string is composed of a `%' followed
# by a single format character, or followed by a set of format
# characters surrounded by `{' and `}' as separators. The format
# characters are:
#
# s = file name
# V = old version number (pre-checkin)
# v = new version number (post-checkin)
#
# For example:
#DEFAULT (echo ""; id; echo %s; date; cat) >> $CVSROOT/CVSROOT/commitlog
# or
#DEFAULT (echo ""; id; echo %{sVv}; date; cat) >> $CVSROOT/CVSROOT/commitlog

View File

@@ -1,26 +0,0 @@
# Three different line formats are valid:
# key -a aliases...
# key [options] directory
# key [options] directory files...
#
# Where "options" are composed of:
# -i prog Run "prog" on "cvs commit" from top-level of module.
# -o prog Run "prog" on "cvs checkout" of module.
# -e prog Run "prog" on "cvs export" of module.
# -t prog Run "prog" on "cvs rtag" of module.
# -u prog Run "prog" on "cvs update" of module.
# -d dir Place module in directory "dir" instead of module name.
# -l Top-level directory only -- do not recurse.
#
# NOTE: If you change any of the "Run" options above, you'll have to
# release and re-checkout any working directories of these modules.
#
# And "directory" is a path to a directory relative to $CVSROOT.
#
# The "-a" option specifies an alias. An alias is interpreted as if
# everything on the right of the "-a" had been typed on the command line.
#
# You can encode a module within a module by using the special '&'
# character to interpose another module into the current module. This
# can be useful for creating a module that consists of many directories
# spread out over the entire source repository.

View File

@@ -1,12 +0,0 @@
# The "notify" file controls where notifications from watches set by
# "cvs watch add" or "cvs edit" are sent. The first entry on a line is
# a regular expression which is tested against the directory that the
# change is being made to, relative to the $CVSROOT. If it matches,
# then the remainder of the line is a filter program that should contain
# one occurrence of %s for the user to notify, and information on its
# standard input.
#
# "ALL" or "DEFAULT" can be used in place of the regular expression.
#
# For example:
#ALL mail %s -s "CVS notification"

View File

@@ -1,13 +0,0 @@
# The "rcsinfo" file is used to control templates with which the editor
# is invoked on commit and import.
#
# The first entry on a line is a regular expression which is tested
# against the directory that the change is being made to, relative to the
# $CVSROOT. For the first match that is found, then the remainder of the
# line is the name of the file that contains the template.
#
# If the repository name does not match any of the regular expressions in this
# file, the "DEFAULT" line is used, if it is specified.
#
# If the name "ALL" appears as a regular expression it is always used
# in addition to the first matching regex or "DEFAULT".

View File

@@ -1,20 +0,0 @@
# The "taginfo" file is used to control pre-tag checks.
# The filter on the right is invoked with the following arguments:
#
# $1 -- tagname
# $2 -- operation "add" for tag, "mov" for tag -F, and "del" for tag -d
# $3 -- repository
# $4-> file revision [file revision ...]
#
# A non-zero exit of the filter program will cause the tag to be aborted.
#
# The first entry on a line is a regular expression which is tested
# against the directory that the change is being committed to, relative
# to the $CVSROOT. For the first match that is found, then the remainder
# of the line is the name of the filter to run.
#
# If the repository name does not match any of the regular expressions in this
# file, the "DEFAULT" line is used, if it is specified.
#
# If the name "ALL" appears as a regular expression it is always used
# in addition to the first matching regex or "DEFAULT".

View File

@@ -1,21 +0,0 @@
# The "verifymsg" file is used to allow verification of logging
# information. It works best when a template (as specified in the
# rcsinfo file) is provided for the logging procedure. Given a
# template with locations for, a bug-id number, a list of people who
# reviewed the code before it can be checked in, and an external
# process to catalog the differences that were code reviewed, the
# following test can be applied to the code:
#
# Making sure that the entered bug-id number is correct.
# Validating that the code that was reviewed is indeed the code being
# checked in (using the bug-id number or a seperate review
# number to identify this particular code set.).
#
# If any of the above test failed, then the commit would be aborted.
#
# Actions such as mailing a copy of the report to each reviewer are
# better handled by an entry in the loginfo file.
#
# One thing that should be noted is the the ALL keyword is not
# supported. There can be only one entry that matches a given
# repository.

View File

@@ -22,13 +22,15 @@ ifdef MINGW32
OBJ_DIR = $(OBJ_DIR_W32)
BIN_DIR = $(BIN_DIR_W32)
LIB_DIR = lib.w32
all: $(addprefix $(BIN_DIR)/,j2k_to_image.exe image_to_j2k.exe)
all: $(BIN_DIR) $(OBJ_DIR) $(LIB_DIR) \
$(addprefix $(BIN_DIR)/,j2k_to_image.exe image_to_j2k.exe)
else
CC = gcc
OBJ_DIR = obj
BIN_DIR = bin
LIB_DIR = lib
all: $(addprefix $(BIN_DIR)/,j2k_to_image image_to_j2k)
all: $(BIN_DIR) $(OBJ_DIR) $(LIB_DIR) \
$(addprefix $(BIN_DIR)/,j2k_to_image image_to_j2k)
endif
ifdef DYNAMIC
@@ -44,14 +46,26 @@ else
LDFLAGS += $(LIB_OPENJPEG)
endif
$(BIN_DIR):
mkdir $(BIN_DIR)
$(OBJ_DIR):
mkdir $(OBJ_DIR)
$(LIB_DIR):
mkdir $(LIB_DIR)
$(LIB_DIR)/libopenjpeg.dll: $(OPENJPEG_DIR)/$(LIB_DIR)/libopenjpeg.dll
ln -sf ../$< $@
$(OPENJPEG_DIR)/$(LIB_DIR)/libopenjpeg.dll:
cd $(OPENJPEG_DIR) && $(MAKE) $(LIB_DIR)/libopenjpeg.dll
cd $(OPENJPEG_DIR) && \
$(MAKE) $(OBJ_DIR) $(LIB_DIR) $(LIB_DIR)/libopenjpeg.dll
$(OPENJPEG_DIR)/$(LIB_DIR)/libopenjpeg.a:
cd $(OPENJPEG_DIR) && $(MAKE) $(LIB_DIR)/libopenjpeg.a
cd $(OPENJPEG_DIR) && \
$(MAKE) $(OBJ_DIR) $(LIB_DIR) $(LIB_DIR)/libopenjpeg.a
$(OPENJPEG_DIR)/$(LIB_DIR)/libopenjpeg.so:
cd $(OPENJPEG_DIR) && $(MAKE) $(LIB_DIR)/libopenjpeg.so
cd $(OPENJPEG_DIR) && \
$(MAKE) $(LIB_DIR) $(LIB_DIR)/libopenjpeg.so
$(OBJ_DIR)/%.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

View File

@@ -30,7 +30,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
/* -->> -->> -->> -->>
@@ -70,15 +69,18 @@ typedef struct {
int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
int subsampling_dy, int Dim[2])
{
FILE *IN;
FILE *Compo0 = NULL, *Compo1 = NULL, *Compo2 = NULL;
BITMAPFILEHEADER_t File_h;
BITMAPINFOHEADER_t Info_h;
unsigned char *RGB;
unsigned char *table_R, *table_G, *table_B;
int i, w, h;
int gray_scale = 1, not_end_file = 1, line = 0, col = 0;
unsigned int j, w, h, PAD, type = 0;
int i;
int gray_scale = 1, not_end_file = 1;
unsigned int line = 0, col = 0;
unsigned char v, v2;
UINT4 W, H;
@@ -94,7 +96,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
File_h.bfType = (getc(IN) << 8) + File_h.bfType;
if (File_h.bfType != 19778) {
printf("Error, not a BMP file!\n");
fprintf(stderr,"Error, not a BMP file!\n");
return 0;
} else {
/* FILE HEADER */
@@ -185,6 +187,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
1) *
subsampling_dy + 1;
img->numcomps = 3;
img->color_space = 1;
img->comps =
(j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) {
@@ -216,30 +219,41 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
W = Info_h.biWidth;
H = Info_h.biHeight;
if (Info_h.biWidth % 2)
W++;
RGB = (unsigned char *) malloc(3 * W * H * sizeof(unsigned char));
// PAD = 4 - (3 * W) % 4;
// PAD = (PAD == 4) ? 0 : PAD;
PAD = (3 * W) % 4 ? 4 - (3 * W) % 4 : 0;
fread(RGB, sizeof(unsigned char), 3 * W * H, IN);
for (i = 0; i < W * H; i++) {
unsigned char R, G, B;
B =
RGB[3 * W * H - ((i * 3) / (3 * W) + 1) * 3 * W +
(i * 3) % (W * 3)];
G =
RGB[3 * W * H - ((i * 3 + 1) / (3 * W) + 1) * 3 * W +
(i * 3 + 1) % (W * 3)];
R =
RGB[3 * W * H - ((i * 3 + 2) / (3 * W) + 1) * 3 * W +
(i * 3 + 2) % (W * 3)];
if ((i % W < W - 1 && Info_h.biWidth % 2) || !(Info_h.biWidth % 2)) {
fprintf(Compo0, "%c", R);
fprintf(Compo1, "%c", G);
fprintf(Compo2, "%c", B);
RGB =
(unsigned char *) malloc((3 * W + PAD) * H *
sizeof(unsigned char));
fread(RGB, sizeof(unsigned char), (3 * W + PAD) * H, IN);
for (j = 0; j < (3 * W + PAD) * H; j++) {
unsigned char elmt;
int Wp = 3 * W + PAD;
elmt = RGB[(H - (j / Wp + 1)) * Wp + j % Wp];
if ((j % Wp) < (3 * W)) {
switch (type) {
case 0:
fprintf(Compo2, "%c", elmt);
type = 1;
break;
case 1:
fprintf(Compo1, "%c", elmt);
type = 2;
break;
case 2:
fprintf(Compo0, "%c", elmt);
type = 0;
break;
}
}
}
fclose(Compo0);
fclose(Compo1);
fclose(Compo2);
@@ -260,13 +274,13 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
for (i = 0; i < Info_h.biClrUsed; i++) {
table_B[i] = getc(IN);
table_G[i] = getc(IN);
table_R[i] = getc(IN);
for (j = 0; j < Info_h.biClrUsed; j++) {
table_B[j] = getc(IN);
table_G[j] = getc(IN);
table_R[j] = getc(IN);
getc(IN);
if (table_R[i] != table_G[i] && table_R[i] != table_B[i]
&& table_G[i] != table_B[i])
if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
&& table_G[j] != table_B[j])
gray_scale = 0;
}
@@ -296,11 +310,11 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
fprintf(stderr,
"\033[0;33mFailed to open Compo0 for writing !\033[0;39m\n");
}
for (i = 0; i < W * H; i++) {
if ((i % W < W - 1 && Info_h.biWidth % 2)
for (j = 0; j < W * H; j++) {
if ((j % W < W - 1 && Info_h.biWidth % 2)
|| !(Info_h.biWidth % 2))
fprintf(Compo0, "%c",
table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
}
fclose(Compo0);
} else {
@@ -331,15 +345,15 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
"\033[0;33mFailed to open Compo2 for writing !\033[0;39m\n");
}
for (i = 0; i < W * H; i++) {
if ((i % W < W - 1 && Info_h.biWidth % 2)
for (j = 0; j < W * H; j++) {
if ((j % W < W - 1 && Info_h.biWidth % 2)
|| !(Info_h.biWidth % 2)) {
fprintf(Compo0, "%c",
table_R[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
table_R[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
fprintf(Compo1, "%c",
table_G[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
table_G[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
fprintf(Compo2, "%c",
table_B[RGB[W * H - ((i) / (W) + 1) * W + (i) % (W)]]);
table_B[RGB[W * H - ((j) / (W) + 1) * W + (j) % (W)]]);
}
}
@@ -361,19 +375,17 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
1) *
subsampling_dy + 1;
table_R = (unsigned char *) malloc(256 * sizeof(unsigned char));
table_G = (unsigned char *) malloc(256 * sizeof(unsigned char));
table_B = (unsigned char *) malloc(256 * sizeof(unsigned char));
for (i = 0; i < Info_h.biClrUsed; i++) {
table_B[i] = getc(IN);
table_G[i] = getc(IN);
table_R[i] = getc(IN);
for (j = 0; j < Info_h.biClrUsed; j++) {
table_B[j] = getc(IN);
table_G[j] = getc(IN);
table_R[j] = getc(IN);
getc(IN);
if (table_R[i] != table_G[i] && table_R[i] != table_B[i]
&& table_G[i] != table_B[i])
if (table_R[j] != table_G[j] && table_R[j] != table_B[j]
&& table_G[j] != table_B[j])
gray_scale = 0;
}
@@ -446,7 +458,7 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
not_end_file = 0;
break;
case 2:
printf("No Delta supported\n");
fprintf(stderr,"No Delta supported\n");
return 1;
break;
default:
@@ -463,26 +475,26 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
if (gray_scale) {
for (line = 0; line < Info_h.biHeight; line++)
for (col = 0; col < Info_h.biWidth; col++)
fprintf(Compo0, "%c",
table_R[(int)
fprintf(Compo0, "%c", table_R[(int)
RGB[(Info_h.biHeight - line -
1) * Info_h.biWidth + col]]);
1) * Info_h.biWidth +
col]]);
fclose(Compo0);
} else {
for (line = 0; line < Info_h.biHeight; line++)
for (col = 0; col < Info_h.biWidth; col++) {
fprintf(Compo0, "%c",
table_R[(int)
fprintf(Compo0, "%c", table_R[(int)
RGB[(Info_h.biHeight - line -
1) * Info_h.biWidth + col]]);
fprintf(Compo1, "%c",
table_G[(int)
1) * Info_h.biWidth +
col]]);
fprintf(Compo1, "%c", table_G[(int)
RGB[(Info_h.biHeight - line -
1) * Info_h.biWidth + col]]);
fprintf(Compo2, "%c",
table_B[(int)
1) * Info_h.biWidth +
col]]);
fprintf(Compo2, "%c", table_B[(int)
RGB[(Info_h.biHeight - line -
1) * Info_h.biWidth + col]]);
1) * Info_h.biWidth +
col]]);
}
fclose(Compo0);
fclose(Compo1);
@@ -490,8 +502,8 @@ int bmptoimage(char *filename, j2k_image_t * img, int subsampling_dx,
}
free(RGB);
} else
printf
("Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
fprintf(stderr,
"Other system than 24 bits/pixels or 8 bits (no RLE coding) is not yet implemented [%d]\n",
Info_h.biBitCount);
fclose(IN);
@@ -544,32 +556,72 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
FILE *f;
int w, h, prec;
int i, compno, bandno;
char str[256], endian[16];
char sign;
char str[256];
char endian1,endian2,sign;
char signtmp[32];
char temp[32];
int bigendian;
j2k_comp_t *comp;
img->numcomps = 1;
img->color_space = 2;
img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (compno = 0; compno < img->numcomps; compno++) {
FILE *src;
char tmp[16];
int max = 0;
int Y1;
comp = &img->comps[compno];
sprintf(str, "%s", filename);
f = fopen(str, "rb");
if (!f) {
fprintf(stderr, "Failed to open %s for reading !\n", str);
return 0;
}
if (fscanf(f, "PG %s %c %d %d %d", endian, &sign, &prec, &w, &h) == 5)
{
fseek(f, 0, SEEK_SET);
fscanf(f, "PG%[ \t]%c%c%[ \t+-]%d%[ \t]%d%[ \t]%d",temp,&endian1,&endian2,signtmp,&prec,temp,&w,temp,&h);
i=0;
sign='+';
while (signtmp[i]!='\0') {
if (signtmp[i]=='-') sign='-';
i++;
}
fgetc(f);
if (!strcmp(endian, "ML"))
if (endian1=='M' && endian2=='L')
bigendian = 1;
else
else if (endian2=='M' && endian1=='L')
bigendian = 0;
else {
fprintf(stderr, "Bad pgx header, please check input file\n");
return 0;
}
if (compno == 0) {
img->x0 = Dim[0];
img->y0 = Dim[1];
@@ -596,8 +648,7 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
comp->dy = subsampling_dy;
bandno = 1;
Y1 =
cp.ty0 + bandno * cp.tdy <
Y1 = cp.ty0 + bandno * cp.tdy <
img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
Y1 -= img->y0;
@@ -616,8 +667,7 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
if (!src) {
fprintf(stderr, "failed to open %s for writing !\n", tmp);
}
Y1 =
cp.ty0 + bandno * cp.tdy <
Y1 = cp.ty0 + bandno * cp.tdy <
img->y1 ? cp.ty0 + bandno * cp.tdy : img->y1;
Y1 -= img->y0;
}
@@ -644,9 +694,6 @@ int pgxtoimage(char *filename, j2k_image_t * img, int tdy,
max = v;
fprintf(src, "%d ", v);
}
} else {
return 0;
}
fclose(f);
fclose(src);
comp->bpp = int_floorlog2(max) + 1;
@@ -707,6 +754,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
subsampling_dy + 1;
img->numcomps = 1;
img->color_space = 2;
img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
img->comps[0].prec = 8;
img->comps[0].bpp = 8;
@@ -750,6 +798,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
subsampling_dy + 1;
img->numcomps = 1;
img->color_space = 2;
img->comps = (j2k_comp_t *) malloc(sizeof(j2k_comp_t));
img->comps[0].prec = 8;
img->comps[0].bpp = 8;
@@ -791,6 +840,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
1) *
subsampling_dy + 1;
img->numcomps = 3;
img->color_space = 1;
img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) {
img->comps[i].prec = 8;
@@ -853,6 +903,7 @@ int pnmtoimage(char *filename, j2k_image_t * img, int subsampling_dx,
1) *
subsampling_dy + 1;
img->numcomps = 3;
img->color_space = 1;
img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) {
img->comps[i].prec = 8;

View File

@@ -26,11 +26,26 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
@@ -41,137 +56,152 @@
void help_display()
{
printf("HELP\n----\n\n");
printf
("- the option -help displays the readme.txt file on screen\n\n");
fprintf(stdout,"HELP\n----\n\n");
fprintf(stdout,"- the -h option displays this help information on screen\n\n");
printf("List of parameters for the coder JPEG 2000 :\n");
printf("\n");
printf
("- The markers COD and QCD are writed both of two in the main_header and never appear in the tile_header. The markers in the main header are : SOC SIZ COD QCD COM.\n");
printf("\n");
printf
("- This coder can encode mega image, a test was made on a 24000x24000 pixels color image. You need enough disk space memory (twice the original) to encode the image. (i.e. for a 1.5 Gb image you need a minimum of 3Gb of disk memory)\n");
printf("\n");
printf("REMARKS :\n");
printf("---------\n");
printf("\n");
printf
("* the value of rate enter in the code line is the compression factor !\n");
printf("exemple :\n");
printf("\n");
printf
("-r 20,10,1 means quality 1 : compress 20x, quality 2 : compress 10x and quality 3 : compress 1x = lossless\n");
printf("\n");
printf("By default :\n");
printf("------------\n");
printf("\n");
printf(" * lossless\n");
printf(" * 1 tile\n");
printf(" * size of precinct 2^15 x 2^15 (means 1 precinct)\n");
printf(" * size of code-block 64 x 64\n");
printf(" * Number of resolution : 6\n");
printf(" * No SOP marker in the codestream\n");
printf(" * No EPH marker in the codestream\n");
printf(" * No sub-sampling in x and y direction\n");
printf(" * No mode switch activated\n");
printf(" * progression order : LRCP\n");
printf(" * No index file\n");
printf(" * No ROI upshifted\n");
printf(" * No offset of the origin of the image\n");
printf(" * No offset of the origin of the tiles\n");
printf(" * Reversible DWT 5-3\n");
printf("\n");
printf("Parameters :\n");
printf("------------\n");
printf("\n");
printf
("-i : source file (-i source.pnm also *.pgm, *.ppm) required\n");
printf("\n");
printf
("-o : destination file (-o dest.j2k) required\n");
printf("\n");
printf
("-help : Display the help information optional\n ");
printf("\n");
printf
("-r : different rates (-r 20,10,5) optional\n ");
printf("\n");
printf
("-n : Number of resolution (-n 3) optional\n");
printf("\n");
printf
("-b : size of code block (-b 32,32) optional\n");
printf("\n");
printf
("-c : size of precinct (-c 128,128) optional\n");
printf("\n");
printf
("-t : size of tile (-t 512,512) optional\n");
printf("\n");
printf
("-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] optional\n");
printf("\n");
printf
("-s : subsampling factor (-s 2,2) [-s X,Y] optional\n");
printf("\n");
printf
("-SOP : write SOP marker before each packet optional\n");
printf("\n");
printf
("-EPH : write EPH marker after each header packet optional\n");
printf("\n");
printf
("-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL) 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] optional\n");
printf
(" for several mode switch you have to add the value of each mode you want\n");
printf
(" ex : RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
printf("\n");
printf
("-x : Create an index file *.Idx (-x index_name.Idx) optional\n");
printf("\n");
printf
("-ROI:c=%%d,U=%%d : quantization indices upshifted for component c=%%d [\%%d = 0,1,2]\n");
printf
(" with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) optional\n");
printf("\n");
printf
("-d : offset of the origin of the image (-d 150,300) optional\n");
printf("\n");
printf
("-T : offset of the origin of the tiles (-T 100,75) optional\n");
printf("\n");
printf
("-I : Use the irreversible DWT 9-7 (-I) optional\n");
printf("\n");
printf("IMPORTANT :\n");
printf("-----------\n");
printf("\n");
printf("* subsampling bigger than 2 can produce error\n");
printf("\n");
printf
("The index file respect the structure below :\n");
printf
("---------------------------------------------\n");
printf("\n");
printf("Image_height Image_width\n");
printf("progression order\n");
printf("Tiles_size_X Tiles_size_Y\n");
printf("Components_nb\n");
printf("Layers_nb\n");
printf("decomposition_levels\n");
printf("Precincts_size_X Precincts_size_Y\n");
printf("Main_header_end_position\n");
printf("Codestream_size\n");
printf("Tile0 start_pos end_Theader end_pos\n");
printf("Tile1 '' '' ''\n");
printf("...\n");
printf("TileN '' '' ''\n");
printf("Tpacket_0 Tile layer res. comp. prec. start_pos end_pos\n");
printf("...\n");
printf("Tpacket_M '' '' '' '' '' '' ''\n");
fprintf(stdout,"List of parameters for the coder JPEG 2000:\n");
fprintf(stdout,"\n");
fprintf(stdout,"REMARKS:\n");
fprintf(stdout,"---------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
fprintf
(stdout,"COD and QCD never appear in the tile_header.\n");
fprintf(stdout,"\n");
fprintf
(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
fprintf
(stdout,"color image. You need enough disk space memory (twice the original) to encode \n");
fprintf
(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
fprintf(stdout,"\n");
fprintf(stdout,"By default:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf(stdout," * Lossless\n");
fprintf(stdout," * 1 tile\n");
fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
fprintf(stdout," * Size of code-block : 64 x 64\n");
fprintf(stdout," * Number of resolutions: 6\n");
fprintf(stdout," * No SOP marker in the codestream\n");
fprintf(stdout," * No EPH marker in the codestream\n");
fprintf(stdout," * No sub-sampling in x or y direction\n");
fprintf(stdout," * No mode switch activated\n");
fprintf(stdout," * Progression order: LRCP\n");
fprintf(stdout," * No index file\n");
fprintf(stdout," * No ROI upshifted\n");
fprintf(stdout," * No offset of the origin of the image\n");
fprintf(stdout," * No offset of the origin of the tiles\n");
fprintf(stdout," * Reversible DWT 5-3\n");
fprintf(stdout,"\n");
fprintf(stdout,"Parameters:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"Required Parameters (except with -h):\n");
fprintf(stdout,"\n");
fprintf
(stdout,"-i : source file (-i source.pnm also *.pgm, *.ppm) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-o : destination file (-o dest.j2k or .jp2) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"Optional Parameters:\n");
fprintf(stdout,"\n");
fprintf(stdout,"-h : display the help information \n ");
fprintf(stdout,"\n");
fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
fprintf(stdout," - The rate specified for each quality level is the desired \n");
fprintf(stdout," compression factor.\n");
fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
fprintf(stdout,"\n");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"\n");
fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"\n");
fprintf(stdout,"-n : number of resolutions (-n 3) \n");
fprintf(stdout,"\n");
fprintf(stdout,"-b : size of code block (-b 32,32) \n");
fprintf(stdout,"\n");
fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
fprintf(stdout,"\n");
fprintf(stdout,"-t : size of tile (-t 512,512) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
fprintf(stdout,"\n");
fprintf
(stdout,"-SOP : write SOP marker before each packet \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-EPH : write EPH marker after each header packet \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
fprintf
(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
fprintf
(stdout," Indicate multiple modes by adding their values. \n");
fprintf
(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
fprintf(stdout,"\n");
fprintf
(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
fprintf
(stdout," for component c=%%d [%%d = 0,1,2]\n");
fprintf
(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-d : offset of the origin of the image (-d 150,300) \n");
fprintf(stdout,"\n");
fprintf
(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
fprintf(stdout,"\n");
fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
fprintf(stdout,"\n");
fprintf(stdout,"IMPORTANT:\n");
fprintf(stdout,"-----------\n");
fprintf(stdout,"\n");
fprintf(stdout,"The index file has the structure below:\n");
fprintf(stdout,"---------------------------------------\n");
fprintf(stdout,"\n");
fprintf(stdout,"Image_height Image_width\n");
fprintf(stdout,"progression order\n");
fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
fprintf(stdout,"Components_nb\n");
fprintf(stdout,"Layers_nb\n");
fprintf(stdout,"decomposition_levels\n");
fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
fprintf(stdout,"Main_header_end_position\n");
fprintf(stdout,"Codestream_size\n");
fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
fprintf(stdout,"MaxDisto\n");
fprintf(stdout,"TotalDisto\n\n");
}
int give_progression(char progression[4])
@@ -220,12 +250,13 @@ int floorlog2(int a)
return l;
}
void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
void encode_stepsize(float stepsize, int numbps, int *expn, int *mant)
{
int p, n;
p = floorlog2(stepsize) - 13;
n = 11 - floorlog2(stepsize);
*mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
int p, n, stepsizeTMP;
stepsizeTMP=(int) floor(stepsize * 8192.0);
p = floorlog2(stepsizeTMP) - 13;
n = 11 - floorlog2(stepsizeTMP);
*mant = (n < 0 ? stepsizeTMP >> -n : stepsizeTMP << n) & 0x7ff;
*expn = numbps - p;
}
@@ -234,7 +265,7 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
int numbands, bandno;
numbands = 3 * tccp->numresolutions - 2;
for (bandno = 0; bandno < numbands; bandno++) {
double stepsize;
float stepsize;
int resno, level, orient, gain;
resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
@@ -250,7 +281,7 @@ void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
double norm = dwt_norms_97[orient][level];
stepsize = (1 << (gain + 1)) / norm;
}
encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
encode_stepsize(stepsize, prec + gain,
&tccp->stepsizes[bandno].expn,
&tccp->stepsizes[bandno].mant);
}
@@ -260,14 +291,16 @@ int main(int argc, char **argv)
{
int len;
int NumResolution, numD_min; /* NumResolution : number of resolution */
int Tile_arg; /* Tile_arg = 0 (pas en argument ou = 1 (entre en argument) */
int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
int CSty; /* CSty : coding style */
int Prog_order; /* progression order (default LRCP) */
char progression[4];
int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
int prcw_init, prch_init; /* Initialisation precincts' size */
int prcw_init[J2K_MAXRLVLS]; /* Initialisation Precinct width */
int prch_init[J2K_MAXRLVLS]; /* Initialisation Precinct height */
//int prcw_init, prch_init; /* Initialisation precincts' size */
int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
int mode, value; /* mode switch (cblk_style) */
int mode, value; /* Mode switch (cblk_style) */
int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
int ROI_compno, ROI_shift; /* region of interrest */
int Dim[2]; /* portion of the image coded */
@@ -284,6 +317,13 @@ int main(int argc, char **argv)
char *index = 0;
char *s, S1, S2, S3;
int ir = 0;
int res_spec = 0; /* For various precinct sizes specification */
char sep;
char *j2k_codestream;
char *jp2_codestream;
FILE *f;
int depth_0, sign, depth;
/* default value */
/* ------------- */
@@ -293,7 +333,7 @@ int main(int argc, char **argv)
cblockh_init = 64;
cp.tw = 1;
cp.th = 1;
img.index_on = 0;
cp.index_on = 0;
Prog_order = 0;
numpocs = 0;
mode = 0;
@@ -305,9 +345,10 @@ int main(int argc, char **argv)
Dim[1] = 0;
TX0 = 0;
TY0 = 0;
cp.comment = NULL;
cp.comment = "Created by OpenJPEG version 0.9";
cp.disto_alloc = 0;
cp.fixed_alloc = 0;
cp.fixed_quality = 0; //add fixed_quality
/* img.PPT=0; */
Tile_arg = 0;
@@ -315,58 +356,88 @@ int main(int argc, char **argv)
tcp_init = &cp_init.tcps[0];
tcp_init->numlayers = 0;
cp.intermed_file = 1;
while (1) {
int c =
getopt(argc, argv, "i:o:r:q:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
int c = getopt(argc, argv,
"i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I");
if (c == -1)
break;
switch (c) {
case 'i': /* IN fill */
infile = optarg;
s = optarg;
while (*s && *s != '.') {
while (*s) {
s++;
}
s++;
S1 = *s;
s++;
S2 = *s;
s++;
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
|| (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
cp.image_type = 0;
cp.decod_format = PGX_DFMT;
break;
}
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p'
&& S2 == 'g'
&& S3 == 'm')
|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P'
&& S2 == 'P'
|| (S1 == 'P' && S2 == 'N' && S3 == 'M')
|| (S1 == 'p' && S2 == 'g' && S3 == 'm') || (S1 == 'P'
&& S2 == 'G'
&& S3 == 'M')
|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
cp.image_type = 1;
|| (S1 == 'P' && S2 == 'P' && S3 == 'M') || (S1 == 'p'
&& S2 == 'p'
&& S3 == 'm')) {
cp.decod_format = PXM_DFMT;
break;
}
if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
|| (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
cp.image_type = 2;
cp.decod_format = BMP_DFMT;
break;
}
fprintf(stderr,
"!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n");
"!! Unrecognized format for infile : %c%c%c [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n\n",
S1, S2, S3);
return 1;
break;
/* ----------------------------------------------------- */
case 'o': /* OUT fill */
outfile = optarg;
while (*outfile) {
outfile++;
}
outfile--;
S3 = *outfile;
outfile--;
S2 = *outfile;
outfile--;
S1 = *outfile;
outfile = optarg;
if ((S1 == 'j' && S2 == '2' && S3 == 'k')
|| (S1 == 'J' && S2 == '2' && S3 == 'K'))
cp.cod_format = J2K_CFMT;
else if ((S1 == 'j' && S2 == 'p' && S3 == '2')
|| (S1 == 'J' && S2 == 'P' && S3 == '2'))
cp.cod_format = JP2_CFMT;
else {
fprintf(stderr,
"Unknown output format image *.%c%c%c [only *.j2k, *.jp2]!! \n",
S1, S2, S3);
return 1;
}
break;
/* ----------------------------------------------------- */
case 'r': /* rates */
case 'r': /* rates rates/distorsion */
s = optarg;
while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers]) == 1) {
tcp_init->numlayers++;
@@ -378,13 +449,59 @@ int main(int argc, char **argv)
s++;
}
cp.disto_alloc = 1;
cp.matrice = NULL;
break;
/* ----------------------------------------------------- */
case 'q': /* rates */
case 'q': /* add fixed_quality */
s = optarg;
while (sscanf(s, "%f", &tcp_init->distoratio[tcp_init->numlayers])
== 1) {
tcp_init->numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
cp.fixed_quality = 1;
cp.matrice = NULL;
break;
/* dda */
/* ----------------------------------------------------- */
case 'f': /* mod fixed_quality (before : -q) */
s = optarg;
sscanf(s, "%d", &tcp_init->numlayers);
s++;
if (tcp_init->numlayers > 9)
s++;
cp.matrice =
(int *) malloc(tcp_init->numlayers * NumResolution * 3 *
sizeof(int));
s = s + 2;
for (i = 0; i < tcp_init->numlayers; i++) {
tcp_init->rates[i] = 1;
sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
s += 2;
if (cp.matrice[i * NumResolution * 3] > 9)
s++;
cp.matrice[i * NumResolution * 3 + 1] = 0;
cp.matrice[i * NumResolution * 3 + 2] = 0;
for (j = 1; j < NumResolution; j++) {
sscanf(s, "%d,%d,%d",
&cp.matrice[i * NumResolution * 3 + j * 3 + 0],
&cp.matrice[i * NumResolution * 3 + j * 3 + 1],
&cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
s += 6;
if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
s++;
}
if (i < tcp_init->numlayers - 1)
s++;
}
cp.fixed_alloc = 1;
break;
@@ -399,8 +516,16 @@ int main(int argc, char **argv)
break;
/* ----------------------------------------------------- */
case 'c': /* precinct dimension */
sscanf(optarg, "%d,%d", &prcw_init, &prch_init);
s = optarg;
do {
sep = 0;
sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
&prch_init[res_spec], &sep);
CSty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
}
while (sep == ',');
break;
/* ----------------------------------------------------- */
case 'b': /* code-block dimension */
@@ -415,12 +540,17 @@ int main(int argc, char **argv)
/* ----------------------------------------------------- */
case 'x': /* creation of index file */
index = optarg;
img.index_on = 1;
cp.index_on = 1;
break;
/* ----------------------------------------------------- */
case 'p': /* progression order */
sscanf(optarg, "%s", progression);
s = optarg;
for (i = 0; i < 4; i++) {
progression[i] = *s;
s++;
}
Prog_order = give_progression(progression);
if (Prog_order == -1) {
fprintf(stderr,
"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
@@ -455,8 +585,7 @@ int main(int argc, char **argv)
fprintf(stderr, "\\----------------------------------/\n");
s = optarg;
while (sscanf
(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
@@ -492,16 +621,14 @@ int main(int argc, char **argv)
/* ------------------------------------------------------ */
case 'R': /* ROI */
if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
fprintf(stderr,
"ROI error !! [-ROI:c='compno',U='shift']\n");
fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'T': /* Tile offset */
if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
fprintf(stderr,
"-T 'tile offset' argument error !! [-T X0,Y0]");
fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
break;
@@ -526,19 +653,20 @@ int main(int argc, char **argv)
/* -------------- */
if (!infile || !outfile) {
fprintf(stderr,
"usage: pnmtoj2k -i pnm-file -o j2k-file\n");
"usage: image_to_j2k -i image-file -o j2k/jp2-file (+ options)\n");
return 1;
}
if (cp.disto_alloc & cp.fixed_alloc) {
if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
&& (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
fprintf(stderr,
"Error: option -r and -q can not be used together !!\n");
"Error: options -r -q and -f cannot be used together !!\n");
return 1;
}
} // mod fixed_quality
/* if no rate entered, lossless by default */
if (tcp_init->numlayers == 0) {
tcp_init->rates[tcp_init->numlayers] = 1;
tcp_init->rates[0] = 0; //MOD antonin : losslessbug
tcp_init->numlayers++;
cp.disto_alloc = 1;
}
@@ -558,12 +686,12 @@ int main(int argc, char **argv)
}
}
switch (cp.image_type) {
case 0:
switch (cp.decod_format) {
case PGX_DFMT:
if (Tile_arg) {
if (!pgxtoimage
(infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim,
cp)) {
(infile, &img, cp.tdy, subsampling_dx, subsampling_dy, Dim, cp))
{
fprintf(stderr, "not a pgx file\n");
return 1;
}
@@ -576,14 +704,14 @@ int main(int argc, char **argv)
}
break;
case 1:
case PXM_DFMT:
if (!pnmtoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
fprintf(stderr, " not a pnm file\n");
return 1;
}
break;
case 2:
case BMP_DFMT:
if (!bmptoimage(infile, &img, subsampling_dx, subsampling_dy, Dim)) {
fprintf(stderr, " not a bmp file\n");
return 1;
@@ -610,6 +738,13 @@ int main(int argc, char **argv)
cp.tdy = img.y1 - cp.ty0;
}
/* Initialization for PPM marker */
cp.ppm = 0;
cp.ppm_data = NULL;
cp.ppm_data_first = NULL;
cp.ppm_previous = 0;
cp.ppm_store = 0;
/* Init the mutiple tiles */
/* ---------------------- */
cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
@@ -618,13 +753,24 @@ int main(int argc, char **argv)
tcp = &cp.tcps[tileno];
tcp->numlayers = tcp_init->numlayers;
for (j = 0; j < tcp->numlayers; j++) {
if (cp.fixed_quality) // add fixed_quality
tcp->distoratio[j] = tcp_init->distoratio[j];
else
tcp->rates[j] = tcp_init->rates[j];
}
tcp->csty = CSty;
tcp->prg = Prog_order;
tcp->mct = img.numcomps == 3 ? 1 : 0;
tcp->ppt = 0;
tcp->ppt_data = NULL;
tcp->ppt_data_first = NULL;
tcp->ppt_store = 0;
numpocs_tile = 0;
if (numpocs) { /* intialisation of POC */
tcp->POC = 0;
if (numpocs) {
/* intialisation of POC */
tcp->POC = 1;
for (i = 0; i < numpocs; i++) {
if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
tcp_poc = &tcp->pocs[numpocs_tile];
@@ -657,23 +803,34 @@ int main(int argc, char **argv)
else
tccp->roishift = 0;
if (CSty & J2K_CCP_CSTY_PRT) {
for (j = 0; j < tccp->numresolutions; j++) {
int size_prcw, size_prch;
size_prcw = prcw_init >> (tccp->numresolutions - j - 1);
size_prch = prch_init >> (tccp->numresolutions - j - 1);
if (size_prcw < 1) {
int p = 0;
for (j = tccp->numresolutions - 1; j >= 0; j--) {
if (p < res_spec) {
if (prcw_init[p] < 1)
tccp->prcw[j] = 1;
} else {
tccp->prcw[j] =
int_floorlog2(prcw_init >> (tccp->numresolutions - j - 1));
}
if (size_prch < 1) {
else
tccp->prcw[j] = int_floorlog2(prcw_init[p]);
if (prch_init[p] < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(prch_init[p]);
} else {
tccp->prch[j] =
int_floorlog2(prch_init >> (tccp->numresolutions - j - 1));
int size_prcw, size_prch;
size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
if (size_prcw < 1)
tccp->prcw[j] = 1;
else
tccp->prcw[j] = int_floorlog2(size_prcw);
if (size_prch < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(size_prch);
}
p++;
/*printf("\nsize precinct pour level %d : %d,%d\n", j,
tccp->prcw[j], tccp->prch[j]); */
}
} else {
for (j = 0; j < tccp->numresolutions; j++) {
@@ -685,36 +842,163 @@ int main(int argc, char **argv)
}
}
if (cp.cod_format == J2K_CFMT) { /* J2K format output */
if (cp.intermed_file == 1) { /* After the encoding of each tile, j2k_encode
stores the data in the file */
len = j2k_encode(&img, &cp, outfile, cp.tdx * cp.tdy * 2, index);
if (len == 0) {
fprintf(stderr, "failed to encode image\n");
return 1;
}
} else {
j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
/* Allocate memory for all tiles */
cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len =
j2k_encode(&img, &cp, j2k_codestream,
cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
if (len == 0) {
fprintf(stderr, "failed to encode image\n");
return 1;
}
f = fopen(outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1;
}
fwrite(j2k_codestream, 1, len, f);
free(j2k_codestream);
fclose(f);
}
} else { /* JP2 format output */
jp2_struct_t *jp2_struct;
if (cp.image_type) { /* PNM PGM PPM */
/* Remove the temporary file Compo */
/* ------------------------------- */
if (cp.intermed_file == 1) {
/*For the moment, JP2 format does not use intermediary
files for each tile */
cp.intermed_file = 0;
}
j2k_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
jp2_codestream = (char *) malloc(cp.tdx * cp.tdy * cp.tw * cp.th * 2);
cio_init(j2k_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = j2k_encode(&img, &cp, j2k_codestream,
cp.tdx * cp.tdy * cp.tw * cp.th * 2, index);
if (len == 0) {
fprintf(stderr, "failed to encode image\n");
return 1;
}
jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
jp2_struct->image = &img;
jp2_struct->j2k_codestream_len = len;
/* Initialising the standard JP2 box content */
/* If you wish to modify those boxes, you have to modify
the jp2_struct content */
jp2_struct->numcomps = img.numcomps; // NC
if (jp2_init_stdjp2(jp2_struct)) {
fprintf(stderr, "Error with jp2 initialization");
return 1;
};
jp2_struct->h = img.y1 - img.y0; // HEIGHT
jp2_struct->w = img.x1 - img.x0; // WIDTH
depth_0 = img.comps[0].prec - 1;
sign = img.comps[0].sgnd;
jp2_struct->bpc = depth_0 + (sign << 7);
for (i = 1; i < img.numcomps; i++) {
depth = img.comps[i].prec - 1;
sign = img.comps[i].sgnd;
if (depth_0 != depth)
jp2_struct->bpc = 255;
}
for (i = 0; i < img.numcomps; i++)
jp2_struct->comps[i].bpcc =
img.comps[i].prec - 1 + (img.comps[i].sgnd << 7);
if ((img.numcomps == 1 || img.numcomps == 3)
&& (jp2_struct->bpc != 255))
jp2_struct->meth = 1;
else
jp2_struct->meth = 2;
if (jp2_struct->meth == 1) {
if (img.color_space == 1)
jp2_struct->enumcs = 16;
else if (img.color_space == 2)
jp2_struct->enumcs = 17;
else if (img.color_space == 3)
jp2_struct->enumcs = 18; // YUV
} else
jp2_struct->enumcs = 0; // PROFILE (??)
cio_init(jp2_codestream, cp.tdx * cp.tdy * cp.tw * cp.th * 2);
len = jp2_wrap_j2k(jp2_struct, j2k_codestream, jp2_codestream);
f = fopen(outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1;
}
fwrite(jp2_codestream, 1, len, f);
free(jp2_codestream);
free(j2k_codestream);
free(jp2_struct->comps);
free(jp2_struct->cl);
free(jp2_struct);
fclose(f);
}
/* Remove the temporary files */
/* -------------------------- */
if (cp.decod_format != PGX_DFMT) { /* PNM PGM PPM or BMP */
for (i = 0; i < img.numcomps; i++) {
char tmp[256];
sprintf(tmp, "Compo%d", i);
if (unlink(tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n",
tmp);
char tmp;
sprintf(&tmp, "Compo%d", i);
if (remove(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n", &tmp);
}
}
} else { /* PGX */
/* Kill temporary bandtile file for cleaning the memory space on user's disk */
for (i = 0; i < cp.th; i++) {
char tmp;
sprintf(&tmp, "bandtile%d", i + 1);
if (unlink(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n",
&tmp);
if (remove(&tmp) == -1) {
fprintf(stderr, "failed to kill %s file !\n", &tmp);
}
}
}
/* Free memory */
free(img.comps);
free(cp_init.tcps);
if (tcp_init->numlayers > 9)
free(cp.matrice);
for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
free(cp.tcps[tileno].tccps);
free(cp.tcps);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

249
codec/image_to_j2k.dsp Normal file
View File

@@ -0,0 +1,249 @@
# Microsoft Developer Studio Project File - Name="image_to_j2k" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=image_to_j2k - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "image_to_j2k.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "image_to_j2k.mak" CFG="image_to_j2k - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "image_to_j2k - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "image_to_j2k - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "image_to_j2k - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD BASE RSC /l 0x80c /d "NDEBUG"
# ADD RSC /l 0x80c /i "../libopenjpeg" /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "image_to_j2k - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "_DEBUG" /D "DONT_HAVE_GETOPT" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x80c /d "_DEBUG"
# ADD RSC /l 0x80c /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "image_to_j2k - Win32 Release"
# Name "image_to_j2k - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=.\convert.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.c
# End Source File
# Begin Source File
SOURCE=.\compat\getopt.c
# End Source File
# Begin Source File
SOURCE=.\image_to_j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=.\convert.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

View File

@@ -1,6 +1,6 @@
/* Copyright (c) 2001 David Janssens
* Copyright (c) 2002 Yannick Verschueren
* Copyright (c) 2002 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2002-2003 Yannick Verschueren
* Copyright (c) 2002-2003 Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
*
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
@@ -27,256 +27,607 @@
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
#else
#include "compat/getopt.h"
#endif
int ceildiv(int a, int b)
void usage_display(char *prgm)
{
return (a + b - 1) / b;
fprintf(stdout,"Usage:\n");
fprintf(stdout," %s...\n",prgm);
fprintf(stdout," -i <compressed file>\n");
fprintf(stdout," REQUIRED\n");
fprintf(stdout," Currently accepts J2K-files, JP2-files and JPT-files. The file type\n");
fprintf(stdout," is identified based on its suffix.\n");
fprintf(stdout," -o <decompressed file>\n");
fprintf(stdout," REQUIRED\n");
fprintf(stdout," Currently accepts PGM-files, PPM-files, PNM-files, PGX-files and\n");
fprintf(stdout," BMP-files. Binary data is written to the file (not ascii). If a PGX\n");
fprintf(stdout," filename is given, there will be as many output files as there are\n");
fprintf(stdout," components: an indice starting from 0 will then be appended to the\n");
fprintf(stdout," output filename, just before the \"pgx\" extension. If a PGM filename\n");
fprintf(stdout," is given and there are more than one component, only the first component\n");
fprintf(stdout," will be written to the file.\n");
fprintf(stdout," -r <reduce factor>\n");
fprintf(stdout," Set the number of highest resolution levels to be discarded. The\n");
fprintf(stdout," image resolution is effectively divided by 2 to the power of the\n");
fprintf(stdout," number of discarded levels. The reduce factor is limited by the\n");
fprintf(stdout," smallest total number of decomposition levels among tiles.\n");
fprintf(stdout," -l <number of quality layers to decode>\n");
fprintf(stdout," Set the maximum number of quality layers to decode. If there are\n");
fprintf(stdout," less quality layers than the specified number, all the quality layers\n");
fprintf(stdout," are decoded.\n");
fprintf(stdout," -u\n");
fprintf(stdout," print an usage statement\n");
fprintf(stdout,"\n");
}
int main(int argc, char **argv)
{
FILE *f;
char *src;
char *dest, S1, S2, S3;
FILE *fsrc=NULL;
FILE *fdest=NULL;
char *infile=NULL;
char *outfile=NULL;
char *tmp=NULL;
char S1, S2, S3;
char *src=NULL;
int len;
j2k_image_t *img;
j2k_cp_t *cp;
int w, h, max;
int i, image_type = -1;
if (argc < 3) {
fprintf(stderr, "usage: %s j2k-file pnm-file\n", argv[0]);
j2k_image_t img;
j2k_cp_t cp;
jp2_struct_t *jp2_struct=NULL;
int w, wr, wrr, h, hr, hrr, max;
int i, compno, pad, j;
int adjust;
cp.layer=0;
cp.reduce=0;
cp.decod_format=-1;
cp.cod_format=-1;
while (1) {
int c = getopt(argc, argv,"i:o:r:l:u");
if (c == -1)
break;
switch (c) {
//Input file
case 'i':
infile = optarg;
tmp = optarg;
while (*tmp) {
tmp++;
}
tmp--;
S3 = *tmp;
tmp--;
S2 = *tmp;
tmp--;
S1 = *tmp;
/* J2K format */
if ((S1 == 'j' && S2 == '2' && S3 == 'k')
|| (S1 == 'J' && S2 == '2' && S3 == 'K')
|| (S1 == 'j' && S2 == '2' && S3 == 'c')
|| (S1 == 'J' && S2 == '2' && S3 == 'C')) {
cp.cod_format=J2K_CFMT;
break;
}
/* JP2 format */
if ((S1 == 'j' && S2 == 'p' && S3 == '2')
|| (S1 == 'J' && S2 == 'P' && S3 == '2')) {
cp.cod_format=JP2_CFMT;
break;
}
/* JPT format */
if ((S1 == 'j' && S2 == 'p' && S3 == 't')
|| (S1 == 'J' && S2 == 'P' && S3 == 'T')) {
cp.cod_format=JPT_CFMT;
break;
}
fprintf(stderr,
"j2k_to_image : Unknown input image format *.%c%c%c [only *.j2k, *.jp2, *.jpc or *.jpt]!! \n",
S1, S2, S3);
return 1;
break;
/* ----------------------------------------------------- */
//Output file
case 'o':
outfile = optarg;
tmp = optarg;
while (*tmp) {
tmp++;
}
tmp--;
S3 = *tmp;
tmp--;
S2 = *tmp;
tmp--;
S1 = *tmp;
f = fopen(argv[1], "rb");
if (!f) {
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
return 1;
}
dest = argv[2];
while (*dest && *dest != '.') {
dest++;
}
dest++;
S1 = *dest;
dest++;
S2 = *dest;
dest++;
S3 = *dest;
// PGX format
if ((S1 == 'p' && S2 == 'g' && S3 == 'x')
|| (S1 == 'P' && S2 == 'G' && S3 == 'X')) {
image_type = 0;
cp.decod_format = PGX_DFMT;
break;
}
// PxM format
if ((S1 == 'p' && S2 == 'n' && S3 == 'm')
|| (S1 == 'P' && S2 == 'N' && S3 == 'M') || (S1 == 'p' && S2 == 'g'
&& S3 == 'm')
|| (S1 == 'P' && S2 == 'G' && S3 == 'M') || (S1 == 'P' && S2 == 'P'
&& S3 == 'M')
|| (S1 == 'P' && S2 == 'N' && S3 == 'M')
|| (S1 == 'p' && S2 == 'g' && S3 == 'm')
|| (S1 == 'P' && S2 == 'G' && S3 == 'M')
|| (S1 == 'P' && S2 == 'P' && S3 == 'M')
|| (S1 == 'p' && S2 == 'p' && S3 == 'm')) {
image_type = 1;
cp.decod_format = PXM_DFMT;
break;
}
// BMP format
if ((S1 == 'b' && S2 == 'm' && S3 == 'p')
|| (S1 == 'B' && S2 == 'M' && S3 == 'P')) {
image_type = 2;
cp.decod_format = BMP_DFMT;
break;
}
if (image_type == -1) {
// otherwise : error
fprintf(stderr,
"\033[0;33m!! Unrecognized format for infile [accept only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\033[0;39m\n\n");
"!! Unrecognized output image format *.%c%c%c [only *.pnm, *.pgm, *.ppm, *.pgx or *.bmp] !!\n",
S1, S2, S3);
return 1;
break;
/* ----------------------------------------------------- */
//Reduce option
case 'r':
tmp=optarg;
sscanf(tmp, "%d", &cp.reduce);
break;
/* ----------------------------------------------------- */
//Layering option
case 'l':
tmp=optarg;
sscanf(tmp, "%d", &cp.layer);
break;
/* ----------------------------------------------------- */
case 'u':
usage_display(argv[0]);
return 0;
break;
/* ----------------------------------------------------- */
default:
fprintf(stderr,"WARNING -> this option is not valid \"-%c %s\"\n",c,optarg);
break;
}
}
//Check required arguments
//------------------------
if (!infile || !outfile) {
fprintf(stderr,"ERROR -> At least one required argument is missing\nCheck j2k_to_image -u for usage information\n");
return 1;
}
fseek(f, 0, SEEK_END);
len = ftell(f);
fseek(f, 0, SEEK_SET);
//Read the input file and put it in memory
//----------------------------------------
fsrc = fopen(infile, "rb");
if (!fsrc) {
fprintf(stderr, "ERROR -> failed to open %s for reading\n", infile);
return 1;
}
fseek(fsrc, 0, SEEK_END);
len = ftell(fsrc);
fseek(fsrc, 0, SEEK_SET);
src = (char *) malloc(len);
fread(src, 1, len, f);
fclose(f);
fread(src, 1, len, fsrc);
fclose(fsrc);
//Decode the code-stream
//----------------------
switch(cp.cod_format) {
case J2K_CFMT:
if (!j2k_decode(src, len, &img, &cp)) {
fprintf(stderr, "j2k_to_image: failed to decode image!\n");
fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
return 1;
}
break;
case JP2_CFMT:
jp2_struct = (jp2_struct_t *) malloc(sizeof(jp2_struct_t));
jp2_struct->image = &img;
if (jp2_read_struct(src, jp2_struct, len)) {
fprintf(stderr, "ERROR -> j2k_to_image: failed to decode jp2 structure!\n");
return 1;
}
if (!j2k_decode(src + jp2_struct->j2k_codestream_offset, jp2_struct->j2k_codestream_len, &img, &cp)) {
fprintf(stderr, "ERROR -> j2k_to_image: failed to decode image!\n");
return 1;
}
/* Insert code here if you want to create actions on jp2_struct before deleting it */
free(jp2_struct);
break;
case JPT_CFMT:
if (!j2k_decode_jpt_stream(src, len, &img, &cp)) {
fprintf(stderr, "ERROR -> j2k_to_image: failed to decode JPT-file!\n");
return 1;
}
break;
default:
fprintf(stderr,
"ERROR -> j2k_to_image : Unknown input image format\n");
return 1;
break;
}
//Free the memory containing the code-stream
//------------------------------------------
free(src);
/* ------------------ CREATE OUT IMAGE WITH THE RIGHT FORMAT ----------------------- */
/* ------------------------ / */
/* / */
/* FORMAT : PNM, PGM or PPM / */
/* / */
/* ------------------------ / */
//Create output image
//-------------------
if (image_type == 1) { /* PNM PGM PPM */
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec) {
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
max = (1 << img->comps[0].prec) - 1;
fprintf(f, "P6\n%d %d\n%d\n", w, h, max);
for (i = 0; i < w * h; i++) {
char r, g, b;
r = img->comps[0].data[i];
g = img->comps[1].data[i];
b = img->comps[2].data[i];
fprintf(f, "%c%c%c", r, g, b);
/* ---------------------------- / */
/* / / */
/* / FORMAT : PNM, PGM or PPM / */
/* / / */
/* ---------------------------- / */
switch (cp.decod_format) {
case PXM_DFMT: /* PNM PGM PPM */
tmp=outfile;
while (*tmp) {
tmp++;
}
fclose(f);
tmp--;
tmp--;
S2 = *tmp;
if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
&& img.comps[1].dx == img.comps[2].dx
&& img.comps[0].dy == img.comps[1].dy
&& img.comps[1].dy == img.comps[2].dy
&& img.comps[0].prec == img.comps[1].prec
&& img.comps[1].prec == img.comps[2].prec
&& S2 !='g' && S2 !='G') {
fdest = fopen(outfile, "wb");
if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return 1;
}
w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
// wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[0].dx);
wr = img.comps[0].w;
wrr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
// hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
hr = img.comps[0].h;
hrr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
max = img.comps[0].prec > 8 ? 255 : (1 << img.comps[0].prec) - 1;
img.comps[0].x0 =
int_ceildivpow2(img.comps[0].x0 -
int_ceildiv(img.x0, img.comps[0].dx),
img.comps[0].factor);
img.comps[0].y0 =
int_ceildivpow2(img.comps[0].y0 -
int_ceildiv(img.y0, img.comps[0].dy),
img.comps[0].factor);
fprintf(fdest, "P6\n%d %d\n%d\n", wrr, hrr, max);
adjust = img.comps[0].prec > 8 ? img.comps[0].prec - 8 : 0;
for (i = 0; i < wrr * hrr; i++) {
int r, g, b;
unsigned char rc,gc,bc;
r = img.comps[0].data[i / wrr * wr + i % wrr];
r += (img.comps[0].sgnd ? 1 << (img.comps[0].prec - 1) : 0);
rc = (unsigned char) ((r >> adjust)+((r >> (adjust-1))%2));
g = img.comps[1].data[i / wrr * wr + i % wrr];
g += (img.comps[1].sgnd ? 1 << (img.comps[1].prec - 1) : 0);
gc = (unsigned char) ((g >> adjust)+((g >> (adjust-1))%2));
b = img.comps[2].data[i / wrr * wr + i % wrr];
b += (img.comps[2].sgnd ? 1 << (img.comps[2].prec - 1) : 0);
bc = (unsigned char) ((b >> adjust)+((b >> (adjust-1))%2));
fprintf(fdest, "%c%c%c", rc, gc, bc);
}
free(img.comps[0].data);
free(img.comps[1].data);
free(img.comps[2].data);
fclose(fdest);
} else {
int compno;
for (compno = 0; compno < img->numcomps; compno++) {
int ncomp=(S2=='g' || S2=='G')?1:img.numcomps;
if (img.numcomps>ncomp) {
fprintf(stderr,"WARNING -> [PGM files] Only the first component\n");
fprintf(stderr," is written to the file\n");
}
for (compno = 0; compno < ncomp; compno++) {
char name[256];
if (img->numcomps > 1) {
sprintf(name, "%d.%s", compno, argv[2]);
if (ncomp > 1) {
sprintf(name, "%d.%s", compno, outfile);
} else {
sprintf(name, "%s", argv[2]);
sprintf(name, "%s", outfile);
}
f = fopen(name, "wb");
fdest = fopen(name, "wb");
if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
return 1;
}
w = ceildiv(img->x1 - img->x0, img->comps[compno].dx);
h = ceildiv(img->y1 - img->y0, img->comps[compno].dy);
max = (1 << img->comps[compno].prec) - 1;
fprintf(f, "P5\n%d %d\n%d\n", w, h, max);
for (i = 0; i < w * h; i++) {
char l;
l = img->comps[compno].data[i];
fprintf(f, "%c", l);
w = int_ceildiv(img.x1 - img.x0, img.comps[compno].dx);
// wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor),img.comps[compno].dx);
wr = img.comps[compno].w;
wrr =
int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
h = int_ceildiv(img.y1 - img.y0, img.comps[compno].dy);
// hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[compno].dy);
hr = img.comps[compno].h;
hrr =
int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
max =
img.comps[compno].prec >
8 ? 255 : (1 << img.comps[compno].prec) - 1;
img.comps[compno].x0 =
int_ceildivpow2(img.comps[compno].x0 -
int_ceildiv(img.x0,
img.comps[compno].dx),
img.comps[compno].factor);
img.comps[compno].y0 =
int_ceildivpow2(img.comps[compno].y0 -
int_ceildiv(img.y0,
img.comps[compno].dy),
img.comps[compno].factor);
fprintf(fdest, "P5\n%d %d\n%d\n", wrr, hrr, max);
adjust =
img.comps[compno].prec > 8 ? img.comps[compno].prec - 8 : 0;
for (i = 0; i < wrr * hrr; i++) {
int l;
unsigned char lc;
l = img.comps[compno].data[i / wrr * wr + i % wrr];
l += (img.comps[compno].
sgnd ? 1 << (img.comps[compno].prec - 1) : 0);
lc = (unsigned char) ((l >> adjust)+((l >> (adjust-1))%2));
fprintf(fdest, "%c", lc);
}
fclose(f);
fclose(fdest);
free(img.comps[compno].data);
}
}
} else
break;
/* ------------------------ / */
/* / */
/* FORMAT : PGX / */
/* / */
/* ------------------------ / */
if (image_type == 0) { /* PGX */
int compno;
for (compno = 0; compno < img->numcomps; compno++) {
j2k_comp_t *comp = &img->comps[compno];
/* / / */
/* / FORMAT : PGX / */
/* / / */
/* /----------------------- / */
case PGX_DFMT: /* PGX */
for (compno = 0; compno < img.numcomps; compno++) {
j2k_comp_t *comp = &img.comps[compno];
char name[256];
/* sprintf(name, "%s-%d.pgx", argv[2], compno); */
sprintf(name, "%s", argv[2]);
f = fopen(name, "wb");
w = ceildiv(img->x1 - img->x0, comp->dx);
h = ceildiv(img->y1 - img->y0, comp->dy);
fprintf(f, "PG LM %c %d %d %d\n", comp->sgnd ? '-' : '+', comp->prec,
w, h);
for (i = 0; i < w * h; i++) {
int v = img->comps[compno].data[i];
if (comp->prec <= 8) {
char c = (char) v;
fwrite(&c, 1, 1, f);
} else if (comp->prec <= 16) {
short s = (short) v;
fwrite(&s, 2, 1, f);
} else {
fwrite(&v, 4, 1, f);
int nbytes = 0;
tmp = outfile;
while (*tmp) {
tmp++;
}
while (*tmp!='.') {
tmp--;
}
fclose(f);
}
} else
/* ------------------------ / */
/* / */
/* FORMAT : BMP / */
/* / */
/* ------------------------ / */
*tmp='\0';
if (img.numcomps > 1)
sprintf(name, "%s-%d.pgx", outfile, compno);
else
sprintf(name, "%s.pgx", outfile);
if (image_type == 2) { /* BMP */
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec) {
fdest = fopen(name, "wb");
if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", name);
return 1;
}
// w = int_ceildiv(img.x1 - img.x0, comp->dx);
// wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), comp->dx);
w = img.comps[compno].w;
wr = int_ceildivpow2(img.comps[compno].w, img.comps[compno].factor);
// h = int_ceildiv(img.y1 - img.y0, comp->dy);
// hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), comp->dy);
h = img.comps[compno].h;
hr = int_ceildivpow2(img.comps[compno].h, img.comps[compno].factor);
fprintf(fdest, "PG ML %c %d %d %d\n", comp->sgnd ? '-' : '+',
comp->prec, wr, hr);
if (comp->prec <= 8)
nbytes = 1;
else if (comp->prec <= 16)
nbytes = 2;
else
nbytes = 4;
for (i = 0; i < wr * hr; i++) {
int v = img.comps[compno].data[i / wr * w + i % wr];
for (j = nbytes - 1; j >= 0; j--) {
char byte = (char) (v >> (j * 8));
fwrite(&byte, 1, 1, fdest);
}
}
free(img.comps[compno].data);
fclose(fdest);
}
break;
/* ------------------------ / */
/* / / */
/* / FORMAT : BMP / */
/* / / */
/* /----------------------- / */
case BMP_DFMT: /* BMP */
if (img.numcomps == 3 && img.comps[0].dx == img.comps[1].dx
&& img.comps[1].dx == img.comps[2].dx
&& img.comps[0].dy == img.comps[1].dy
&& img.comps[1].dy == img.comps[2].dy
&& img.comps[0].prec == img.comps[1].prec
&& img.comps[1].prec == img.comps[2].prec) {
/* -->> -->> -->> -->>
24 bits color
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fdest = fopen(outfile, "wb");
if (!fdest) {
fprintf(stderr, "ERROR -> failed to open %s for writing\n", outfile);
return 1;
}
fprintf(f, "BM");
// w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
// wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
w = img.comps[0].w;
wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
// h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
// hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
h = img.comps[0].h;
hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
fprintf(fdest, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w * 3 + 3 * h * (w % 2) + 54) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 8) &
0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 16) &
0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2) + 54) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c",
(unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
54) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 8) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 16) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
(unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff,
(unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
(unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff,
(unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
(unsigned char) ((wr) >> 8) & 0xff,
(unsigned char) ((wr) >> 16) & 0xff,
(unsigned char) ((wr) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
(unsigned char) ((hr) >> 8) & 0xff,
(unsigned char) ((hr) >> 16) & 0xff,
(unsigned char) ((hr) >> 24) & 0xff);
fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(fdest, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c",
(unsigned char) (3 * h * w + 3 * h * (w % 2)) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w * 3 + 3 * h * (w % 2)) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c",
(unsigned char) (3 * hr * wr +
3 * hr * (wr % 2)) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
8) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
16) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
for (i = 0; i < w * h; i++) {
for (i = 0; i < wr * hr; i++) {
unsigned char R, G, B;
/* a modifier */
// R = img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
R = img.comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
// G = img.comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
G = img.comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
// B = img.comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
B = img.comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
fprintf(fdest, "%c%c%c", B, G, R);
R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
fprintf(f, "%c%c%c", B, G, R);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff);
if ((i + 1) % wr == 0) {
for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
fclose(f);
}
fclose(fdest);
free(img.comps[1].data);
free(img.comps[2].data);
} else { /* Gray-scale */
/* -->> -->> -->> -->>
@@ -284,67 +635,112 @@ int main(int argc, char **argv)
8 bits non code (Gray scale)
<<-- <<-- <<-- <<-- */
f = fopen(argv[2], "wb");
w = ceildiv(img->x1 - img->x0, img->comps[0].dx);
h = ceildiv(img->y1 - img->y0, img->comps[0].dy);
fdest = fopen(outfile, "wb");
// w = int_ceildiv(img.x1 - img.x0, img.comps[0].dx);
// wr = int_ceildiv(int_ceildivpow2(img.x1 - img.x0,img.factor), img.comps[0].dx);
w = img.comps[0].w;
wr = int_ceildivpow2(img.comps[0].w, img.comps[0].factor);
fprintf(f, "BM");
// h = int_ceildiv(img.y1 - img.y0, img.comps[0].dy);
// hr = int_ceildiv(int_ceildivpow2(img.y1 - img.y0,img.factor), img.comps[0].dy);
h = img.comps[0].h;
hr = int_ceildivpow2(img.comps[0].h, img.comps[0].factor);
fprintf(fdest, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (h * w + 54 + 1024 + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 8) &
0xff,
(unsigned char) ((h * w + 54 + 1024 + h * (w % 2)) >> 16) &
0xff,
(unsigned char) ((h * w + 54 + 1024 + w * (w % 2)) >> 24) &
0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c",
(unsigned char) (hr * wr + 54 + 1024 +
hr * (wr % 2)) & 0xff,
(unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
>> 8) & 0xff,
(unsigned char) ((hr * wr + 54 + 1024 + hr * (wr % 2))
>> 16) & 0xff,
(unsigned char) ((hr * wr + 54 + 1024 + wr * (wr % 2))
>> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54 + 1024) & 0xff, ((54 + 1024) >> 8) & 0xff,
((54 + 1024) >> 16) & 0xff, ((54 + 1024) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (54 + 1024) & 0xff,
((54 + 1024) >> 8) & 0xff, ((54 + 1024) >> 16) & 0xff,
((54 + 1024) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((w) & 0xff),
(unsigned char) ((w) >> 8) & 0xff,
(unsigned char) ((w) >> 16) & 0xff,
(unsigned char) ((w) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((h) & 0xff),
(unsigned char) ((h) >> 8) & 0xff,
(unsigned char) ((h) >> 16) & 0xff,
(unsigned char) ((h) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
(unsigned char) ((wr) >> 8) & 0xff,
(unsigned char) ((wr) >> 16) & 0xff,
(unsigned char) ((wr) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
(unsigned char) ((hr) >> 8) & 0xff,
(unsigned char) ((hr) >> 16) & 0xff,
(unsigned char) ((hr) >> 24) & 0xff);
fprintf(fdest, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(fdest, "%c%c", (8) & 0xff, ((8) >> 8) & 0xff);
fprintf(fdest, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) (h * w + h * (w % 2)) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 8) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 16) & 0xff,
(unsigned char) ((h * w + h * (w % 2)) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c",
(unsigned char) (hr * wr + hr * (wr % 2)) & 0xff,
(unsigned char) ((hr * wr + hr * (wr % 2)) >> 8) &
0xff,
(unsigned char) ((hr * wr + hr * (wr % 2)) >> 16) &
0xff,
(unsigned char) ((hr * wr + hr * (wr % 2)) >> 24) & 0xff);
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
fprintf(fdest, "%c%c%c%c", (256) & 0xff, ((256) >> 8) & 0xff,
((256) >> 16) & 0xff, ((256) >> 24) & 0xff);
}
for (i = 0; i < 256; i++) {
fprintf(f, "%c%c%c%c", i, i, i, 0);
fprintf(fdest, "%c%c%c%c", i, i, i, 0);
}
for (i = 0; i < w * h; i++) {
fprintf(f, "%c",
img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
if (((i + 1) % w == 0 && w % 2))
fprintf(f, "%c", 0);
for (i = 0; i < wr * hr; i++) {
/* a modifier !! */
// fprintf(fdest, "%c", img.comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)]);
fprintf(fdest, "%c",
img.comps[0].data[w * hr - ((i) / (wr) + 1) * w +
(i) % (wr)]);
/*if (((i + 1) % w == 0 && w % 2))
fprintf(fdest, "%c", 0); */
if ((i + 1) % wr == 0) {
for (pad = wr % 4 ? 4 - wr % 4 : 0; pad > 0; pad--) /* ADD */
fprintf(fdest, "%c", 0);
}
}
fclose(fdest);
free(img.comps[0].data);
break;
default:
fprintf(stderr,
"ERROR -> j2k_to_image : Unknown output image format\n");
return 1;
break;
}
}
// Free remaining structures
//--------------------------
j2k_dec_release();
// Check memory leaks if debug mode
//---------------------------------
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
return 0;
}

240
codec/j2k_to_image.dsp Normal file
View File

@@ -0,0 +1,240 @@
# Microsoft Developer Studio Project File - Name="j2k_to_image" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=j2k_to_image - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "j2k_to_image.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "j2k_to_image.mak" CFG="j2k_to_image - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "j2k_to_image - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "j2k_to_image - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "j2k_to_image - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "NDEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD BASE RSC /l 0x80c /d "NDEBUG"
# ADD RSC /l 0x80c /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "j2k_to_image - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "j2k_to_image___Win32_Debug"
# PROP BASE Intermediate_Dir "j2k_to_image___Win32_Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "j2k_to_image___Win32_Debug"
# PROP Intermediate_Dir "j2k_to_image___Win32_Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "_DEBUG" /D "WIN32" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /GZ /c
# ADD BASE RSC /l 0x80c /d "_DEBUG"
# ADD RSC /l 0x80c /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "j2k_to_image - Win32 Release"
# Name "j2k_to_image - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.c
# End Source File
# Begin Source File
SOURCE=.\compat\getopt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.c
# End Source File
# Begin Source File
SOURCE=.\j2k_to_image.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

View File

@@ -18,15 +18,23 @@ ifdef MINGW32
AR = i386-mingw32-ar
OBJ_DIR = $(OBJ_DIR_W32)
LIB_DIR = $(LIB_DIR_W32)
all: $(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.dll
all: $(OBJ_DIR) $(LIB_DIR) \
$(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.dll
else
CC = gcc
AR = ar
OBJ_DIR = obj
LIB_DIR = lib
all: $(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.so
all: $(OBJ_DIR) $(LIB_DIR) \
$(LIB_DIR)/libopenjpeg.a $(LIB_DIR)/libopenjpeg.so
endif
$(OBJ_DIR):
mkdir $(OBJ_DIR)
$(LIB_DIR):
mkdir $(LIB_DIR)
$(OBJ_DIR)/%.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
@@ -38,15 +46,18 @@ $(OBJ_DIR)/int.o: int.c
$(OBJ_DIR)/j2k.o: j2k.c j2k.h cio.h tcd.h dwt.h int.h
$(OBJ_DIR)/mct.o: mct.c mct.h fix.h
$(OBJ_DIR)/mqc.o: mqc.c mqc.h
$(OBJ_DIR)/pi.o: pi.c pi.h int.h
$(OBJ_DIR)/raw.o: raw.c raw.h
$(OBJ_DIR)/t1.o: t1.c t1.h j2k.h mqc.h raw.h int.h mct.h dwt.h fix.h
$(OBJ_DIR)/t2.o: t2.c t2.h tcd.h bio.h j2k.h pi.h tgt.h int.h cio.h
$(OBJ_DIR)/tcd.o: tcd.c tcd.h int.h t1.h t2.h dwt.h mct.h
$(OBJ_DIR)/tgt.o: tgt.c tgt.h bio.h
$(OBJ_DIR)/jpt.o: jpt.c jpt.h cio.h
$(OBJ_DIR)/jp2.o: jp2.c jp2.h
COM_OBJS = $(addprefix $(OBJ_DIR)/, j2k.o bio.o cio.o dwt.o fix.o int.o mct.o \
mqc.o pi.o t1.o t2.o tgt.o tcd.o raw.o)
mqc.o pi.o t1.o t2.o tgt.o tcd.o raw.o jpt.o jp2.o)
$(LIB_DIR)/libopenjpeg.a: ${COM_OBJS}
$(AR) -sr $@ $^

View File

@@ -25,31 +25,34 @@
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
***/
#include "bio.h"
#include <stdio.h>
#include <setjmp.h>
static unsigned char *bio_start, *bio_end, *bio_bp;
static unsigned int bio_buf;
static int bio_ct;
static unsigned char *bio_start; /* pointer to the start of the buffer */
static unsigned char *bio_end; /* pointer to the end of the buffer */
static unsigned char *bio_bp; /* pointer to the present position in the buffer */
static unsigned int bio_buf; /* temporary place where each byte is read or written */
static int bio_ct; /* coder : number of bits free to write // decoder : number of bits read */
extern jmp_buf j2k_error;
/* <summary> */
/* Number of bytes written. */
/* </summary> */
/*
* Number of bytes written.
*/
int bio_numbytes()
{
return bio_bp - bio_start;
}
/* <summary> */
/* Init encoder. */
/* </summary> */
/* <param name="bp">Output buffer</param> */
/* <param name="len">Output buffer length</param> */
/*
* Init encoder.
*
* bp : Output buffer
* len : Output buffer length
*/
void bio_init_enc(unsigned char *bp, int len)
{
bio_start = bp;
@@ -59,11 +62,12 @@ void bio_init_enc(unsigned char *bp, int len)
bio_ct = 8;
}
/* <summary> */
/* Init decoder. */
/* </summary> */
/* <param name="bp">Input buffer</param> */
/* <param name="len">Input buffer length</param> */
/*
* Init decoder.
*
* bp : Input buffer
* len : Input buffer length
*/
void bio_init_dec(unsigned char *bp, int len)
{
bio_start = bp;
@@ -73,9 +77,10 @@ void bio_init_dec(unsigned char *bp, int len)
bio_ct = 0;
}
/* <summary> */
/* Write byte. --> function modified to eliminate longjmp !!! */
/* </summary> */
/*
* Write byte. --> function modified to eliminate longjmp !!!
*
*/
int bio_byteout()
{
bio_buf = (bio_buf << 8) & 0xffff;
@@ -86,9 +91,10 @@ int bio_byteout()
return 0;
}
/* <summary> */
/* Read byte. --> function modified to eliminate longjmp !! */
/* </summary> */
/*
* Read byte. --> function modified to eliminate longjmp !!
*
*/
int bio_bytein()
{
bio_buf = (bio_buf << 8) & 0xffff;
@@ -99,10 +105,11 @@ int bio_bytein()
return 0;
}
/* <summary> */
/* Write bit. */
/* </summary> */
/* <param name="b">Bit to write (0 or 1)</param> */
/*
* Write bit.
*
* b : Bit to write (0 or 1)
*/
void bio_putbit(int b)
{
if (bio_ct == 0) {
@@ -112,9 +119,10 @@ void bio_putbit(int b)
bio_buf |= b << bio_ct;
}
/* <summary> */
/* Read bit. */
/* </summary> */
/*
* Read bit.
*
*/
int bio_getbit()
{
if (bio_ct == 0) {
@@ -124,11 +132,12 @@ int bio_getbit()
return (bio_buf >> bio_ct) & 1;
}
/* <summary> */
/* Write bits. */
/* </summary> */
/* <param name="v">Value of bits</param> */
/* <param name="n">Number of bits to write</param> */
/*
* Write bits.
*
* v : Value of bits
* n : Number of bits to write
*/
void bio_write(int v, int n)
{
int i;
@@ -137,10 +146,11 @@ void bio_write(int v, int n)
}
}
/* <summary> */
/* Read bits. */
/* </summary> */
/* <param name="n">Number of bits to read</param> */
/*
* Read bits.
*
* n : Number of bits to read
*/
int bio_read(int n)
{
int i, v;
@@ -151,9 +161,10 @@ int bio_read(int n)
return v;
}
/* <summary> */
/* Flush bits. MOdified to eliminate longjmp !! */
/* </summary> */
/*
* Flush bits. Modified to eliminate longjmp !!
*
*/
int bio_flush()
{
bio_ct = 0;
@@ -168,8 +179,9 @@ int bio_flush()
return 0;
}
/* <summary> */
/* </summary> */
/*
* Passes the ending bits (coming from flushing)
*/
int bio_inalign()
{
bio_ct = 0;

View File

@@ -29,12 +29,47 @@
#ifndef __BIO_H
#define __BIO_H
/*
* Number of bytes written.
*/
int bio_numbytes();
/*
* Init encoder.
*
* bp : Output buffer
* len : Output buffer length
*/
void bio_init_enc(unsigned char *bp, int len);
/*
* Init decoder.
*
* bp : Input buffer
* len : Input buffer length
*/
void bio_init_dec(unsigned char *bp, int len);
/*
* Write bits.
*
* v : Value of bits
* n : Number of bits to write
*/
void bio_write(int v, int n);
/*
* Read bits.
*
* n : Number of bits to read
*/
int bio_read(int n);
int bio_flush(); /* modified to eliminated longjmp !! */
/*
* Flush bits. Modified to eliminate longjmp !!
*/
int bio_flush();
int bio_inalign(); /* modified to eliminated longjmp !! */
#endif

View File

@@ -26,54 +26,62 @@
#include "cio.h"
#include <setjmp.h>
#include <memory.h>
static unsigned char *cio_start, *cio_end, *cio_bp;
static unsigned char *cio_start; /* pointer to the start of the stream */
static unsigned char *cio_end; /* pointer to the end of the stream */
static unsigned char *cio_bp; /* pointer to the present position */
extern jmp_buf j2k_error;
/* <summary> */
/* Number of bytes written. */
/* </summary> */
/*
* Number of bytes written.
*/
int cio_numbytes()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Get position in byte stream. */
/* </summary> */
/*
* Get position in byte stream.
*/
int cio_tell()
{
return cio_bp - cio_start;
}
/* <summary> */
/* Set position in byte stream. */
/* </summary> */
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos)
{
cio_bp = cio_start + pos;
}
/* <summary> */
/* Number of bytes left before the end of the stream. */
/* </summary> */
/*
* Number of bytes left before the end of the stream.
*/
int cio_numbytesleft()
{
return cio_end - cio_bp;
}
/* <summary> */
/* Get pointer to the current position in the stream. */
/* </summary> */
/*
* Get pointer to the current position in the stream.
*/
unsigned char *cio_getbp()
{
return cio_bp;
}
/* <summary> */
/* Initialize byte IO. */
/* </summary> */
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len)
{
cio_start = bp;
@@ -81,9 +89,9 @@ void cio_init(unsigned char *bp, int len)
cio_bp = bp;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
/*
* Write a byte.
*/
void cio_byteout(unsigned char v)
{
if (cio_bp >= cio_end)
@@ -92,9 +100,9 @@ void cio_byteout(unsigned char v)
}
/* <summary> */
/* Read a byte. */
/* </summary> */
/*
* Read a byte.
*/
unsigned char cio_bytein()
{
if (cio_bp >= cio_end)
@@ -102,9 +110,12 @@ unsigned char cio_bytein()
return *cio_bp++;
}
/* <summary> */
/* Write a byte. */
/* </summary> */
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n)
{
int i;
@@ -113,9 +124,13 @@ void cio_write(unsigned int v, int n)
}
}
/* <summary> */
/* Read some bytes. */
/* </summary> */
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n)
{
int i;
@@ -127,10 +142,38 @@ unsigned int cio_read(int n)
return v;
}
/* <summary> */
/* Write some bytes. */
/* </summary> */
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n)
{
cio_bp += n;
}
/*
* Read n bytes, copy to buffer
*
* n : number of bytes to transfer
*/
void cio_read_to_buf(unsigned char* src_buf, int n)/* Glenn adds */
{
if (cio_bp + n > cio_end)
longjmp(j2k_error, 1);
memcpy(cio_bp, src_buf, n);
cio_bp += n;
}
/*
* Write n bytes, copy from buffer
*
* n : number of bytes to transfer
*/
void cio_write_from_buf(unsigned char* dest_buf, int n)/* Glenn adds */
{
if (cio_bp + n > cio_end)
longjmp(j2k_error, 1);
memcpy(dest_buf, cio_bp, n);
cio_bp += n;
}

View File

@@ -27,14 +27,81 @@
#ifndef __CIO_H
#define __CIO_H
int cio_tell();
void cio_seek(int pos);
/*
* Number of bytes written.
*
* returns number of bytes written
*/
int cio_numbytes();
/*
* Get position in byte stream.
*
* return position in bytes
*/
int cio_tell();
/*
* Set position in byte stream.
*
* pos : position, in number of bytes, from the beginning of the stream
*/
void cio_seek(int pos);
/*
* Number of bytes left before the end of the stream.
*
* Returns the number of bytes before the end of the stream
*/
int cio_numbytesleft();
/*
* Get pointer to the current position in the stream.
*
* return : pointer to the position
*/
unsigned char *cio_getbp();
/*
* Initialize byte IO
*
* bp : destination/source stream
* len : length of the stream
*/
void cio_init(unsigned char *bp, int len);
/*
* Write some bytes.
*
* v : value to write
* n : number of bytes to write
*/
void cio_write(unsigned int v, int n);
/*
* Read some bytes.
*
* n : number of bytes to read
*
* return : value of the n bytes read
*/
unsigned int cio_read(int n);
/*
* Skip some bytes.
*
* n : number of bytes to skip
*/
void cio_skip(int n);
/*
* Read n bytes, copy to buffer
*/
void cio_read_to_buf(unsigned char* src_buf, int n);/* Glenn Pearson adds */
/*
* Write n bytes, copy from buffer
*/
void cio_write_from_buf(unsigned char* dest_buf, int n);/* Glenn Pearson adds */
#endif

View File

@@ -1,7 +1,8 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2005, Reiner Wahler
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,15 +27,37 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* NOTE:
* This is a modified version of the openjpeg dwt.c file.
* Average speed improvement compared to the original file (measured on
* my own machine, a P4 running at 3.0 GHz):
* 5x3 wavelets about 2 times faster
* 9x7 wavelets about 3 times faster
* for both, encoding and decoding.
*
* The better performance is caused by doing the 1-dimensional DWT
* within a temporary buffer where the data can be accessed sequential
* for both directions, horizontal and vertical. The 2d vertical DWT was
* the major bottleneck in the former version.
*
* I have also removed the "Add Patrick" part because it is not longer
* needed.
*
* 6/6/2005
* -Ive (aka Reiner Wahler)
* mail: ive@lilysoft.com
*/
#include "dwt.h"
#include "int.h"
#include "fix.h"
#include "tcd.h"
#include <stdlib.h>
#include <stdio.h>
#define S(i) a[x*(i)*2]
#define D(i) a[x*(1+(i)*2)]
#define S(i) a[(i)*2]
#define D(i) a[(1+(i)*2)]
#define S_(i) ((i)<0?S(0):((i)>=sn?S(sn-1):S(i)))
#define D_(i) ((i)<0?D(0):((i)>=dn?D(dn-1):D(i)))
/* new */
@@ -61,357 +84,229 @@ double dwt_norms_real[4][10] = {
{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};
/* Add Patrick */
static int *b = NULL;
static int lastSizeOfB = 0;
void dwt_clean()
{
if (b != NULL) {
free(b);
}
b = NULL;
lastSizeOfB = 0;
}
/* \ Add Patrick */
/* <summary> */
/* Forward lazy transform. */
/* Forward lazy transform (horizontal). */
/* </summary> */
void dwt_deinterleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[i] = a[(2 * i + 1) * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[2 * i * x];
} else {
for (i = 0; i < sn; i++)
b[i] = a[2 * i * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[(2 * i + 1) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
void dwt_deinterleave_h(int *a, int *b, int dn, int sn, int cas) {
int i;
for (i=0; i<sn; i++) b[i]=a[2*i+cas];
for (i=0; i<dn; i++) b[sn+i]=a[(2*i+1-cas)];
}
/* <summary> */
/* Forward lazy transform. */
/* Forward lazy transform (vertical). */
/* </summary> */
void dwt_deinterleave_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
}
if (cas) {
for (i = 0; i < sn; i++)
b[i] = a[(2 * i + 1) * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[2 * i * x];
} else {
for (i = 0; i < sn; i++)
b[i] = a[2 * i * x];
for (i = 0; i < dn; i++)
b[sn + i] = a[(2 * i + 1) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
void dwt_deinterleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
int i;
for (i=0; i<sn; i++) b[i*x]=a[2*i+cas];
for (i=0; i<dn; i++) b[(sn+i)*x]=a[(2*i+1-cas)];
}
/* <summary> */
/* Inverse lazy transform. */
/* Inverse lazy transform (horizontal). */
/* </summary> */
void dwt_interleave(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
void dwt_interleave_h(int *a, int *b, int dn, int sn, int cas) {
int i;
/* for (i=0; i<sn; i++) b[2*i+cas]=a[i];*/
/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)];*/
int* ai;
int* bi;
ai=a;
bi=b+cas;
for (i=0; i<sn; i++) {
*bi = *ai; bi+=2; ai++;
}
if (cas) {
for (i = 0; i < sn; i++)
b[2 * i + 1] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i] = a[(sn + i) * x];
} else {
for (i = 0; i < sn; i++)
b[2 * i] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i + 1] = a[(sn + i) * x];
ai=a+sn;
bi=b+1-cas;
for (i=0; i<dn; i++) {
*bi = *ai; bi+=2; ai++;
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Inverse lazy transform. */
/* Inverse lazy transform (vertical). */
/* </summary> */
void dwt_interleave_real(int *a, int n, int x, int res, int cas)
{
int dn, sn, i;
sn = res;
dn = n - res;
if (lastSizeOfB != n) {
if (b != NULL)
free(b);
b = (int *) malloc(n * sizeof(int));
lastSizeOfB = n;
void dwt_interleave_v(int *a, int *b, int dn, int sn, int x, int cas) {
int i;
/* for (i=0; i<sn; i++) b[2*i+cas]=a[i*x];*/
/* for (i=0; i<dn; i++) b[2*i+1-cas]=a[(sn+i)*x];*/
int* ai;
int* bi;
ai=a;
bi=b+cas;
for (i=0; i<sn; i++) {
*bi = *ai; bi+=2; ai+=x;
}
ai=a+(sn*x);
bi=b+1-cas;
for (i=0; i<dn; i++) {
*bi = *ai; bi+=2; ai+=x;
}
}
if (cas) {
for (i = 0; i < sn; i++)
b[2 * i + 1] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i] = a[(sn + i) * x];
} else {
for (i = 0; i < sn; i++)
b[2 * i] = a[i * x];
for (i = 0; i < dn; i++)
b[2 * i + 1] = a[(sn + i) * x];
}
for (i = 0; i < n; i++)
a[i * x] = b[i];
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_encode_1(int *a, int n, int x, int res, int cas)
void dwt_encode_1(int *a, int dn, int sn, int cas)
{
int dn, sn, i = 0;
sn = res;
dn = n - res;
int i;
if (cas) {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) *= 2;
else {
for (i = 0; i < dn; i++)
S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
for (i = 0; i < sn; i++)
D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++) D(i) -= (S_(i) + S_(i + 1)) >> 1;
for (i = 0; i < sn; i++) S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
D(i) -= (S_(i) + S_(i + 1)) >> 1;
for (i = 0; i < sn; i++)
S(i) += (D_(i - 1) + D_(i) + 2) >> 2;
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(0) *= 2;
else {
for (i = 0; i < dn; i++) S(i) -= (DD_(i) + DD_(i - 1)) >> 1;
for (i = 0; i < sn; i++) D(i) += (SS_(i) + SS_(i + 1) + 2) >> 2;
}
}
dwt_deinterleave(a, n, x, res, cas);
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 1-D. */
/* </summary> */
void dwt_decode_1(int *a, int n, int x, int res, int cas)
void dwt_decode_1(int *a, int dn, int sn, int cas)
{
int dn, sn, i = 0;
/* dn=n/2; */
/* sn=(n+1)/2; */
sn = res;
dn = n - res;
int i;
dwt_interleave(a, n, x, res, cas);
if (cas) {
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(i) /= 2;
else {
for (i = 0; i < sn; i++)
D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
for (i = 0; i < dn; i++)
S(i) += (DD_(i) + DD_(i - 1)) >> 1;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++) S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
for (i = 0; i < dn; i++) D(i) += (S_(i) + S_(i + 1)) >> 1;
}
} else {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) -= (D_(i - 1) + D_(i) + 2) >> 2;
for (i = 0; i < dn; i++)
D(i) += (S_(i) + S_(i + 1)) >> 1;
if (!sn && dn == 1) /* NEW : CASE ONE ELEMENT */
S(0) /= 2;
else {
for (i = 0; i < sn; i++) D(i) -= (SS_(i) + SS_(i + 1) + 2) >> 2;
for (i = 0; i < dn; i++) S(i) += (DD_(i) + DD_(i - 1)) >> 1;
}
}
}
/* <summary> */
/* Forward 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
void dwt_encode(tcd_tilecomp_t * tilec)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
/* Check the different cases for that it's necessary to invert high pass and low pass filter */
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
/* inversion on the previous and propagation of the inversion on the other tile */
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j=0; j<rw; j++) {
aj=a+j;
for (k=0; k<rh; k++) bj[k]=aj[k*w];
dwt_encode_1(bj, dn, sn, cas_col);
dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
}
free(bj);
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j=0; j<rh; j++) {
aj=a+j*w;
for (k=0; k<rw; k++) bj[k]=aj[k];
dwt_encode_1(bj, dn, sn, cas_row);
dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
}
free(bj);
}
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
/* inversion on the previous and propagation of the inversion on the other tile */
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
/* subband LL shorter than LH or HL */
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rw; j++)
dwt_encode_1(a + j, rh, w, rh1, cas_col);
for (j = 0; j < rh; j++)
dwt_encode_1(a + j * w, rw, 1, rw1, cas_row);
}
dwt_clean();
}
/* <summary> */
/* Inverse 5-3 wavelet tranform in 2-D. */
/* </summary> */
void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec)
void dwt_decode(tcd_tilecomp_t * tilec, int stop)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l - 1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (row_tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j = 0; j < rh; j++) {
aj = a+j*w;
dwt_interleave_h(aj, bj, dn, sn, cas_row);
dwt_decode_1(bj, dn, sn, cas_row);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
free(bj);
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j = 0; j < rw; j++) {
aj = a+j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_col);
dwt_decode_1(bj, dn, sn, cas_col);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
free(bj);
}
}
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (col_tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rh; j++)
dwt_decode_1(a + j * w, rw, 1, rw1, cas_row);
for (j = 0; j < rw; j++)
dwt_decode_1(a + j, rh, w, rh1, cas_col);
}
dwt_clean();
}
/* <summary> */
/* Get gain of 5-3 wavelet transform. */
@@ -436,28 +331,10 @@ double dwt_getnorm(int level, int orient)
/* <summary> */
/* Forward 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
void dwt_encode_1_real(int *a, int dn, int sn, int cas)
{
int dn, sn, i = 0;
dn = n - res;
sn = res;
if (cas) {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 5038);
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 6660);
}
} else {
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
D(i) -= fix_mul(S_(i) + S_(i + 1), 12993);
@@ -468,44 +345,40 @@ void dwt_encode_1_real(int *a, int n, int x, int res, int cas)
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 5038);
D(i) = fix_mul(D(i), 5038); /*5038 */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 6660);
S(i) = fix_mul(S(i), 6659); /*6660 */
}
} else {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 12993);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 5038); /*5038 */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 6659); /*6660 */
}
}
dwt_deinterleave_real(a, n, x, res, cas);
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 1-D. */
/* </summary> */
void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
void dwt_decode_1_real(int *a, int dn, int sn, int cas)
{
int dn, sn, i = 0;
dn = n - res;
sn = res;
dwt_interleave_real(a, n, x, res, cas);
if (cas) {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 10076);
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 13320);
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 12993);
}
} else {
int i;
if (!cas) {
if ((dn > 0) || (sn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
S(i) = fix_mul(S(i), 10076);
S(i) = fix_mul(S(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
D(i) = fix_mul(D(i), 13320);
D(i) = fix_mul(D(i), 13318); /* 13320 */
for (i = 0; i < sn; i++)
S(i) -= fix_mul(D_(i - 1) + D_(i), 3633);
for (i = 0; i < dn; i++)
@@ -513,7 +386,22 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
for (i = 0; i < sn; i++)
S(i) += fix_mul(D_(i - 1) + D_(i), 434);
for (i = 0; i < dn; i++)
D(i) += fix_mul(S_(i) + S_(i + 1), 12993);
D(i) += fix_mul(S_(i) + S_(i + 1), 12994); /* 12993 */
}
} else {
if ((sn > 0) || (dn > 1)) { /* NEW : CASE ONE ELEMENT */
for (i = 0; i < sn; i++)
D(i) = fix_mul(D(i), 10078); /* 10076 */
for (i = 0; i < dn; i++)
S(i) = fix_mul(S(i), 13318); /* 13320 */
for (i = 0; i < sn; i++)
D(i) -= fix_mul(SS_(i) + SS_(i + 1), 3633);
for (i = 0; i < dn; i++)
S(i) -= fix_mul(DD_(i) + DD_(i - 1), 7233);
for (i = 0; i < sn; i++)
D(i) += fix_mul(SS_(i) + SS_(i + 1), 434);
for (i = 0; i < dn; i++)
S(i) += fix_mul(DD_(i) + DD_(i - 1), 12994); /* 12993 */
}
}
}
@@ -522,153 +410,124 @@ void dwt_decode_1_real(int *a, int n, int x, int res, int cas)
/* Forward 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l)
void dwt_encode_real(tcd_tilecomp_t * tilec)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = 0; i < l; i++) {
int cas_col = 0;
int cas_row = 0;
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
cas_row = tilec->resolutions[l - i].x0 % 2;
cas_col = tilec->resolutions[l - i].y0 % 2;
sn = rh1;
dn = rh - rh1;
bj=(int*)malloc(rh*sizeof(int));
for (j = 0; j < rw; j++) {
aj = a + j;
for (k = 0; k < rh; k++) bj[k] = aj[k*w];
dwt_encode_1_real(bj, dn, sn, cas_col);
dwt_deinterleave_v(bj, aj, dn, sn, w, cas_col);
}
free(bj);
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
sn = rw1;
dn = rw - rw1;
bj=(int*)malloc(rw*sizeof(int));
for (j = 0; j < rh; j++) {
aj = a + j * w;
for (k = 0; k < rw; k++) bj[k] = aj[k];
dwt_encode_1_real(bj, dn, sn, cas_row);
dwt_deinterleave_h(bj, aj, dn, sn, cas_row);
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rw; j++)
dwt_encode_1_real(a + j, rh, w, rh1, cas_col);
for (j = 0; j < rh; j++)
dwt_encode_1_real(a + j * w, rw, 1, rw1, cas_row);
free(bj);
}
}
/* <summary> */
/* Inverse 9-7 wavelet transform in 2-D. */
/* </summary> */
void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec,
tcd_tilecomp_t * col_tilec)
void dwt_decode_real(tcd_tilecomp_t * tilec, int stop)
{
int i, j, rw, rh, rw1, rh1;
int previous_rw, previous_rh, previous_rw1, previous_rh1;
for (i = l - 1; i >= 0; i--) {
int cas_col = 0;
int cas_row = 0;
int i, j, k;
int* a;
int* aj;
int* bj;
int w, l;
w = tilec->x1-tilec->x0;
l = tilec->numresolutions-1;
a = tilec->data;
for (i = l-1; i >= stop; i--) {
int rw; /* width of the resolution level computed */
int rh; /* heigth of the resolution level computed */
int rw1; /* width of the resolution level once lower than computed one */
int rh1; /* height of the resolution level once lower than computed one */
int cas_col; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
int cas_row; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
int dn, sn;
rw = tilec->resolutions[l - i].x1 - tilec->resolutions[l - i].x0;
rh = tilec->resolutions[l - i].y1 - tilec->resolutions[l - i].y0;
rw1 =
tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1 =
tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
rw1= tilec->resolutions[l - i - 1].x1 - tilec->resolutions[l - i - 1].x0;
rh1= tilec->resolutions[l - i - 1].y1 - tilec->resolutions[l - i - 1].y0;
if (tilec->previous_row) {
previous_rw =
tilec->resolutions[l - i].previous_x1 - tilec->resolutions[l -
i].
previous_x0;
previous_rw1 =
tilec->resolutions[l - i - 1].previous_x1 - tilec->resolutions[l -
i -
1].
previous_x0;
if ((previous_rw1 > previous_rw - previous_rw1 && rw1 == rw - rw1)
|| (row_tilec->resolutions[l - i - 1].cas_row
&& previous_rw1 == previous_rw - previous_rw1))
cas_row = 1;
cas_col = tilec->resolutions[l - i].x0 % 2; /* 0 = non inversion on horizontal filtering 1 = inversion between low-pass and high-pass filtering */
cas_row = tilec->resolutions[l - i].y0 % 2; /* 0 = non inversion on vertical filtering 1 = inversion between low-pass and high-pass filtering */
sn = rw1;
dn = rw-rw1;
bj = (int*)malloc(rw * sizeof(int));
for (j = 0; j < rh; j++) {
aj = a+j*w;
dwt_interleave_h(aj, bj, dn, sn, cas_col);
dwt_decode_1_real(bj, dn, sn, cas_col);
for (k = 0; k < rw; k++) aj[k] = bj[k];
}
free(bj);
if (tilec->previous_col) {
previous_rh =
tilec->resolutions[l - i].previous_y1 - tilec->resolutions[l -
i].
previous_y0;
previous_rh1 =
tilec->resolutions[l - i - 1].previous_y1 - tilec->resolutions[l -
i -
1].
previous_y0;
if ((previous_rh1 > previous_rh - previous_rh1 && rh1 == rh - rh1)
|| (col_tilec->resolutions[l - i - 1].cas_col
&& previous_rh1 == previous_rh - previous_rh1))
cas_col = 1;
sn = rh1;
dn = rh-rh1;
bj = (int*)malloc(rh * sizeof(int));
for (j=0; j<rw; j++) {
aj = a+j;
dwt_interleave_v(aj, bj, dn, sn, w, cas_row);
dwt_decode_1_real(bj, dn, sn, cas_row);
for (k = 0; k < rh; k++) aj[k * w] = bj[k];
}
if (rw1 < rw - rw1)
cas_row = 1;
if (rh1 < rh - rh1)
cas_col = 1;
/* OFFSET IMAGE (If origin of the resolution is odd and first tile on the row or column) */
if (!tilec->previous_row && ((tilec->resolutions[l - i].x0 % 2) == 1))
cas_row = 1;
if (!tilec->previous_col && ((tilec->resolutions[l - i].y0 % 2) == 1))
cas_col = 1;
tilec->resolutions[l - i - 1].cas_row = cas_row;
tilec->resolutions[l - i - 1].cas_col = cas_col;
for (j = 0; j < rh; j++)
dwt_decode_1_real(a + j * w, rw, 1, rw1, cas_row);
for (j = 0; j < rw; j++)
dwt_decode_1_real(a + j, rh, w, rh1, cas_col);
free(bj);
}
}
/* <summary> */
/* Get gain of 9-7 wavelet transform. */
/* </summary> */
int dwt_getgain_real(int orient)
{
(void)orient;
return 0;
}

View File

@@ -33,23 +33,16 @@
/*
* Apply a reversible DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
* tilec : tile component information (present tile)
*/
/* void dwt_encode(int* a, int w, int h, int l); */
void dwt_encode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
void dwt_encode(tcd_tilecomp_t * tilec);
/*
* Apply a reversible inverse DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
* tilec : tile component information (present tile)
*/
void dwt_decode(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec, tcd_tilecomp_t * col_tilec);
/* void dwt_decode(int* a, int w, int h,tcd_tilecomp_t *tilec, int l); */
void dwt_decode(tcd_tilecomp_t * tilec, int stop);
/*
* Get the gain of a subband for the reversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)
@@ -65,23 +58,13 @@ double dwt_getnorm(int level, int orient);
/*
* Apply an irreversible DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
void dwt_encode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l);
void dwt_encode_real(tcd_tilecomp_t * tilec);
/*
* Apply an irreversible inverse DWT transform to a component of an image
* a: samples of the component
* w: width of the component
* h: height of the component
* l: number of decomposition levels in the DWT
*/
void dwt_decode_real(int *a, int w, int h, tcd_tilecomp_t * tilec, int l,
tcd_tilecomp_t * row_tilec,
tcd_tilecomp_t * col_tilec);
void dwt_decode_real(tcd_tilecomp_t * tilec, int stop);
/*
* Get the gain of a subband for the irreversible DWT
* orient: number that identifies the subband (0->LL, 1->HL, 2->LH, 3->HH)

View File

@@ -25,17 +25,39 @@
*/
#include "fix.h"
#include <math.h> /*Add Antonin : multbug1*/
#ifdef DAVID_WIN32
#ifdef WIN32
#define int64 __int64
#else
#define int64 long long
#endif
/* <summary> */
/* Multiply two fixed-precision rational numbers. */
/* </summary> */
int fix_mul(int a, int b)
/*
* Multiply two fixed-precision rational numbers.
*/
/*int fix_mul(int a, int b)
{
return (int) ((int64) a * (int64) b >> 13);
}*/
/*Mod Antonin : multbug1*/
/*
int fix_mul(int a, int b)
{
double tmp= (double) ((int64) a * (int64) b);
int64 v = (int64) ((fabs(tmp/8192.0)>=floor(fabs(tmp/8192.0))+0.5)?fabs(tmp/8192.0)+1.0:fabs(tmp/8192.0));
v = (tmp<0)?-v:v;
return (int) v;
}
*/
/*doM*/
int fix_mul(int a, int b) /* Luke Lee optimized : 11/16/2004*/
{
int64 temp = (int64) a * (int64) b >> 12;
return (int) ((temp >> 1) + (temp & 1)) ;
}

View File

@@ -24,25 +24,35 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
/* <summary> */
/* Get the minimum of two integers. */
/* </summary> */
#include "int.h"
/*
* Get the minimum of two integers.
*
* returns a if a < b else b
*/
int int_min(int a, int b)
{
return a < b ? a : b;
}
/* <summary> */
/* Get the maximum of two integers. */
/* </summary> */
/*
* Get the maximum of two integers.
*
* returns a if a > b else b
*/
int int_max(int a, int b)
{
return a > b ? a : b;
}
/* <summary> */
/* Clamp an integer inside an interval. */
/* </summary> */
/*
* Clamp an integer inside an interval.
*
* return a if (min < a < max)
* return max if (a > max)
* return min if (a < min)
*/
int int_clamp(int a, int min, int max)
{
if (a < min)
@@ -52,41 +62,49 @@ int int_clamp(int a, int min, int max)
return a;
}
/* <summary> */
/* Get absolute value of integer. */
/* </summary> */
/*
* Get absolute value of integer.
*/
int int_abs(int a)
{
return a < 0 ? -a : a;
}
/* <summary> */
/* Divide an integer and round upwards. */
/* </summary> */
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
int int_ceildiv(int a, int b)
{
return (a + b - 1) / b;
}
/* <summary> */
/* Divide an integer by a power of 2 and round upwards. */
/* </summary> */
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
int int_ceildivpow2(int a, int b)
{
return (a + (1 << b) - 1) >> b;
}
/* <summary> */
/* Divide an integer by a power of 2 and round downwards. */
/* </summary> */
/*
* Divide an integer by a power of 2 and round downwards.
*
* a divided by 2^b
*/
int int_floordivpow2(int a, int b)
{
return a >> b;
}
/* <summary> */
/* Get logarithm of an integer and round downwards. */
/* </summary> */
/*
* Get logarithm of an integer and round downwards.
*
* log2(a)
*/
int int_floorlog2(int a)
{
int l;

View File

@@ -24,16 +24,65 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "j2k.h"
#ifndef __INT_H
#define __INT_H
/*
* Get the minimum of two integers.
*
* returns a if a < b else b
*/
int int_min(int a, int b);
/*
* Get the maximum of two integers.
*
* returns a if a > b else b
*/
int int_max(int a, int b);
/*
* Clamp an integer inside an interval.
*
* return a if (min < a < max)
* return max if (a > max)
* return min if (a < min)
*/
int int_clamp(int a, int min, int max);
/*
* Get absolute value of integer.
*/
int int_abs(int a);
/*
* Divide an integer and round upwards.
*
* a divided by b
*/
int int_ceildiv(int a, int b);
int int_ceildivpow2(int a, int b);
int int_floordivpow2(int a, int b);
/*
* Divide an integer by a power of 2 and round upwards.
*
* a divided by 2^b
*/
LIBJ2K_API int int_ceildivpow2(int a, int b);
/*
* Divide an integer by a power of 2 and round downwards.
*
* a divided by 2^b
*/
LIBJ2K_API int int_floordivpow2(int a, int b);
/*
* Get logarithm of an integer and round downwards.
*
* log2(a)
*/
int int_floorlog2(int a);
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -28,21 +28,34 @@
#define VERSION "0.0.8"
#ifdef DAVID_WIN32
#ifdef LIBJ2K_EXPORTS
#if defined(_WIN32) && defined (OPENJPEGDLL)
#ifdef gdcmopenjpeg_EXPORTS /*LIBJ2K_EXPORTS*/
#define LIBJ2K_API __declspec(dllexport)
#else
#define LIBJ2K_API __declspec(dllimport)
#endif
#else
#ifdef OPENJPEGSTATIC
#define LIBJ2K_API extern
#else
#define LIBJ2K_API
#endif
#endif
#ifndef __J2K_H
#define __J2K_H
#define J2K_MAXRLVLS 33
#define J2K_MAXBANDS (3*J2K_MAXRLVLS+1)
#define J2K_MAXRLVLS 33 /* Number of maximum resolution level authorized */
#define J2K_MAXBANDS (3*J2K_MAXRLVLS-2) /* Number of maximum sub-band linked to number of resolution level */
#define J2K_CFMT 0
#define JP2_CFMT 1
#define JPT_CFMT 2
#define MJ2_CFMT 3
#define PXM_DFMT 0
#define PGX_DFMT 1
#define BMP_DFMT 2
#define YUV_DFMT 3
#define J2K_CP_CSTY_PRT 0x01
#define J2K_CP_CSTY_SOP 0x02
@@ -60,9 +73,13 @@
typedef struct {
int dx, dy; /* XRsiz, YRsiz */
int w, h; /* width and height of data */
int x0, y0; /* offset of the component compare to the whole image */
int prec; /* precision */
int bpp; /* deapth of image in bits */
int sgnd; /* signed */
int resno_decoded; /* number of decoded resolution */
int factor; /* number of division by 2 of the out image compare to the original size of image */
int *data; /* image-component data */
} j2k_comp_t;
@@ -70,8 +87,7 @@ typedef struct {
int x0, y0; /* XOsiz, YOsiz */
int x1, y1; /* Xsiz, Ysiz */
int numcomps; /* number of components */
int index_on; /* 0 = no index || 1 = index */
/* int PPT; */
int color_space; /* sRGB, Greyscale or YUV */
j2k_comp_t *comps; /* image-components */
} j2k_image_t;
@@ -88,7 +104,7 @@ typedef struct {
int cblksty; /* code-block coding style */
int qmfbid; /* discrete wavelet transform identifier */
int qntsty; /* quantisation style */
j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantisation */
j2k_stepsize_t stepsizes[J2K_MAXBANDS]; /* stepsizes used for quantization */
int numgbits; /* number of guard bits */
int roishift; /* Region Of Interest shift */
int prcw[J2K_MAXRLVLS]; /* Precinct width */
@@ -104,39 +120,67 @@ typedef struct {
} j2k_poc_t;
typedef struct {
int first; /* 1 : first part-tile of a tile */
int csty; /* coding style */
int prg; /* progression order */
int numlayers; /* number of layers */
int mct; /* multi-component transform identifier */
int rates[100]; /* rates of layers */
int numpocs; /* number of progression order changes */
int POC; /* Precise if a POC marker has been used O:NO, 1:YES */
j2k_poc_t pocs[32]; /* progression order changes */
unsigned char *ppt_data; /* packet header store there for futur use in t2_decode_packet */
unsigned char *ppt_data_first; /* pointer remaining on the first byte of the first header if ppt is used */
int ppt; /* If ppt == 1 --> there was a PPT marker for the present tile */
int ppt_store; /* Use in case of multiple marker PPT (number of info already store) */
int ppt_len; /* ppmbug1 */
float distoratio[100]; /* add fixed_quality */
j2k_tccp_t *tccps; /* tile-component coding parameters */
} j2k_tcp_t;
typedef struct {
int image_type; /* 0: PNM, PGM, PPM 1: PGX */
int intermed_file; /* 1: Store each encoded tile one by one in the output file (for mega-Images)*/
int decod_format; /* 0: PGX, 1: PxM, 2: BMP */
int cod_format; /* 0: J2K, 1: JP2, 2: JPT */
int disto_alloc; /* Allocation by rate/distortion */
int fixed_alloc; /* Allocation by fixed layer */
int fixed_quality; /* add fixed_quality */
int reduce; /* if != 0, then original dimension divided by 2^(reduce); if == 0 or not used, image is decoded to the full resolution */
int layer; /* if != 0, then only the first "layer" layers are decoded; if == 0 or not used, all the quality layers are decoded */
int index_on; /* 0 = no index || 1 = index */
int tx0, ty0; /* XTOsiz, YTOsiz */
int tdx, tdy; /* XTsiz, YTsiz */
char *comment; /* comment for coding */
int tw, th;
int tw, th; /* number of tiles in width and heigth */
int *tileno; /* ID number of the tiles present in the codestream */
int tileno_size; /* size of the vector tileno */
unsigned char *ppm_data; /* packet header store there for futur use in t2_decode_packet */
unsigned char *ppm_data_first; /* pointer remaining on the first byte of the first header if ppm is used */
int ppm; /* If ppm == 1 --> there was a PPM marker for the present tile */
int ppm_store; /* Use in case of multiple marker PPM (number of info already store) */
int ppm_previous; /* Use in case of multiple marker PPM (case on non-finished previous info) */
int ppm_len; /* ppmbug1 */
j2k_tcp_t *tcps; /* tile coding parameters */
int *matrice; /* Fixed layer */
} j2k_cp_t;
typedef struct {
int start_pos, end_pos; /* start and end position */
double disto;
double disto; /* ADD for Marcela */
} info_packet; /* Index struct */
typedef struct {
double *thresh; /* value of thresh for each layer by tile cfr. Marcela */
int num_tile; /* Number of Tile */
int start_pos; /* Start position */
int end_header; /* End position of the header */
int end_pos; /* End position */
int pw, ph; /* number of precinct by tile */
int pw[33], ph[33]; /* precinct number for each resolution level */
int pdx[33], pdy[33]; /* precinct size (in power of 2), in X and Y for each resolution level */
info_packet *packet; /* information concerning packets inside tile */
int nbpix; /* add fixed_quality */
double distotile; /* add fixed_quality */
} info_tile; /* index struct */
typedef struct {
@@ -146,29 +190,26 @@ typedef struct {
int index_write; /* writing the packet inthe index with t2_encode_packets */
int Im_w, Im_h; /* Image width and Height */
int Prog; /* progression order */
int Tile_x, Tile_y; /* Number of Tile in X and Y */
int tw, th;
int Tile_x, Tile_y; /* Tile size in x and y */
int Tile_Ox, Tile_Oy;
int tw, th; /* Number of Tile in X and Y */
int Comp; /* Component numbers */
int Layer; /* number of layer */
int Decomposition; /* number of decomposition */
int pw, ph; /* nombre precinct in X and Y */
int pdx, pdy; /* size of precinct in X and Y */
int Main_head_end; /* Main header position */
int codestream_size; /* codestream's size */
info_tile *tile; /* information concerning tiles inside image */
} info_image; /* index struct */
/*
* Encode an image into a JPEG-2000 codestream
* i: image to encode
* cp: coding parameters
* dest: destination buffer
* output: destination buffer or name of the output file when cp->intermed_file==1
* len: length of destination buffer
* index : index file name
*/
LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *output,
int len, char *index);
/* LIBJ2K_API int j2k_encode(j2k_image_t *i, j2k_cp_t *cp,unsigned char *dest, int len); */
@@ -179,7 +220,22 @@ LIBJ2K_API int j2k_encode(j2k_image_t * i, j2k_cp_t * cp, char *outfile,
* i: decode image
* cp: coding parameters that were used to encode the image
*/
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t ** i,
j2k_cp_t ** cp);
LIBJ2K_API int j2k_decode(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp);
/*
* Decode an image form a JPT-stream (JPEG 2000, JPIP)
* src: source buffer
* len: length of source buffer
* i: decode image
* cp: coding parameters that were used to encode the image
*
*/
int j2k_decode_jpt_stream(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp);
LIBJ2K_API void j2k_dec_release();/*antonin*/
#endif

529
libopenjpeg/jp2.c Normal file
View File

@@ -0,0 +1,529 @@
/*
* Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "j2k.h"
#include "jp2.h"
#include "cio.h"
#define JPIP_JPIP 0x6a706970
#define JP2_JP 0x6a502020
#define JP2_FTYP 0x66747970
#define JP2_JP2H 0x6a703268
#define JP2_IHDR 0x69686472
#define JP2_COLR 0x636f6c72
#define JP2_JP2C 0x6a703263
#define JP2_URL 0x75726c20
#define JP2_DBTL 0x6474626c
#define JP2_BPCC 0x62706363
#define JP2_JP2 0x6a703220
/*
*
* Read box headers
*
*/
int jp2_read_boxhdr(jp2_box_t * box)
{
box->init_pos = cio_tell();
box->length = cio_read(4);
box->type = cio_read(4);
if (box->length == 1) {
if (cio_read(4) != 0) {
fprintf(stderr, "Error: Cannot handle box sizes higher than 2^32\n");
return 1;
}
box->length = cio_read(4);
if (box->length == 0)
box->length = cio_numbytesleft() + 12;
} else if (box->length == 0) {
box->length = cio_numbytesleft() + 8;
}
return 0;
}
/*
*
* Initialisation of a Standard JP2 structure
*/
int jp2_init_stdjp2(jp2_struct_t * jp2_struct)
{
jp2_struct->comps =
(jp2_comps_t *) malloc(jp2_struct->numcomps * sizeof(jp2_comps_t));
jp2_struct->precedence = 0; /* PRECEDENCE*/
jp2_struct->approx = 0; /* APPROX*/
jp2_struct->brand = JP2_JP2; /* BR */
jp2_struct->minversion = 0; /* MinV */
jp2_struct->numcl = 1;
jp2_struct->cl = (unsigned int *) malloc(jp2_struct->numcl * sizeof(int));
jp2_struct->cl[0] = JP2_JP2; /* CL0 : JP2 */
jp2_struct->C = 7; /* C : Always 7*/
jp2_struct->UnkC = 0; /* UnkC, colorspace specified in colr box*/
jp2_struct->IPR = 0; /* IPR, no intellectual property*/
return 0;
}
void jp2_write_url(char *Idx_file)
{
unsigned int i;
char str[256];
jp2_box_t box;
sprintf(str, "%s", Idx_file);
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_URL, 4); /* DBTL*/
cio_write(0, 1); /* VERS*/
cio_write(0, 3); /* FLAG*/
for (i = 0; i < strlen(str); i++) {
cio_write(str[i], 1);
}
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the IHDR box
*
* Image Header box
*
*/
int jp2_read_ihdr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_IHDR != box.type) {
fprintf(stderr, "Error: Expected IHDR Marker\n");
return 1;
}
jp2_struct->h = cio_read(4); /* HEIGHT*/
jp2_struct->w = cio_read(4); /* WIDTH*/
jp2_struct->numcomps = cio_read(2); /* NC*/
jp2_struct->bpc = cio_read(1); /* BPC*/
jp2_struct->C = cio_read(1); /* C */
jp2_struct->UnkC = cio_read(1); /* UnkC*/
jp2_struct->IPR = cio_read(1); /* IPR*/
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with IHDR Box\n");
return 1;
}
return 0;
}
void jp2_write_ihdr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_IHDR, 4); /* IHDR*/
cio_write(jp2_struct->h, 4); /* HEIGHT*/
cio_write(jp2_struct->w, 4); /* WIDTH*/
cio_write(jp2_struct->numcomps, 2); /* NC*/
cio_write(jp2_struct->bpc, 1); /* BPC */
cio_write(jp2_struct->C, 1); /* C : Always 7*/
cio_write(jp2_struct->UnkC, 1); /* UnkC, colorspace unknow*/
cio_write(jp2_struct->IPR, 1); /* IPR, no intellectual property*/
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
void jp2_write_bpcc(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_BPCC, 4); /* BPCC*/
for (i = 0; i < jp2_struct->numcomps; i++)
cio_write(jp2_struct->comps[i].bpcc, 1);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
int jp2_read_bpcc(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_BPCC != box.type) {
fprintf(stderr, "Error: Expected BPCC Marker\n");
return 1;
}
for (i = 0; i < jp2_struct->numcomps; i++)
jp2_struct->comps[i].bpcc = cio_read(1);
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with BPCC Box\n");
return 1;
}
return 0;
}
void jp2_write_colr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_COLR, 4); /* COLR*/
cio_write(jp2_struct->meth, 1); /* METH*/
cio_write(jp2_struct->precedence, 1); /* PRECEDENCE*/
cio_write(jp2_struct->approx, 1); /* APPROX*/
if (jp2_struct->meth == 1)
cio_write(jp2_struct->enumcs, 4); /* EnumCS*/
else
cio_write(0, 1); /* PROFILE (??)*/
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
int jp2_read_colr(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
int skip_len;
jp2_read_boxhdr(&box);
do {
if (JP2_COLR != box.type) {
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_COLR != box.type);
jp2_struct->meth = cio_read(1); /* METH*/
jp2_struct->precedence = cio_read(1); /* PRECEDENCE*/
jp2_struct->approx = cio_read(1); /* APPROX*/
if (jp2_struct->meth == 1)
jp2_struct->enumcs = cio_read(4); /* EnumCS*/
else {
/* SKIP PROFILE */
skip_len = box.init_pos + box.length - cio_tell();
if (skip_len < 0) {
fprintf(stderr, "Error with JP2H box size\n");
return 1;
}
cio_skip(box.init_pos + box.length - cio_tell());
}
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with BPCC Box\n");
return 1;
}
return 0;
}
/*
* Write the JP2H box
*
* JP2 Header box
*
*/
void jp2_write_jp2h(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);;
cio_write(JP2_JP2H, 4); /* JP2H */
jp2_write_ihdr(jp2_struct);
if (jp2_struct->bpc == 255)
jp2_write_bpcc(jp2_struct);
jp2_write_colr(jp2_struct);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the JP2H box
*
* JP2 Header box
*
*/
int jp2_read_jp2h(jp2_struct_t * jp2_struct)
{
jp2_box_t box;
int skip_len;
jp2_read_boxhdr(&box);
do {
if (JP2_JP2H != box.type) {
if (box.type == JP2_JP2C) {
fprintf(stderr, "Error: Expected JP2H Marker\n");
return 1;
}
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_JP2H != box.type);
if (jp2_read_ihdr(jp2_struct))
return 1;
if (jp2_struct->bpc == 255) {
if (jp2_read_bpcc(jp2_struct))
return 1;
}
if (jp2_read_colr(jp2_struct))
return 1;
skip_len = box.init_pos + box.length - cio_tell();
if (skip_len < 0) {
fprintf(stderr, "Error with JP2H box size\n");
return 1;
}
cio_skip(box.init_pos + box.length - cio_tell());
return 0;
}
/*
* Write the FTYP box
*
* File type box
*
*/
void jp2_write_ftyp(jp2_struct_t * jp2_struct)
{
unsigned int i;
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_FTYP, 4); /* FTYP */
cio_write(jp2_struct->brand, 4); /* BR */
cio_write(jp2_struct->minversion, 4); /* MinV */
for (i = 0; i < jp2_struct->numcl; i++)
cio_write(jp2_struct->cl[i], 4); /* CL */
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the FTYP box
*
* File type box
*
*/
int jp2_read_ftyp(jp2_struct_t * jp2_struct)
{
int i;
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_FTYP != box.type) {
fprintf(stderr, "Error: Excpected FTYP Marker\n");
return 1;
}
jp2_struct->brand = cio_read(4); /* BR */
jp2_struct->minversion = cio_read(4); /* MinV */
jp2_struct->numcl = (box.length - 16) / 4;
jp2_struct->cl =
(unsigned int *) malloc(jp2_struct->numcl * sizeof(unsigned int));
for (i = 0; i < (int) jp2_struct->numcl; i++)
jp2_struct->cl[i] = cio_read(4); /* CLi */
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with FTYP Box\n");
return 1;
}
return 0;
}
int jp2_write_jp2c(int j2k_codestream_len, int *j2k_codestream_offset,
char *j2k_codestream)
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_JP2C, 4); /* JP2C*/
*j2k_codestream_offset = cio_tell();
memcpy(cio_getbp(), j2k_codestream, j2k_codestream_len);
box.length = 8 + j2k_codestream_len;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
return box.length;
}
int jp2_read_jp2c(unsigned int *j2k_codestream_len,
unsigned int *j2k_codestream_offset)
{
jp2_box_t box;
jp2_read_boxhdr(&box);
do {
if (JP2_JP2C != box.type) {
cio_skip(box.length - 8);
jp2_read_boxhdr(&box);
}
} while (JP2_JP2C != box.type);
*j2k_codestream_offset = cio_tell();
*j2k_codestream_len = box.length - 8;
return 0;
}
void jp2_write_jp()
{
jp2_box_t box;
box.init_pos = cio_tell();
cio_skip(4);
cio_write(JP2_JP, 4); /* JP*/
cio_write(0x0d0a870a, 4);
box.length = cio_tell() - box.init_pos;
cio_seek(box.init_pos);
cio_write(box.length, 4); /* L */
cio_seek(box.init_pos + box.length);
}
/*
* Read the JP box
*
* JPEG 2000 signature
*
* return 1 if error else 0
*/
int jp2_read_jp()
{
jp2_box_t box;
jp2_read_boxhdr(&box);
if (JP2_JP != box.type) {
fprintf(stderr, "Error: Expected JP Marker\n");
return 1;
}
if (0x0d0a870a != cio_read(4)) {
fprintf(stderr, "Error with JP Marker\n");
return 1;
}
if (cio_tell() - box.init_pos != box.length) {
fprintf(stderr, "Error with JP Box size\n");
return 1;
}
return 0;
}
int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len)
{
cio_init(src, len);
if (jp2_read_jp())
return 1;
if (jp2_read_ftyp(jp2_struct))
return 1;
if (jp2_read_jp2h(jp2_struct))
return 1;
if (jp2_read_jp2c
(&jp2_struct->j2k_codestream_len,
&jp2_struct->j2k_codestream_offset))
return 1;
return 0;
}
int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
char *output)
{
(void)output;
jp2_write_jp();
jp2_write_ftyp(jp2_struct);
jp2_write_jp2h(jp2_struct);
jp2_write_jp2c(jp2_struct->j2k_codestream_len,
&jp2_struct->j2k_codestream_offset, j2k_codestream);
return cio_tell();
}

118
libopenjpeg/jp2.h Normal file
View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __JP2_H
#define __JP2_H
#include "j2k.h"
typedef struct {
int depth;
int sgnd;
int bpcc;
} jp2_comps_t;
typedef struct {
unsigned int w;
unsigned int h;
unsigned int numcomps;
unsigned int bpc;
unsigned int C;
unsigned int UnkC;
unsigned int IPR;
unsigned int meth;
unsigned int approx;
unsigned int enumcs;
unsigned int precedence;
unsigned int brand;
unsigned int minversion;
unsigned int numcl;
unsigned int *cl;
jp2_comps_t *comps;
j2k_image_t *image;
unsigned int j2k_codestream_offset;
unsigned int j2k_codestream_len;
} jp2_struct_t;
typedef struct {
int length;
int type;
int init_pos;
} jp2_box_t;
/* int jp2_init_stdjp2(jp2_struct_t * jp2_struct, j2k_image_t * img);
*
* Create a standard jp2_structure
* jp2_struct: the structure you are creating
* img: a j2k_image_t wich will help you to create the jp2_structure
*/
int jp2_init_stdjp2(jp2_struct_t * jp2_struct);
/* int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream)
*
* Write the jp2c codestream box
* j2k_len: the j2k codestream length
* j2k_codestream_offset: the function will return the j2k codestream offset
* j2k_codestream: the j2k codestream to include in jp2 file
*/
int jp2_write_jp2c(int j2k_len, int *j2k_codestream_offset, char *j2k_codestream);
/* int jp2_write_jp2h(jp2_struct_t * jp2_struct);
*
* Write the jp2h header box
* jp2_struct: the jp2 structure you are working with
*/
void jp2_write_jp2h(jp2_struct_t * jp2_struct);
/* int jp2_read_jp2h(jp2_struct_t * jp2_struct);
*
* Read the jp2h header box
* jp2_struct: the jp2 structure you are working with
*/
int jp2_read_jp2h(jp2_struct_t * jp2_struct);
/* int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
int j2k_len, char *output)
*
* Wrap a J2K codestream in a JP2 file
* jp2_struct: the jp2 structure used to create jp2 boxes
* j2k_codestream: the j2k codestream to include in jp2 file
* output: pointer to jp2 codestream that will be created
*/
int jp2_wrap_j2k(jp2_struct_t * jp2_struct, char *j2k_codestream,
char *output);
/* int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct);
*
* Decode the structure of a JP2 file
* src: pointer to memory where compressed data is stored
* jp2_struct: the jp2 structure that will be created
* len: length of jp2 codestream
*/
int jp2_read_struct(unsigned char *src, jp2_struct_t * jp2_struct, int len);
#endif

163
libopenjpeg/jpt.c Normal file
View File

@@ -0,0 +1,163 @@
/*
* Copyright (c) 2004, Yannick Verschueren
* Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include "jpt.h"
#include "j2k.h"
#include "cio.h"
/*
* Read the information contains in VBAS [JPP/JPT stream message header]
* Store information (7 bits) in value
*
*/
unsigned int jpt_read_VBAS_info(unsigned int value)
{
unsigned char elmt;
elmt = cio_read(1);
while ((elmt >> 7) == 1) {
value = (value << 7);
value |= (elmt & 0x7f);
elmt = cio_read(1);
}
value = (value << 7);
value |= (elmt & 0x7f);
return value;
}
/*
* Initialize the value of the message header structure
*
*/
void jpt_init_Msg_Header(jpt_msg_header_struct_t * header)
{
header->Id = 0; /* In-class Identifier */
header->last_byte = 0; /* Last byte information */
header->Class_Id = 0; /* Class Identifier */
header->CSn_Id = 0; /* CSn : index identifier */
header->Msg_offset = 0; /* Message offset */
header->Msg_length = 0; /* Message length */
header->Layer_nb = 0; /* Auxiliary for JPP case */
}
/*
* Re-initialize the value of the message header structure
*
* Only parameters always present in message header
*
*/
void jpt_reinit_Msg_Header(jpt_msg_header_struct_t * header)
{
header->Id = 0; /* In-class Identifier */
header->last_byte = 0; /* Last byte information */
header->Msg_offset = 0; /* Message offset */
header->Msg_length = 0; /* Message length */
}
/*
* Read the message header for a JPP/JPT - stream
*
*/
void jpt_read_Msg_Header(jpt_msg_header_struct_t * header)
{
unsigned char elmt, Class = 0, CSn = 0;
jpt_reinit_Msg_Header(header);
/* ------------- */
/* VBAS : Bin-ID */
/* ------------- */
elmt = cio_read(1);
/* See for Class and CSn */
switch ((elmt >> 5) & 0x03) {
case 0:
fprintf(stderr, "Forbidden value encounter in message header !!\n");
break;
case 1:
Class = 0;
CSn = 0;
break;
case 2:
Class = 1;
CSn = 0;
break;
case 3:
Class = 1;
CSn = 1;
break;
default:
break;
}
/* see information on bits 'c' [p 10 : A.2.1 general, ISO/IEC FCD 15444-9] */
if (((elmt >> 4) & 0x01) == 1)
header->last_byte = 1;
/* In-class identifier */
header->Id |= (elmt & 0x0f);
if ((elmt >> 7) == 1)
header->Id = jpt_read_VBAS_info(header->Id);
/* ------------ */
/* VBAS : Class */
/* ------------ */
if (Class == 1) {
header->Class_Id = 0;
header->Class_Id = jpt_read_VBAS_info(header->Class_Id);
}
/* ---------- */
/* VBAS : CSn */
/* ---------- */
if (CSn == 1) {
header->CSn_Id = 0;
header->CSn_Id = jpt_read_VBAS_info(header->CSn_Id);
}
/* ----------------- */
/* VBAS : Msg_offset */
/* ----------------- */
header->Msg_offset = jpt_read_VBAS_info(header->Msg_offset);
/* ----------------- */
/* VBAS : Msg_length */
/* ----------------- */
header->Msg_length = jpt_read_VBAS_info(header->Msg_length);
/* ---------- */
/* VBAS : Aux */
/* ---------- */
if ((header->Class_Id & 0x01) == 1) {
header->Layer_nb = 0;
header->Layer_nb = jpt_read_VBAS_info(header->Layer_nb);
}
}

56
libopenjpeg/jpt.h Normal file
View File

@@ -0,0 +1,56 @@
/*
* Copyright (c) 2004, Yannick Verschueren
* Copyright (c) 2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Message Header JPT_stream Structure
*
*/
typedef struct {
unsigned int Id; /* In-class Identifier */
unsigned int last_byte; /* Last byte information */
unsigned int Class_Id; /* Class Identifier */
unsigned int CSn_Id; /* CSn : index identifier */
unsigned int Msg_offset; /* Message offset */
unsigned int Msg_length; /* Message length */
unsigned int Layer_nb; /* Auxiliary for JPP case */
} jpt_msg_header_struct_t;
/*
* Initialize the value of the message header structure
*
* header : Message header structure
*
*/
void jpt_init_Msg_Header(jpt_msg_header_struct_t * header);
/*
* Read the message header for a JPP/JPT - stream
*
* header : Message header structure
*
*/
void jpt_read_Msg_Header(jpt_msg_header_struct_t * header);

View File

@@ -318,9 +318,9 @@ void mqc_bypass_init_enc()
{
mqc_c = 0;
mqc_ct = 8;
if (*mqc_bp == 0xff) {
/*if (*mqc_bp == 0xff) {
mqc_ct = 7;
}
} */
}
/* <summary> */
@@ -405,6 +405,7 @@ int mqc_restart_enc()
void mqc_restart_init_enc()
{
/* <Re-init part> */
mqc_setcurctx(0);
mqc_a = 0x8000;
mqc_c = 0;
mqc_ct = 12;
@@ -412,7 +413,6 @@ void mqc_restart_init_enc()
if (*mqc_bp == 0xff) {
mqc_ct = 13;
}
}
@@ -536,7 +536,10 @@ void mqc_init_dec(unsigned char *bp, int len)
mqc_start = bp;
mqc_end = bp + len;
mqc_bp = bp;
mqc_c = *mqc_bp << 16;
/*add antonin initbug1*/
if (len==0) mqc_c = 0xff << 16;
else mqc_c = *mqc_bp << 16;
/*dda*/
mqc_bytein();
mqc_c <<= 7;
mqc_ct -= 7;

View File

@@ -79,7 +79,7 @@ void mqc_bypass_init_enc();
/*
* BYPASS mode switch
*/
void mqc_bypass_enc();
void mqc_bypass_enc(int d);
/*
* BYPASS mode switch

View File

@@ -31,6 +31,7 @@
#include <j2k.h>
#include <tcd.h>
#include <jp2.h>
#include <bio.h>
#include <cio.h>

View File

@@ -1,7 +1,7 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2003, Yannick Verschueren
* Copyright (c) 2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2003-2004, Yannick Verschueren
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -31,34 +31,26 @@
#include <stdlib.h>
#include <stdio.h>
/* <summary> */
/* Create a packet iterator. */
/* </summary> */
/* <summary>
* Create a packet iterator.
* </summary> */
pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
{
int p, q;
int compno, resno, pino, layno, precno;
int p, q, i;
int compno, resno, pino;
int maxres = 0;
pi_iterator_t *pi;
j2k_tcp_t *tcp;
j2k_tccp_t *tccp;
tcp = &cp->tcps[tileno];
pi =
(pi_iterator_t *) malloc((tcp->numpocs + 1) * sizeof(pi_iterator_t));
pi = (pi_iterator_t *) malloc((tcp->numpocs + 1) *
sizeof(pi_iterator_t));
for (pino = 0; pino < tcp->numpocs + 1; pino++) { /* change */
p = tileno % cp->tw;
q = tileno / cp->tw;
/* pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0);
pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0);
pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
pi->numcomps=img->numcomps;
pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t)); */
pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, img->x0);
pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, img->y0);
pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, img->x1);
@@ -69,7 +61,6 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
for (compno = 0; compno < pi->numcomps; compno++) {
int tcx0, tcy0, tcx1, tcy1;
/* pi_comp_t *comp=&pi->comps[compno]; */
pi_comp_t *comp = &pi[pino].comps[compno];
tccp = &tcp->tccps[compno];
comp->dx = img->comps[compno].dx;
@@ -106,34 +97,29 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
res->pw = (px1 - px0) >> res->pdx;
res->ph = (py1 - py0) >> res->pdy;
res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx); /*Mod Antonin : sizebug1*/
res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy); /*Mod Antonin : sizebug1*/
}
}
/* pi->first=1;
pi->poc.resno0=0;
pi->poc.compno0=0;
pi->poc.layno1=tcp->numlayers;
pi->poc.resno1=maxres;
pi->poc.compno1=img->numcomps;
pi->poc.prg=tcp->prg; */
tccp = &tcp->tccps[0];
pi[pino].step_p = 1;
pi[pino].step_c = 100 * pi[pino].step_p;
pi[pino].step_r = img->numcomps * pi[pino].step_c;
pi[pino].step_l = maxres * pi[pino].step_r;
for (layno = 0; layno < 10; layno++) {
/* pi_comp_t *compo; */
/* pi_resolution_t *res; */
for (resno = 0; resno < 10; resno++) {
for (compno = 0; compno < 3; compno++) {
/* compo=&pi[pino].comps[compno]; */
/* res=&compo->resolutions[pi->resno]; */
for (precno = 0; precno < 99; precno++) {
pi[pino].include[layno][resno][compno][precno] = 0;
}
}
}
if (pino == 0) {
pi[pino].include =
(short int *) malloc(img->numcomps * maxres *
tcp->numlayers * 100 * sizeof(short int));
for (i = 0; i < img->numcomps * maxres * tcp->numlayers * 100; i++)
pi[pino].include[i] = 0;
}
/* pi[pino].include=(short int*)calloc(img->numcomps*maxres*tcp->numlayers*1000,sizeof(short int)); */
else
pi[pino].include = pi[pino - 1].include;
if (pino == tcp->numpocs) {
if (tcp->POC == 0) {
pi[pino].first = 1;
pi[pino].poc.resno0 = 0;
pi[pino].poc.compno0 = 0;
@@ -154,9 +140,11 @@ pi_iterator_t *pi_create(j2k_image_t * img, j2k_cp_t * cp, int tileno)
return pi;
}
/* <summary> */
/* Get next packet in layer=resolution-component-precinct order. */
/* </summary> */
/* <summary>
* Get next packet in layer-resolution-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_lrcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
@@ -172,17 +160,22 @@ int pi_next_lrcp(pi_iterator_t * pi)
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1;
pi->resno++) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c + pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
@@ -193,9 +186,11 @@ int pi_next_lrcp(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in resolution-layer-component-precinct order. */
/* </summary> */
/* <summary>
* Get next packet in resolution-layer-component-precinct order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rlcp(pi_iterator_t * pi)
{
pi_comp_t *comp;
@@ -209,16 +204,22 @@ int pi_next_rlcp(pi_iterator_t * pi)
}
for (pi->resno = pi->poc.resno0; pi->resno < pi->poc.resno1; pi->resno++) {
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
if (pi->resno >= comp->numresolutions) {
continue;
}
res = &comp->resolutions[pi->resno];
for (pi->precno = 0; pi->precno < res->pw * res->ph; pi->precno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] = 1;
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c + pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
@@ -229,9 +230,11 @@ int pi_next_rlcp(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in resolution-precinct-component-layer order. */
/* </summary> */
/* <summary>
* Get next packet in resolution-precinct-component-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_rpcl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@@ -248,10 +251,10 @@ int pi_next_rpcl(pi_iterator_t * pi)
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx =
comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy =
comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
dx = comp->dx *
(1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy *
(1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
@@ -262,10 +265,11 @@ int pi_next_rpcl(pi_iterator_t * pi)
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
comp = &pi->comps[pi->compno];
@@ -276,6 +280,8 @@ int pi_next_rpcl(pi_iterator_t * pi)
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
@@ -288,17 +294,34 @@ int pi_next_rpcl(pi_iterator_t * pi)
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
@@ -310,9 +333,11 @@ int pi_next_rpcl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in precinct-component-resolution-layer order. */
/* </summary> */
/* <summary>
* Get next packet in precinct-component-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_pcrl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@@ -330,10 +355,10 @@ int pi_next_pcrl(pi_iterator_t * pi)
for (resno = 0; resno < comp->numresolutions; resno++) {
int dx, dy;
res = &comp->resolutions[resno];
dx =
comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
dy =
comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
dx = comp->dx *
(1 << (res->pdx + comp->numresolutions - 1 - resno));
dy = comp->dy *
(1 << (res->pdy + comp->numresolutions - 1 - resno));
pi->dx = !pi->dx ? dx : int_min(pi->dx, dx);
pi->dy = !pi->dy ? dy : int_min(pi->dy, dy);
}
@@ -343,20 +368,23 @@ int pi_next_pcrl(pi_iterator_t * pi)
pi->y += pi->dy - (pi->y % pi->dy)) {
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->compno = pi->poc.compno0; pi->compno < pi->poc.compno1;
pi->compno++) {
for (pi->compno = pi->poc.compno0;
pi->compno < pi->poc.compno1; pi->compno++) {
comp = &pi->comps[pi->compno];
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1, comp->numresolutions);
pi->resno++) {
pi->resno < int_min(pi->poc.resno1,
comp->numresolutions); pi->resno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
@@ -369,17 +397,34 @@ int pi_next_pcrl(pi_iterator_t * pi)
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
@@ -391,9 +436,11 @@ int pi_next_pcrl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet in component-precinct-resolution-layer order. */
/* </summary> */
/* <summary>
* Get next packet in component-precinct-resolution-layer order.
*
* pi: packet iterator to modify
* </summary> */
int pi_next_cprl(pi_iterator_t * pi)
{
pi_comp_t *comp;
@@ -423,16 +470,19 @@ int pi_next_cprl(pi_iterator_t * pi)
for (pi->x = pi->tx0; pi->x < pi->tx1;
pi->x += pi->dx - (pi->x % pi->dx)) {
for (pi->resno = pi->poc.resno0;
pi->resno < int_min(pi->poc.resno1, comp->numresolutions);
pi->resno++) {
pi->resno < int_min(pi->poc.resno1,
comp->numresolutions); pi->resno++) {
int levelno;
int trx0, try0;
int trx1, try1;/* Add antonin pcrl*/
int rpx, rpy;
int prci, prcj;
res = &comp->resolutions[pi->resno];
levelno = comp->numresolutions - 1 - pi->resno;
trx0 = int_ceildiv(pi->tx0, comp->dx << levelno);
try0 = int_ceildiv(pi->ty0, comp->dy << levelno);
trx1 = int_ceildiv(pi->tx1, comp->dx << levelno);/* Add antonin pcrl*/
try1 = int_ceildiv(pi->ty1, comp->dy << levelno);/* Add antonin pcrl*/
rpx = res->pdx + levelno;
rpy = res->pdy + levelno;
if (!
@@ -445,17 +495,34 @@ int pi_next_cprl(pi_iterator_t * pi)
|| (pi->y == pi->ty0 && (try0 << levelno) % (1 << rpx)))) {
continue;
}
/*Add Antonin : sizebug1*/
if ((res->pw==0)||(res->pw==0)) continue;
/*ddA*/
/*Add Antonin : pcrl*/
if ((trx0==trx1)||(try0==try1)) continue;
/*ddA*/
prci =
int_floordivpow2(int_ceildiv(pi->x, comp->dx << levelno),
int_floordivpow2(int_ceildiv
(pi->x, comp->dx << levelno),
res->pdx) - int_floordivpow2(trx0, res->pdx);
prcj =
int_floordivpow2(int_ceildiv(pi->y, comp->dy << levelno),
int_floordivpow2(int_ceildiv
(pi->y, comp->dy << levelno),
res->pdy) - int_floordivpow2(try0, res->pdy);
pi->precno = prci + prcj * res->pw;
for (pi->layno = 0; pi->layno < pi->poc.layno1; pi->layno++) {
if (!pi->include[pi->layno][pi->resno][pi->compno][pi->precno]) {
pi->include[pi->layno][pi->resno][pi->compno][pi->precno] =
1;
if (!pi->
include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p]) {
pi->include[pi->layno * pi->step_l +
pi->resno * pi->step_r +
pi->compno * pi->step_c +
pi->precno * pi->step_p] = 1;
return 1;
}
skip:;
@@ -467,9 +534,11 @@ int pi_next_cprl(pi_iterator_t * pi)
return 0;
}
/* <summary> */
/* Get next packet. */
/* </summary> */
/* <summary>
* Get next packet.
*
* pi: packet iterator to modify
* </summary> */
int pi_next(pi_iterator_t * pi)
{
switch (pi->poc.prg) {

View File

@@ -42,9 +42,10 @@ typedef struct {
} pi_comp_t;
typedef struct {
int include[10][10][3][99];
short int *include; /* precise if the packet has been already used (usefull for progression order change) */
int step_l, step_r, step_c, step_p; /* different steps (layer, resolution, component, precinct) to localize the packet in the include vector */
int compno, resno, precno, layno; /* component, resolution, precinct and layer that indentify the packet */
int first;
int first; /* 0 if the first packet */
j2k_poc_t poc;
int numcomps;
pi_comp_t *comps;

View File

@@ -27,76 +27,28 @@
#include "raw.h"
unsigned char raw_c;
unsigned int raw_ct, raw_lenmax, raw_len;
unsigned char *raw_bp;
unsigned char *raw_start;
unsigned char *raw_end;
unsigned char raw_c; /* temporary buffer where bits are coded or decoded */
unsigned int raw_ct; /* number of bits already read or free to write */
unsigned int raw_lenmax; /* maximum length to decode */
unsigned int raw_len; /* length decoded */
unsigned char *raw_bp; /* pointer to the current position in the buffer */
unsigned char *raw_start; /* pointer to the start of the buffer */
unsigned char *raw_end; /* pointer to the end of the buffer */
/* <summary> */
/* Return the number of bytes already encoded. */
/* </summary> */
/*
* Return the number of bytes already encoded.
*/
int raw_numbytes()
{
return raw_bp - raw_start;
}
/* <summary> */
/* Initialize raw-encoder. */
/* </summary> */
/* <param name="bp">Output buffer.</param> */
void raw_init_enc(unsigned char *bp)
{
raw_bp = bp - 1;
raw_c = 0;
raw_ct = 7;
raw_start = bp;
}
/* <summary> */
/* Encode a symbol using the RAW-coder. */
/* </summary> */
/* <param name="d"> The symbol to be encoded (0 or 1).</param> */
void raw_encode(int d)
{
/* raw_c+=d; */
raw_ct--;
raw_c += (d << raw_ct);
if (raw_ct == 0) {
raw_bp++;
*raw_bp = raw_c;
raw_ct = 7;
if (raw_c == 0xff) {
raw_ct = 6;
}
raw_c = 0;
}
/*else
{
raw_ct--;
raw_c<<=1;
} */
}
/* <summary> */
/* Flush encoded data. */
/* </summary> */
void raw_flush()
{
char first = 1;
int prev = 1;
while (raw_ct != 7) {
raw_encode(first ? 0 : !(prev));
prev = first ? 0 : !(prev);
first = 0;
}
}
/* <summary> */
/* Initialize raw-decoder. */
/* </summary> */
/*
* Initialize raw-decoder.
*
* bp : pointer to the start of the buffer from which the bytes will be read
* len : length of the input buffer
*/
void raw_init_dec(unsigned char *bp, int len)
{
raw_start = bp;
@@ -106,9 +58,9 @@ void raw_init_dec(unsigned char *bp, int len)
raw_ct = 0;
}
/* <summary> */
/* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN */
/* </summary> */
/*
* Decode a symbol using raw-decoder. Cfr p.506 TAUBMAN
*/
int raw_decode()
{
int d;

View File

@@ -33,27 +33,6 @@
*/
int raw_numbytes();
/*
* Initialize the encoder
* bp: pointer to the start of the buffer where the bytes will be written
*/
void raw_init_enc(unsigned char *bp);
/*
* Encode a bit
* d: bit to encode (0 or 1)
*/
void raw_encode(int d);
/*
* Flush the encoder, so that all remaining data is written
*/
void raw_flush();
/*
* Initialize the decoder
* bp: pointer to the start of the buffer from which the bytes will be read
@@ -61,12 +40,9 @@ void raw_flush();
*/
void raw_init_dec(unsigned char *bp, int len);
/*
* Decode a bit (returns 0 or 1)
*/
int raw_decode();
#endif /*
*/
#endif

View File

@@ -92,7 +92,7 @@ static int t1_lut_nmsedec_sig0[1 << T1_NMSEDEC_BITS];
static int t1_lut_nmsedec_ref[1 << T1_NMSEDEC_BITS];
static int t1_lut_nmsedec_ref0[1 << T1_NMSEDEC_BITS];
static int t1_data[T1_MAXCBLKH][T1_MAXCBLKH];
static int t1_data[T1_MAXCBLKH][T1_MAXCBLKW];
static int t1_flags[T1_MAXCBLKH + 2][T1_MAXCBLKH + 2];
int t1_getctxno_zc(int f, int orient)
@@ -158,8 +158,7 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
int *nmsedec, char type, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
v = int_abs(*dp) & one ? 1 : 0;
@@ -188,12 +187,13 @@ void t1_enc_sigpass_step(int *fp, int *dp, int orient, int bpno, int one,
}
}
void t1_dec_sigpass_step(int *fp, int *dp, int orient, int oneplushalf,
char type, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if ((flag & T1_SIG_OTH) && !(flag & (T1_SIG | T1_VISIT))) {
if (type == T1_TYPE_RAW) {
@@ -228,8 +228,9 @@ void t1_enc_sigpass(int w, int h, int bpno, int orient, int *nmsedec,
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, bpno, one, nmsedec, type, vsc);
t1_enc_sigpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i], orient, bpno, one,
nmsedec, type, vsc);
}
}
}
@@ -247,8 +248,13 @@ void t1_dec_sigpass(int w, int h, int bpno, int orient, char type,
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_sigpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, oneplushalf, type, vsc);
t1_dec_sigpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i],
orient,
oneplushalf,
type, vsc);
}
}
}
@@ -258,8 +264,7 @@ void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
char type, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
*nmsedec +=
@@ -276,12 +281,13 @@ void t1_enc_refpass_step(int *fp, int *dp, int bpno, int one, int *nmsedec,
}
}
void t1_dec_refpass_step(int *fp, int *dp, int poshalf, int neghalf,
char type, int vsc)
{
int v, t, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if ((flag & (T1_SIG | T1_VISIT)) == T1_SIG) {
if (type == T1_TYPE_RAW) {
@@ -308,8 +314,8 @@ void t1_enc_refpass(int w, int h, int bpno, int *nmsedec, char type,
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i], bpno,
one, nmsedec, type, vsc);
t1_enc_refpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i], bpno, one, nmsedec, type, vsc);
}
}
}
@@ -327,8 +333,14 @@ void t1_dec_refpass(int w, int h, int bpno, char type, int cblksty)
for (j = k; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_refpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
poshalf, neghalf, type, vsc);
t1_dec_refpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i],
poshalf,
neghalf,
type, vsc);
}
}
}
@@ -338,8 +350,7 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
int *nmsedec, int partial, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if (partial)
goto label_partial;
@@ -361,12 +372,14 @@ void t1_enc_clnpass_step(int *fp, int *dp, int orient, int bpno, int one,
*fp &= ~T1_VISIT;
}
void t1_dec_clnpass_step(int *fp, int *dp, int orient, int oneplushalf,
int partial, int vsc)
{
int v, flag;
flag =
vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
flag = vsc ? ((*fp) & (~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
: (*fp);
if (partial)
goto label_partial;
@@ -402,8 +415,9 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| (t1_flags[1 + k + 3][1 + i] &
(~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
& (T1_SIG | T1_VISIT | T1_SIG_OTH));
(~
(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
} else {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
@@ -438,9 +452,9 @@ void t1_enc_clnpass(int w, int h, int bpno, int orient, int *nmsedec,
for (j = k + runlen; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_enc_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, bpno, one, nmsedec, agg
&& (j == k + runlen), vsc);
t1_enc_clnpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i], orient, bpno, one,
nmsedec, agg && (j == k + runlen), vsc);
}
}
}
@@ -465,8 +479,9 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
i] & (T1_SIG | T1_VISIT |
T1_SIG_OTH)
|| (t1_flags[1 + k + 3][1 + i] &
(~(T1_SIG_S | T1_SIG_SE | T1_SIG_SW | T1_SGN_S)))
& (T1_SIG | T1_VISIT | T1_SIG_OTH));
(~
(T1_SIG_S | T1_SIG_SE | T1_SIG_SW |
T1_SGN_S))) & (T1_SIG | T1_VISIT | T1_SIG_OTH));
} else {
agg = !(t1_flags[1 + k][1 + i] & (T1_SIG | T1_VISIT | T1_SIG_OTH)
|| t1_flags[1 + k + 1][1 +
@@ -496,9 +511,13 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
for (j = k + runlen; j < k + 4 && j < h; j++) {
vsc = ((cblksty & J2K_CCP_CBLKSTY_VSC)
&& (j == k + 3 || j == h - 1)) ? 1 : 0;
t1_dec_clnpass_step(&t1_flags[1 + j][1 + i], &t1_data[j][i],
orient, oneplushalf, agg
&& (j == k + runlen), vsc);
t1_dec_clnpass_step(&t1_flags[1 + j][1 + i],
&t1_data[j][i],
orient,
oneplushalf,
agg && (j == k + runlen), vsc);
}
}
}
@@ -516,24 +535,22 @@ void t1_dec_clnpass(int w, int h, int bpno, int orient, int cblksty)
}
} /* VSC and BYPASS by Antonin */
double t1_getwmsedec(int nmsedec, int compno, int level, int orient,
int bpno, int qmfbid, double stepsize)
double t1_getwmsedec(int nmsedec, int compno, int level, int orient, int bpno, int qmfbid, float stepsize, int numcomps) /*mod fixed_quality*/
{
double w1, w2, wmsedec;
if (qmfbid == 1) {
w1 = mct_getnorm(compno);
w1 = (numcomps > 1) ? mct_getnorm(compno) : 1;
w2 = dwt_getnorm(level, orient);
} else { /* if (qmfbid == 0) */
w1 = mct_getnorm_real(compno);
w1 = (numcomps > 1) ? mct_getnorm_real(compno) : 1;
w2 = dwt_getnorm_real(level, orient);
}
wmsedec = w1 * w2 * stepsize * (1 << bpno);
wmsedec = w1 * w2 * (stepsize / 8192.0) * (1 << bpno);
wmsedec *= wmsedec * nmsedec / 8192.0;
return wmsedec;
}
void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
int qmfbid, double stepsize, int cblksty)
void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level, int qmfbid, float stepsize, int cblksty, int numcomps, tcd_tile_t * tile) /*mod fixed_quality*/
{
int i, j;
int w, h;
@@ -556,8 +573,12 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
cblk->numbps = max ? (int_floorlog2(max) + 1) - T1_NMSEDEC_FRACBITS : 0;
for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
((int *) t1_flags)[i] = 0;
/* Changed by Dmitry Kolyadin */
for (i = 0; i <= w; i++)
for (j = 0; j <= h; j++){
t1_flags[j][i] = 0;
}
bpno = cblk->numbps - 1;
passtype = 2;
@@ -589,28 +610,34 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
break;
}
cumwmsedec +=
t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid,
stepsize);
cumwmsedec += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*mod fixed_quality*/
tile->distotile += t1_getwmsedec(nmsedec, compno, level, orient, bpno, qmfbid, stepsize, numcomps); /*add antonin quality*/
/* Code switch "RESTART" (i.e. TERMALL) */
if ((cblksty & J2K_CCP_CBLKSTY_TERMALL)
&& !((passtype == 2) && (bpno - 1 < 0))) {
if (type == T1_TYPE_RAW)
correction = mqc_bypass_flush_enc();
else
correction = mqc_restart_enc();
if (type == T1_TYPE_RAW) {
mqc_flush();
correction = 1;
/* correction = mqc_bypass_flush_enc(); */
} else { /* correction = mqc_restart_enc(); */
mqc_flush();
correction = 1;
}
pass->term = 1;
} else {
if (((bpno < (cblk->numbps - 4) && (passtype > 0))
|| ((bpno == (cblk->numbps - 4)) && (passtype == 2)))
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) {
if (type == T1_TYPE_RAW)
correction = mqc_bypass_flush_enc();
else
correction = mqc_restart_enc();
if (type == T1_TYPE_RAW) {
mqc_flush();
correction = 1;
/* correction = mqc_bypass_flush_enc(); */
} else { /* correction = mqc_restart_enc(); */
mqc_flush();
correction = 1;
}
pass->term = 1;
} else {
pass->term = 0;
@@ -654,21 +681,28 @@ void t1_encode_cblk(tcd_cblk_t * cblk, int orient, int compno, int level,
void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
int cblksty)
{
int i;
int w, h;
int i, j, w, h;
int bpno, passtype;
int segno, passno;
/* add TONY */
char type = T1_TYPE_MQ;
/* dda */
for (i = 0; i < sizeof(t1_data) / sizeof(int); i++)
((int *) t1_data)[i] = 0;
for (i = 0; i < sizeof(t1_flags) / sizeof(int); i++)
((int *) t1_flags)[i] = 0;
char type = T1_TYPE_MQ; /*BYPASS mode*/
w = cblk->x1 - cblk->x0;
h = cblk->y1 - cblk->y0;
/* Changed by Dmitry Kolyadin */
for (j = 0; j <= h; j++){
for (i = 0; i <= w; i++) {
t1_flags[j][i] = 0;
}
}
/* Changed by Dmitry Kolyadin */
for (i = 0; i < w; i++) {
for (j = 0; j < h; j++){
t1_data[j][i] = 0;
}
}
bpno = roishift + cblk->numbps - 1;
passtype = 2;
@@ -680,7 +714,7 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
for (segno = 0; segno < cblk->numsegs; segno++) {
tcd_seg_t *seg = &cblk->segs[segno];
/* add TONY */
/* Add BYPASS mode */
type = ((bpno <= (cblk->numbps - 1) - 4) && (passtype < 2)
&& (cblksty & J2K_CCP_CBLKSTY_LAZY)) ? T1_TYPE_RAW :
T1_TYPE_MQ;
@@ -688,23 +722,31 @@ void t1_decode_cblk(tcd_cblk_t * cblk, int orient, int roishift,
raw_init_dec(seg->data, seg->len);
else
mqc_init_dec(seg->data, seg->len);
/* dda */
/* ddA */
for (passno = 0; passno < seg->numpasses; passno++) {
switch (passtype) {
case 0:
t1_dec_sigpass(w, h, bpno, orient, type, cblksty);
t1_dec_sigpass(w, h, bpno+1, orient, type, cblksty);
break;
case 1:
t1_dec_refpass(w, h, bpno, type, cblksty);
t1_dec_refpass(w, h, bpno+1, type, cblksty);
break;
case 2:
t1_dec_clnpass(w, h, bpno, orient, cblksty);
t1_dec_clnpass(w, h, bpno+1, orient, cblksty);
break;
}
if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ)
mqc_reset_enc();
if ((cblksty & J2K_CCP_CBLKSTY_RESET) && type == T1_TYPE_MQ) {
mqc_resetstates();
mqc_setstate(T1_CTXNO_UNI, 0, 46);
mqc_setstate(T1_CTXNO_AGG, 0, 3);
mqc_setstate(T1_CTXNO_ZC, 0, 4);
}
if (++passtype == 3) {
passtype = 0;
@@ -724,6 +766,8 @@ void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
tcd_precinct_t *prc;
tcd_cblk_t *cblk;
tile->distotile = 0; /*add fixed_quality*/
for (compno = 0; compno < tile->numcomps; compno++) {
tilec = &tile->comps[compno];
for (resno = 0; resno < tilec->numresolutions; resno++) {
@@ -769,9 +813,11 @@ void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
t1_data[j][i] =
fix_mul(tilec->
data[x + i +
(y + j) * (tilec->x1 - tilec->x0)],
8192 * 8192 / band->stepsize) >> (13 -
T1_NMSEDEC_FRACBITS);
(y + j) * (tilec->x1 -
tilec->
x0)],
8192 * 8192 /
((int) floor(band->stepsize * 8192))) >> (13 - T1_NMSEDEC_FRACBITS);
}
}
}
@@ -781,19 +827,12 @@ void t1_encode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
} else if (orient == 1) {
orient = 2;
}
t1_encode_cblk(cblk, orient, compno,
tilec->numresolutions - 1 - resno,
tcp->tccps[compno].qmfbid, band->stepsize,
tcp->tccps[compno].cblksty);
t1_encode_cblk(cblk, orient, compno, tilec->numresolutions - 1 - resno, tcp->tccps[compno].qmfbid, band->stepsize, tcp->tccps[compno].cblksty, tile->numcomps, tile); /*mod fixed_quality*/
} /* cblkno */
} /* precno */
} /* bandno */
} /* resno */
} /* compo */
}
@@ -817,7 +856,8 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
orient = 1;
else if (orient == 1)
orient = 2;
t1_decode_cblk(cblk, orient, tcp->tccps[compno].roishift,
t1_decode_cblk(cblk, orient,
tcp->tccps[compno].roishift,
tcp->tccps[compno].cblksty);
if (band->bandno == 0) {
x = cblk->x0 - band->x0;
@@ -835,6 +875,7 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
x = pres->x1 - pres->x0 + cblk->x0 - band->x0;
y = pres->y1 - pres->y0 + cblk->y0 - band->y0;
}
if (tcp->tccps[compno].roishift) {
int thresh, val, mag;
thresh = 1 << tcp->tccps[compno].roishift;
@@ -853,20 +894,22 @@ void t1_decode_cblks(tcd_tile_t * tile, j2k_tcp_t * tcp)
if (tcp->tccps[compno].qmfbid == 1) {
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] =
t1_data[j][i];
int tmp=t1_data[j][i];
if (tmp>>1==0) tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
else tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = tmp<0?(tmp>>1)+1:(tmp>>1);
}
}
} else if (tcp->tccps[compno].qmfbid == 0) {
} else { /* if (tcp->tccps[compno].qmfbid == 0) */
for (j = 0; j < cblk->y1 - cblk->y0; j++) {
for (i = 0; i < cblk->x1 - cblk->x0; i++) {
if (t1_data[j][i] == 0) {
tilec->data[x + i +
(y + j) * (tilec->x1 - tilec->x0)] = 0;
double tmp=t1_data[j][i] * band->stepsize * 4096.0;
int tmp2;
if (t1_data[j][i]>>1 == 0) {
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = 0;
} else {
tilec->data[x + i +
(y + j) * (tilec->x1 - tilec->x0)] =
fix_mul(t1_data[j][i] << 13, band->stepsize);
tmp2=((int) (floor(fabs(tmp)))) + ((int) floor(fabs(tmp*2))%2);
tilec->data[x + i + (y + j) * (tilec->x1 - tilec->x0)] = ((tmp<0)?-tmp2:tmp2);
}
}
}
@@ -884,10 +927,9 @@ int t1_init_ctxno_zc(int f, int orient)
n = 0;
h = ((f & T1_SIG_W) != 0) + ((f & T1_SIG_E) != 0);
v = ((f & T1_SIG_N) != 0) + ((f & T1_SIG_S) != 0);
d =
((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) != 0) + ((f & T1_SIG_SE) !=
0) +
((f & T1_SIG_SW) != 0);
d = ((f & T1_SIG_NW) != 0) + ((f & T1_SIG_NE) !=
0) + ((f & T1_SIG_SE) !=
0) + ((f & T1_SIG_SW) != 0);
switch (orient) {
case 2:
t = h;
@@ -950,15 +992,13 @@ int t1_init_ctxno_sc(int f)
{
int hc, vc, n;
n = 0;
hc =
int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
(T1_SIG_E | T1_SGN_E)) +
((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc =
int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
(T1_SIG_N | T1_SGN_N)) +
@@ -999,15 +1039,13 @@ int t1_init_ctxno_mag(int f)
int t1_init_spb(int f)
{
int hc, vc, n;
hc =
int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
hc = int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
T1_SIG_E) + ((f & (T1_SIG_W | T1_SGN_W)) == T1_SIG_W),
1) - int_min(((f & (T1_SIG_E | T1_SGN_E)) ==
(T1_SIG_E | T1_SGN_E)) +
((f & (T1_SIG_W | T1_SGN_W)) ==
(T1_SIG_W | T1_SGN_W)), 1);
vc =
int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
vc = int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
T1_SIG_N) + ((f & (T1_SIG_S | T1_SGN_S)) == T1_SIG_S),
1) - int_min(((f & (T1_SIG_N | T1_SGN_N)) ==
(T1_SIG_N | T1_SGN_N)) +
@@ -1042,7 +1080,7 @@ void t1_init_luts()
t1_lut_spb[i] = t1_init_spb(i << 4);
}
/* FIXME FIXME FIXME */
/* printf("nmsedec luts:\n"); */
/* fprintf(stdout,"nmsedec luts:\n"); */
for (i = 0; i < (1 << T1_NMSEDEC_BITS); i++) {
t = i / pow(2, T1_NMSEDEC_FRACBITS);
u = t;
@@ -1055,8 +1093,9 @@ void t1_init_luts()
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_sig0[i] =
int_max(0,
(int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
(int) (floor
((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
u = t - 1.0;
if (i & (1 << (T1_NMSEDEC_BITS - 1))) {
v = t - 1.5;
@@ -1071,7 +1110,8 @@ void t1_init_luts()
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
t1_lut_nmsedec_ref0[i] =
int_max(0,
(int) (floor((u * u) * pow(2, T1_NMSEDEC_FRACBITS) + 0.5) /
pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
(int) (floor
((u * u) * pow(2, T1_NMSEDEC_FRACBITS) +
0.5) / pow(2, T1_NMSEDEC_FRACBITS) * 8192.0));
}
}

View File

@@ -1,7 +1,7 @@
/*
* Copyright (c) 2001-2002, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2002-2003, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* Copyright (c) 2002-2004, Yannick Verschueren
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -92,6 +92,20 @@ int t2_getnumpasses()
return 37 + bio_read(7);
}
/*
* Encode a packet of a tile to a destination buffer
*
* Tile : the tile for which to write the packets
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
* tileno : number of the tile encoded
*/
int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
int resno, int precno, int layno, unsigned char *dest,
int len, info_image * info_IM, int tileno)
@@ -102,9 +116,6 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = dest;
/* int PPT=tile->PPT, ppt_len=0; */
/* FILE *PPT_file; */
/* <SOP 0xff91> */
if (tcp->csty & J2K_CP_CSTY_SOP) {
sop = (unsigned char *) malloc(6 * sizeof(unsigned char));
@@ -182,8 +193,8 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
|| passno == (cblk->numpasses + layer->numpasses) - 1) {
increment =
int_max(increment,
int_floorlog2(len) + 1 - (cblk->numlenbits +
int_floorlog2(nump)));
int_floorlog2(len) + 1 -
(cblk->numlenbits + int_floorlog2(nump)));
len = 0;
nump = 0;
}
@@ -223,7 +234,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
c += 2;
}
/* </EPH> */
/* } */
/* Writing the packet body */
for (bandno = 0; bandno < res->numbands; bandno++) {
@@ -232,14 +243,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_layer_t *layer = &cblk->layers[layno];
if (!layer->numpasses) { /* ADD for index Cfr. Marcela --> delta disto by packet */
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
info_PK->disto = layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
if (!layer->numpasses) {
continue;
}
if (c + layer->len > dest + len) {
@@ -253,7 +257,7 @@ int t2_encode_packet(tcd_tile_t * tile, j2k_tcp_t * tcp, int compno,
if (info_IM->index_write && info_IM->index_on) {
info_tile *info_TL = &info_IM->tile[tileno];
info_packet *info_PK = &info_TL->packet[info_IM->num];
info_PK->disto = layer->disto;
info_PK->disto += layer->disto;
if (info_IM->D_max < info_PK->disto)
info_IM->D_max = info_PK->disto;
} /* </ADD> */
@@ -278,20 +282,45 @@ void t2_init_seg(tcd_seg_t * seg, int cblksty, int first)
seg->maxpasses = 109;
}
/*
* Decode a packet of a tile from a source buffer
*
* src : the source buffer
* len : the length of the source buffer
* tile : the tile for which to write the packets
* cp : the image coding parameters
* tcp : the tile coding parameters
* compno : Identity of the packet --> component value
* resno : Identity of the packet --> resolution level value
* precno : Identity of the packet --> precinct value
* layno : Identity of the packet --> quality layer value
*/
int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
j2k_tcp_t * tcp, int compno, int resno, int precno,
int layno)
j2k_cp_t * cp, j2k_tcp_t * tcp, int compno, int resno,
int precno, int layno)
{
int bandno, cblkno;
tcd_tilecomp_t *tilec = &tile->comps[compno];
tcd_resolution_t *res = &tilec->resolutions[resno];
unsigned char *c = src;
unsigned char *hd = NULL;
int present;
if (layno == 0) {
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
tgt_reset(prc->incltree);
tgt_reset(prc->imsbtree);
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
@@ -301,19 +330,102 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
}
}
/* SOP markers*/
if (tcp->csty & J2K_CP_CSTY_SOP) {
if ((*c) != 0xff || (*(c + 1) != 0x91)) {
fprintf(stderr,"Warning : expected SOP marker\n");
} else {
c += 6;
}
bio_init_dec(c, src + len - c);
/*TODO : check the Nsop value*/
}
/* When the marker PPT/PPM is used the packet header are store in PPT/PPM marker
This part deal with this caracteristic
step 1: Read packet header in the saved structure
step 2: Return to codestream for decoding */
if (cp->ppm == 1) { /* PPM */
hd = cp->ppm_data;
bio_init_dec(hd, cp->ppm_len); /*Mod Antonin : ppmbug1*/
} else if (tcp->ppt == 1) { /* PPT */
hd = tcp->ppt_data;
bio_init_dec(hd, tcp->ppt_len); /*Mod Antonin : ppmbug1*/
} else { /* Normal Case */
hd = c;
bio_init_dec(hd, src+len-hd);
}
present = bio_read(1);
if (!present) {
bio_inalign();
c += bio_numbytes();
hd += bio_numbytes();
/* EPH markers*/
if (tcp->csty & J2K_CP_CSTY_EPH) {
if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
printf("Error : expected EPH marker\n");
} else {
hd += 2;
}
}
if (cp->ppm == 1) { /* PPM case */
cp->ppm_len+=cp->ppm_data-hd;
cp->ppm_data = hd;
return c - src;
}
if (tcp->ppt == 1) { /* PPT case */
tcp->ppt_len+=tcp->ppt_data-hd;
tcp->ppt_data = hd;
return c - src;
}
return hd - src;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
int included, increment, n;
tcd_cblk_t *cblk = &prc->cblks[cblkno];
@@ -368,20 +480,62 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
}
if (bio_inalign())
return -999;
c += bio_numbytes();
hd += bio_numbytes();
/* EPH markers*/
if (tcp->csty & J2K_CP_CSTY_EPH) {
c += 2;
if ((*hd) != 0xff || (*(hd + 1) != 0x92)) {
fprintf(stderr,"Error : expected EPH marker\n");
} else {
hd += 2;
}
}
if (cp->ppm==1) {
cp->ppm_len+=cp->ppm_data-hd;
cp->ppm_data = hd;
} else if (tcp->ppt == 1) {
tcp->ppt_len+=tcp->ppt_data-hd;
tcp->ppt_data = hd;
} else {
c=hd;
}
for (bandno = 0; bandno < res->numbands; bandno++) {
tcd_band_t *band = &res->bands[bandno];
tcd_precinct_t *prc = &band->precincts[precno];
/*Add Antonin : sizebug1*/
if ((band->x1-band->x0 == 0)||(band->y1-band->y0 == 0)) continue;
/*ddA*/
for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
tcd_cblk_t *cblk = &prc->cblks[cblkno];
tcd_seg_t *seg;
if (!cblk->numnewpasses)
continue;
if (!cblk->numsegs) {
seg = &cblk->segs[cblk->numsegs++];
seg = &cblk->segs[0];
cblk->numsegs++;
cblk->len = 0;
} else {
seg = &cblk->segs[cblk->numsegs - 1];
@@ -391,8 +545,10 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
}
}
do {
if (c + seg->newlen > src + len)
if (c + seg->newlen > src + len) {
return -999;
}
memcpy(cblk->data + cblk->len, c, seg->newlen);
if (seg->numpasses == 0) {
seg->data = cblk->data + cblk->len;
@@ -413,6 +569,20 @@ int t2_decode_packet(unsigned char *src, int len, tcd_tile_t * tile,
return c - src;
}
/*
* Encode the packets of a tile to a destination buffer
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
unsigned char *dest, int len, info_image * info_IM)
@@ -427,10 +597,10 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
if (pi[pino].layno < maxlayers) {
e =
t2_encode_packet(tile, &cp->tcps[tileno], pi[pino].compno,
pi[pino].resno, pi[pino].precno, pi[pino].layno,
c, dest + len - c, info_IM, tileno);
e = t2_encode_packet(tile, &cp->tcps[tileno],
pi[pino].compno, pi[pino].resno,
pi[pino].precno, pi[pino].layno, c,
dest + len - c, info_IM, tileno);
if (e == -999) {
break;
} else
@@ -464,6 +634,7 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
}
free(pi[pino].comps);
}
free(pi[0].include);
free(pi);
if (e == -999)
return e;
@@ -471,33 +642,64 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
return c - dest;
}
/*
* Decode the packets of a tile from a source buffer
*
* src: the source buffer
* len: length of the source buffer
* img: destination image
* cp: image coding parameters
* tileno: number that identifies the tile for which to decode the packets
* tile: tile for which to decode the packets
*/
int t2_decode_packets(unsigned char *src, int len, j2k_image_t * img,
j2k_cp_t * cp, int tileno, tcd_tile_t * tile)
{
unsigned char *c = src;
pi_iterator_t *pi;
int pino, compno, e = 0;
int n = 0;
pi = pi_create(img, cp, tileno);
for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
while (pi_next(&pi[pino])) {
e =
t2_decode_packet(c, src + len - c, tile, &cp->tcps[tileno],
pi[pino].compno, pi[pino].resno, pi[pino].precno,
if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
e = t2_decode_packet(c, src + len - c, tile, cp,
&cp->tcps[tileno], pi[pino].compno,
pi[pino].resno, pi[pino].precno,
pi[pino].layno);
} else {
e = 0;
}
/* progression in resolution */
img->comps[pi[pino].compno].resno_decoded =
e > 0 ? int_max(pi[pino].resno,
img->comps[pi[pino].compno].
resno_decoded) : img->comps[pi[pino].
compno].resno_decoded;
n++;
if (e == -999) { /* ADD */
break;
} else
} else {
c += e;
}
}
/* FREE space memory taken by pi */
for (compno = 0; compno < pi[pino].numcomps; compno++) {
free(pi[pino].comps[compno].resolutions);
}
free(pi[pino].comps);
}
free(pi[0].include);
free(pi);
if (e == -999)
return e;
else

View File

@@ -34,11 +34,15 @@
/*
* Encode the packets of a tile to a destination buffer
*
* img : the source image
* cp : the image coding parameters
* tileno : number of the tile encoded
* tile : the tile for which to write the packets
* maxlayers : maximum number of layers
* dest : the destination buffer
* len : the length of the destination buffer
* info_IM : structure to create an index file
*/
int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
tcd_tile_t * tile, int maxlayers,
@@ -46,6 +50,7 @@ int t2_encode_packets(j2k_image_t * img, j2k_cp_t * cp, int tileno,
/*
* Decode the packets of a tile from a source buffer
*
* src: the source buffer
* len: length of the source buffer
* img: destination image

File diff suppressed because it is too large Load Diff

View File

@@ -48,86 +48,105 @@ typedef struct {
} tcd_pass_t;
typedef struct {
int numpasses;
int len;
int numpasses; /* Number of passes in the layer */
int len; /* len of information */
double disto; /* add for index (Cfr. Marcela) */
unsigned char *data;
unsigned char *data; /* data */
} tcd_layer_t;
typedef struct {
int x0, y0, x1, y1;
int x0, y0, x1, y1; /* dimension of the code-blocks : left upper corner (x0, y0) right low corner (x1,y1) */
int numbps;
int numlenbits;
int len;
int numpasses;
int numnewpasses;
int numsegs;
tcd_seg_t segs[100];
unsigned char data[8192];
int numpassesinlayers;
tcd_layer_t layers[100];
int totalpasses;
tcd_pass_t passes[100];
int len; /* length */
int numpasses; /* number of pass already done for the code-blocks */
int numnewpasses; /* number of pass added to the code-blocks */
int numsegs; /* number of segments */
tcd_seg_t segs[100]; /* segments informations */
unsigned char data[8192]; /* Data */
int numpassesinlayers; /* number of passes in the layer */
tcd_layer_t layers[100]; /* layer information */
int totalpasses; /* total number of passes */
tcd_pass_t passes[100]; /* information about the passes */
} tcd_cblk_t;
typedef struct {
int x0, y0, x1, y1;
int cw, ch;
tcd_cblk_t *cblks;
tgt_tree_t *incltree;
tgt_tree_t *imsbtree;
int x0, y0, x1, y1; /* dimension of the precinct : left upper corner (x0, y0) right low corner (x1,y1) */
int cw, ch; /* number of precinct in width and heigth */
tcd_cblk_t *cblks; /* code-blocks informations */
tgt_tree_t *incltree; /* inclusion tree */
tgt_tree_t *imsbtree; /* IMSB tree */
} tcd_precinct_t;
typedef struct {
int x0, y0, x1, y1;
int x0, y0, x1, y1; /* dimension of the subband : left upper corner (x0, y0) right low corner (x1,y1) */
int bandno;
tcd_precinct_t *precincts;
tcd_precinct_t *precincts; /* precinct information */
int numbps;
int stepsize;
float stepsize;
} tcd_band_t;
typedef struct {
int x0, y0, x1, y1;
int previous_x0, previous_y0, previous_x1, previous_y1; /* usefull for the DWT */
int cas_col, cas_row; /* usefull for the DWT */
int pw, ph; /* , old_pw,old_ph, old_pw_max,old_ph_max; */
int numbands;
tcd_band_t bands[3];
int x0, y0, x1, y1; /* dimension of the resolution level : left upper corner (x0, y0) right low corner (x1,y1) */
int pw, ph;
int numbands; /* number sub-band for the resolution level */
tcd_band_t bands[3]; /* subband information */
} tcd_resolution_t;
typedef struct {
int x0, y0, x1, y1;
int previous_row, previous_col; /* usefull for the DWT */
int numresolutions;
tcd_resolution_t *resolutions;
int *data;
int x0, y0, x1, y1; /* dimension of component : left upper corner (x0, y0) right low corner (x1,y1) */
int numresolutions; /* number of resolutions level */
tcd_resolution_t *resolutions; /* resolutions information */
int *data; /* data of the component */
int nbpix; /* add fixed_quality */
} tcd_tilecomp_t;
typedef struct {
int x0, y0, x1, y1;
int numcomps;
/* int PPT; */
/* int len_ppt; */
tcd_tilecomp_t *comps;
int x0, y0, x1, y1; /* dimension of the tile : left upper corner (x0, y0) right low corner (x1,y1) */
int numcomps; /* number of components in tile */
tcd_tilecomp_t *comps; /* Components information */
int nbpix; /* add fixed_quality */
double distotile; /* add fixed_quality */
double distolayer[100]; /* add fixed_quality */
} tcd_tile_t;
typedef struct {
int tw, th;
tcd_tile_t *tiles;
int tw, th; /* number of tiles in width and heigth */
tcd_tile_t *tiles; /* Tiles information */
} tcd_image_t;
/*
* Initialize the tile coder/decoder
* Initialize the tile coder (reuses the memory allocated by tcd_malloc_encode)
* img: raw image
* cp: coding parameters
* info_IM: creation of index file
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_init_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile coder (allocate the memory)
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_malloc_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
* Initialize the tile decoder
* img: raw image
* cp: coding parameters
*/
void tcd_init(j2k_image_t * img, j2k_cp_t * cp);
/*
* Free the memory allocated for encoding
* img: raw image
* cp: coding parameters
* curtileno : number that identifies the tile that will be encoded
*/
void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
/*
@@ -135,15 +154,18 @@ void tcd_free_encode(j2k_image_t * img, j2k_cp_t * cp, int curtileno);
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
* info_IM: creation of index file
*/
int tcd_encode_tile_pxm(int tileno, unsigned char *dest, int len,
info_image * info_IM);
/*
* Encode a tile from the raw image into a buffer, format pgx
* tileno: number that identifies one of the tiles to be encoded
* dest: destination buffer
* len: length of destination buffer
* info_IM: creation of index file
*/
int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
info_image * info_IM);
@@ -156,4 +178,6 @@ int tcd_encode_tile_pgx(int tileno, unsigned char *dest, int len,
*/
int tcd_decode_tile(unsigned char *src, int len, int tileno);
void tcd_dec_release();
#endif

View File

@@ -41,6 +41,14 @@ typedef struct {
tgt_node_t *nodes;
} tgt_tree_t;
/*
* Reset a tag-tree (set all leaves to 0)
* tree: tag-tree to reset
*/
void tgt_reset(tgt_tree_t * tree);
/*
* Create a tag-tree
* numleafsh: width of the array of leafs of the tree
@@ -48,12 +56,6 @@ typedef struct {
*/
tgt_tree_t *tgt_create(int numleafsh, int numleafsv);
/*
* Reset a tag-tree (set all leafs to 0)
* tree: tag-tree to reset
*/
void tgt_reset(tgt_tree_t * tree);
/*
* Destroy a tag-tree, liberating memory
* tree: tag-tree to destroy

128
mj2/MJ2_Extractor.dsp Normal file
View File

@@ -0,0 +1,128 @@
# Microsoft Developer Studio Project File - Name="MJ2_Extractor" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=MJ2_Extractor - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "MJ2_Extractor.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "MJ2_Extractor.mak" CFG="MJ2_Extractor - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "MJ2_Extractor - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "MJ2_Extractor - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "MJ2_Extractor - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "MJ2_Extractor - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "MJ2_Extractor___Win32_Debug"
# PROP BASE Intermediate_Dir "MJ2_Extractor___Win32_Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "MJ2_Extractor___Win32_Debug"
# PROP Intermediate_Dir "MJ2_Extractor___Win32_Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "MJ2_Extractor - Win32 Release"
# Name "MJ2_Extractor - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=.\extract_j2k_from_mj2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

136
mj2/MJ2_Wrapper.dsp Normal file
View File

@@ -0,0 +1,136 @@
# Microsoft Developer Studio Project File - Name="MJ2_Wrapper" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=MJ2_Wrapper - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "MJ2_Wrapper.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "MJ2_Wrapper.mak" CFG="MJ2_Wrapper - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "MJ2_Wrapper - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "MJ2_Wrapper - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "MJ2_Wrapper - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "MJ2_Wrapper - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "MJ2_Wrapper___Win32_Debug"
# PROP BASE Intermediate_Dir "MJ2_Wrapper___Win32_Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "MJ2_Wrapper___Win32_Debug"
# PROP Intermediate_Dir "MJ2_Wrapper___Win32_Debug"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "MJ2_Wrapper - Win32 Release"
# Name "MJ2_Wrapper - Win32 Debug"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# Begin Source File
SOURCE=.\wrap_j2k_in_mj2.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project

79
mj2/Makefile Normal file
View File

@@ -0,0 +1,79 @@
#
# Valentin Mesaros (Octalis | DICE/UCL Belgium)
# 19 April 2005
#
# Makefile for mj2 source files (frames_to_mj2, mj2_to_frames, extract_j2k_from_mj2, wrap_j2k_in_mj2)
#
INCLUDES = -Imj2 \
-I../libopenjpeg
C_OPT = -Wall
CFLAGS = $(INCLUDES) $(C_OPT)
CC = gcc
OBJ_MJ2_DIR = obj
BIN_MJ2_DIR = bin
LIB_OPENJPEG_DIR = ../libopenjpeg/lib
LIB_OPENJPEG = $(LIB_OPENJPEG_DIR)/libopenjpeg.a
all: $(OBJ_MJ2_DIR) $(BIN_MJ2_DIR) \
$(BIN_MJ2_DIR)/frames_to_mj2 \
$(BIN_MJ2_DIR)/mj2_to_frames \
$(BIN_MJ2_DIR)/extract_j2k_from_mj2 \
$(BIN_MJ2_DIR)/wrap_j2k_in_mj2
$(OBJ_MJ2_DIR):
mkdir $(OBJ_MJ2_DIR)
$(BIN_MJ2_DIR):
mkdir $(BIN_MJ2_DIR)
$(OBJ_MJ2_DIR)/%.o:
$(CC) -c $(CFLAGS) -o $@ $<
$(OBJ_MJ2_DIR)/frames_to_mj2.o: frames_to_mj2.c
$(OBJ_MJ2_DIR)/mj2_to_frames.o: mj2_to_frames.c
$(OBJ_MJ2_DIR)/mj2.o: mj2.c
$(OBJ_MJ2_DIR)/mj2_convert.o: mj2_convert.c
$(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o: extract_j2k_from_mj2.c
$(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o: wrap_j2k_in_mj2.c
MJ2_OBJS = $(addprefix $(OBJ_MJ2_DIR)/, mj2.o mj2_convert.o)
$(BIN_MJ2_DIR)/frames_to_mj2: $(OBJ_MJ2_DIR)/frames_to_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/frames_to_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/frames_to_mj2.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/mj2_to_frames: $(OBJ_MJ2_DIR)/mj2_to_frames.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/mj2_to_frames \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/mj2_to_frames.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/extract_j2k_from_mj2: $(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/extract_j2k_from_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/extract_j2k_from_mj2.o \
$(LIB_OPENJPEG) \
-lm
$(BIN_MJ2_DIR)/wrap_j2k_in_mj2: $(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o $(MJ2_OBJS) $(LIB_OPENJPEG)
$(CC) $(CFLAGS) \
-o $(BIN_MJ2_DIR)/wrap_j2k_in_mj2 \
$(MJ2_OBJS) $(OBJ_MJ2_DIR)/wrap_j2k_in_mj2.o \
$(LIB_OPENJPEG) \
-lm
clean:
rm -f $(BIN_MJ2_DIR)/* $(OBJ_MJ2_DIR)/*

116
mj2/compat/getopt.c Normal file
View File

@@ -0,0 +1,116 @@
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* last review : october 29th, 2002 */
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int opterr = 1, /* if error message should be printed */
optind = 1, /* index into parent argv vector */
optopt, /* character checked for validity */
optreset; /* reset getopt */
char *optarg; /* argument associated with option */
#define BADCH (int)'?'
#define BADARG (int)':'
#define EMSG ""
/*
* getopt --
* Parse argc/argv argument vector.
*/
int getopt(nargc, nargv, ostr)
int nargc;
char *const *nargv;
const char *ostr;
{
# define __progname nargv[0]
static char *place = EMSG; /* option letter processing */
char *oli; /* option letter list index */
if (optreset || !*place) { /* update scanning pointer */
optreset = 0;
if (optind >= nargc || *(place = nargv[optind]) != '-') {
place = EMSG;
return (-1);
}
if (place[1] && *++place == '-') { /* found "--" */
++optind;
place = EMSG;
return (-1);
}
} /* option letter okay? */
if ((optopt = (int) *place++) == (int) ':' ||
!(oli = strchr(ostr, optopt))) {
/*
* if the user didn't specify '-' as an option,
* assume it means -1.
*/
if (optopt == (int) '-')
return (-1);
if (!*place)
++optind;
if (opterr && *ostr != ':')
(void) fprintf(stderr,
"%s: illegal option -- %c\n", __progname, optopt);
return (BADCH);
}
if (*++oli != ':') { /* don't need argument */
optarg = NULL;
if (!*place)
++optind;
} else { /* need an argument */
if (*place) /* no white space */
optarg = place;
else if (nargc <= ++optind) { /* no arg */
place = EMSG;
if (*ostr == ':')
return (BADARG);
if (opterr)
(void) fprintf(stderr,
"%s: option requires an argument -- %c\n",
__progname, optopt);
return (BADCH);
} else /* white space */
optarg = nargv[optind];
place = EMSG;
++optind;
}
return (optopt); /* dump back option letter */
}

14
mj2/compat/getopt.h Normal file
View File

@@ -0,0 +1,14 @@
/* last review : october 29th, 2002 */
#ifndef _GETOPT_H_
#define _GETOPT_H_
extern int opterr;
extern int optind;
extern int optopt;
extern int optreset;
extern char *optarg;
extern int getopt(int nargc, char *const *nargv, const char *ostr);
#endif /* _GETOPT_H_ */

View File

@@ -0,0 +1,81 @@
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include "mj2.h"
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
int main(int argc, char *argv[]) {
int tnum;
unsigned int snum;
mj2_movie_t movie;
mj2_tk_t *track;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *file, *outfile;
char outfilename[50];
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_extractor mj2filename output_location\n");
printf("Example: MJ2_extractor foreman.mj2 output/foreman\n");
return 1;
}
file = fopen(argv[1], "rb");
if (!file) {
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
return 1;
}
if (mj2_read_struct(file, &movie)) // Creating the movie structure
return 1;
// Decode first video track
tnum = 0;
while (movie.tk[tnum].track_type != 0)
tnum ++;
track = &movie.tk[tnum];
fprintf(stdout,"Extracting %d frames from file...\n",track->num_samples);
for (snum=0; snum < track->num_samples; snum++)
{
sample = &track->sample[snum];
frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
fseek(file,sample->offset+8,SEEK_SET);
fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
sprintf(outfilename,"%s_%d.j2k",argv[2],snum);
outfile = fopen(outfilename, "wb");
if (!outfile) {
fprintf(stderr, "failed to open %s for writing\n",outfilename);
return 1;
}
fwrite(frame_codestream,sample->sample_size-8,1,outfile);
fclose(outfile);
free(frame_codestream);
}
fclose(file);
fprintf(stdout, "%d frames correctly extracted\n", snum);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

997
mj2/frames_to_mj2.c Normal file
View File

@@ -0,0 +1,997 @@
/*
* Copyright (c) 2003-2004, Fran<61>ois-Olivier Devaux
* Copyright (c) 2002-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <openjpeg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <setjmp.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
#else
#include "compat/getopt.h"
#endif
#include "mj2_convert.h"
#define MJ2_MDAT 0x6d646174
#define JP2_JP2C 0x6a703263
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
void help_display()
{
fprintf(stdout,"HELP\n----\n\n");
fprintf(stdout,"- the -h option displays this help information on screen\n\n");
fprintf(stdout,"List of parameters for the MJ2 encoder:\n");
fprintf(stdout,"\n");
fprintf(stdout,"REMARKS:\n");
fprintf(stdout,"---------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
fprintf
(stdout,"COD and QCD never appear in the tile_header.\n");
fprintf(stdout,"\n");
fprintf
(stdout,"- This coder can encode a mega image, a test was made on a 24000x24000 pixels \n");
fprintf
(stdout,"color image. You need enough disk space memory (twice the original) to encode \n");
fprintf
(stdout,"the image,i.e. for a 1.5 GB image you need a minimum of 3GB of disk memory)\n");
fprintf(stdout,"\n");
fprintf(stdout,"By default:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf(stdout," * Lossless\n");
fprintf(stdout," * 1 tile\n");
fprintf(stdout," * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
fprintf(stdout," * Size of code-block : 64 x 64\n");
fprintf(stdout," * Number of resolutions: 6\n");
fprintf(stdout," * No SOP marker in the codestream\n");
fprintf(stdout," * No EPH marker in the codestream\n");
fprintf(stdout," * No sub-sampling in x or y direction\n");
fprintf(stdout," * No mode switch activated\n");
fprintf(stdout," * Progression order: LRCP\n");
fprintf(stdout," * No index file\n");
fprintf(stdout," * No ROI upshifted\n");
fprintf(stdout," * No offset of the origin of the image\n");
fprintf(stdout," * No offset of the origin of the tiles\n");
fprintf(stdout," * Reversible DWT 5-3\n");
fprintf(stdout,"\n");
fprintf(stdout,"Parameters:\n");
fprintf(stdout,"------------\n");
fprintf(stdout,"\n");
fprintf
(stdout,"Required Parameters (except with -h):\n");
fprintf
(stdout,"-i : source file (-i source.yuv) \n");
fprintf
(stdout,"-o : destination file (-o dest.mj2) \n");
fprintf
(stdout,"Optional Parameters:\n");
fprintf(stdout,"-h : display the help information \n");
fprintf(stdout,"-r : different compression ratios for successive layers (-r 20,10,5)\n ");
fprintf(stdout," - The rate specified for each quality level is the desired \n");
fprintf(stdout," compression factor.\n");
fprintf(stdout," Example: -r 20,10,1 means quality 1: compress 20x, \n");
fprintf(stdout," quality 2: compress 10x and quality 3: compress lossless\n");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-q : different psnr for successive layers (-q 30,40,50) \n ");
fprintf(stdout," (options -r and -q cannot be used together)\n ");
fprintf(stdout,"-n : number of resolutions (-n 3) \n");
fprintf(stdout,"-b : size of code block (-b 32,32) \n");
fprintf(stdout,"-c : size of precinct (-c 128,128) \n");
fprintf(stdout,"-t : size of tile (-t 512,512) \n");
fprintf
(stdout,"-p : progression order (-p LRCP) [LRCP, RLCP, RPCL, PCRL, CPRL] \n");
fprintf
(stdout,"-s : subsampling factor (-s 2,2) [-s X,Y] \n");
fprintf(stdout," Remark: subsampling bigger than 2 can produce error\n");
fprintf
(stdout,"-SOP : write SOP marker before each packet \n");
fprintf
(stdout,"-EPH : write EPH marker after each header packet \n");
fprintf
(stdout,"-M : mode switch (-M 3) [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
fprintf
(stdout," 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)] \n");
fprintf
(stdout," Indicate multiple modes by adding their values. \n");
fprintf
(stdout," ex: RESTART(4) + RESET(2) + SEGMARK(32) = -M 38\n");
fprintf
(stdout,"-x : create an index file *.Idx (-x index_name.Idx) \n");
fprintf
(stdout,"-ROI : c=%%d,U=%%d : quantization indices upshifted \n");
fprintf
(stdout," for component c=%%d [%%d = 0,1,2]\n");
fprintf
(stdout," with a value of U=%%d [0 <= %%d <= 37] (i.e. -ROI:c=0,U=25) \n");
fprintf
(stdout,"-d : offset of the origin of the image (-d 150,300) \n");
fprintf
(stdout,"-T : offset of the origin of the tiles (-T 100,75) \n");
fprintf(stdout,"-I : use the irreversible DWT 9-7 (-I) \n");
fprintf(stdout,"-W : image width, height and the dx and dy subsampling \n");
fprintf(stdout," of the Cb and Cr components for YUV files \n");
fprintf(stdout," (default is '352,288,2,2' for CIF format's 352x288 and 4:2:0)\n");
fprintf(stdout,"-F : video frame rate (set to 25 by default)\n");
fprintf(stdout,"\n");
fprintf(stdout,"IMPORTANT:\n");
fprintf(stdout,"-----------\n");
fprintf(stdout,"\n");
fprintf(stdout,"The index file has the structure below:\n");
fprintf(stdout,"---------------------------------------\n");
fprintf(stdout,"\n");
fprintf(stdout,"Image_height Image_width\n");
fprintf(stdout,"progression order\n");
fprintf(stdout,"Tiles_size_X Tiles_size_Y\n");
fprintf(stdout,"Components_nb\n");
fprintf(stdout,"Layers_nb\n");
fprintf(stdout,"decomposition_levels\n");
fprintf(stdout,"[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
fprintf(stdout," [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
fprintf(stdout,"Main_header_end_position\n");
fprintf(stdout,"Codestream_size\n");
fprintf(stdout,"Tile_0 start_pos end_Theader end_pos TotalDisto NumPix MaxMSE\n");
fprintf(stdout,"Tile_1 '' '' '' '' '' ''\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tile_Nt '' '' '' '' '' ''\n");
fprintf(stdout,"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
fprintf(stdout,"...\n");
fprintf(stdout,"Tpacket_Np '' '' '' '' '' '' '' ''\n");
fprintf(stdout,"MaxDisto\n");
fprintf(stdout,"TotalDisto\n\n");
}
int give_progression(char progression[4])
{
if (progression[0] == 'L' && progression[1] == 'R'
&& progression[2] == 'C' && progression[3] == 'P') {
return 0;
} else {
if (progression[0] == 'R' && progression[1] == 'L'
&& progression[2] == 'C' && progression[3] == 'P') {
return 1;
} else {
if (progression[0] == 'R' && progression[1] == 'P'
&& progression[2] == 'C' && progression[3] == 'L') {
return 2;
} else {
if (progression[0] == 'P' && progression[1] == 'C'
&& progression[2] == 'R' && progression[3] == 'L') {
return 3;
} else {
if (progression[0] == 'C' && progression[1] == 'P'
&& progression[2] == 'R' && progression[3] == 'L') {
return 4;
} else {
return -1;
}
}
}
}
}
}
double dwt_norms_97[4][10] = {
{1.000, 1.965, 4.177, 8.403, 16.90, 33.84, 67.69, 135.3, 270.6, 540.9},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.022, 3.989, 8.355, 17.04, 34.27, 68.63, 137.3, 274.6, 549.0},
{2.080, 3.865, 8.307, 17.18, 34.71, 69.59, 139.3, 278.6, 557.2}
};
int floorlog2(int a)
{
int l;
for (l = 0; a > 1; l++) {
a >>= 1;
}
return l;
}
void encode_stepsize(int stepsize, int numbps, int *expn, int *mant)
{
int p, n;
p = floorlog2(stepsize) - 13;
n = 11 - floorlog2(stepsize);
*mant = (n < 0 ? stepsize >> -n : stepsize << n) & 0x7ff;
*expn = numbps - p;
}
void calc_explicit_stepsizes(j2k_tccp_t * tccp, int prec)
{
int numbands, bandno;
numbands = 3 * tccp->numresolutions - 2;
for (bandno = 0; bandno < numbands; bandno++) {
double stepsize;
int resno, level, orient, gain;
resno = bandno == 0 ? 0 : (bandno - 1) / 3 + 1;
orient = bandno == 0 ? 0 : (bandno - 1) % 3 + 1;
level = tccp->numresolutions - 1 - resno;
gain =
tccp->qmfbid == 0 ? 0 : (orient ==
0 ? 0 : (orient == 1
|| orient == 2 ? 1 : 2));
if (tccp->qntsty == J2K_CCP_QNTSTY_NOQNT) {
stepsize = 1.0;
} else {
double norm = dwt_norms_97[orient][level];
stepsize = (1 << (gain + 1)) / norm;
}
encode_stepsize((int) floor(stepsize * 8192.0), prec + gain,
&tccp->stepsizes[bandno].expn,
&tccp->stepsizes[bandno].mant);
}
}
int main(int argc, char **argv)
{
int NumResolution, numD_min; /* NumResolution : number of resolution */
int Tile_arg; /* Tile_arg = 0 (not in argument) ou = 1 (in argument) */
int CSty; /* CSty : coding style */
int Prog_order; /* progression order (default LRCP) */
char progression[4];
int numpocs, numpocs_tile; /* Number of progression order change (POC) default 0 */
int prcw_init[J2K_MAXRLVLS]; /* Initialisation Precinct width */
int prch_init[J2K_MAXRLVLS]; /* Initialisation Precinct height */
//int prcw_init, prch_init; /* Initialisation precincts' size */
int cblockw_init, cblockh_init; /* Initialisation codeblocks' size */
int mode, value; /* Mode switch (cblk_style) */
int subsampling_dx, subsampling_dy; /* subsampling value for dx and dy */
int ROI_compno, ROI_shift; /* region of interrest */
int Dim[2]; /* portion of the image coded */
int TX0, TY0; /* tile off-set */
mj2_movie_t movie;
j2k_cp_t cp, cp_init; /* cp_init is used to initialise in multiple tiles */
j2k_tcp_t *tcp, *tcp_init; /* tcp_init is used to initialise in multiple tile */
j2k_poc_t POC[32]; /* POC : used in case of Progression order change */
j2k_poc_t *tcp_poc;
j2k_tccp_t *tccp;
int i, tileno, l, j;
char *infile = 0;
char *outfile = 0;
char *index = 0;
char *s, S1, S2, S3;
char *buf;
int ir = 0;
int res_spec = 0; /* For various precinct sizes specification */
char sep;
int w; /* Width of YUV file */
int h; /* Height of YUV file */
int CbCr_subsampling_dx; /* Sample rate of YUV 4:4:4 4:2:2 or 4:2:0 */
int CbCr_subsampling_dy; /* Sample rate of YUV 4:4:4 4:2:2 or 4:2:0 */
int frame_rate; /* Video Frame Rate */
int numcomps; /* In YUV files, numcomps always considered as 3 */
int prec; /* In YUV files, precision always considered as 8 */
int x1, y1, len, jp2c_initpos, m, k, pos;
long mdat_initpos, offset;
FILE *mj2file, *yuvfile;
unsigned int sampleno;
j2k_image_t img;
/* default value */
/* ------------- */
NumResolution = 6;
CSty = 0;
cblockw_init = 64;
cblockh_init = 64;
cp.tw = 1;
cp.th = 1;
cp.index_on = 0;
Prog_order = 0;
numpocs = 0;
mode = 0;
subsampling_dx = 1;
subsampling_dy = 1;
ROI_compno = -1; /* no ROI */
ROI_shift = 0;
Dim[0] = 0;
Dim[1] = 0;
TX0 = 0;
TY0 = 0;
cp.comment = NULL;
cp.disto_alloc = 0;
cp.fixed_alloc = 0;
cp.fixed_quality = 0; //add fixed_quality
w = 352; // CIF default value
h = 288; // CIF default value
CbCr_subsampling_dx = 2; // CIF default value
CbCr_subsampling_dy = 2; // CIF default value
frame_rate = 25;
Tile_arg = 0;
cp_init.tcps = (j2k_tcp_t *) malloc(sizeof(j2k_tcp_t)); /* initialisation if only one tile */
tcp_init = &cp_init.tcps[0];
tcp_init->numlayers = 0;
cp.intermed_file = 0; // Don't store each tile in a file during processing
while (1) {
int c = getopt(argc, argv,
"i:o:r:q:f:t:n:c:b:x:p:s:d:h:P:S:E:M:R:T:C:I:W:F:");
if (c == -1)
break;
switch (c) {
case 'i': /* IN fill */
infile = optarg;
s = optarg;
while (*s) {
s++;
}
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'y' && S2 == 'u' && S3 == 'v')
|| (S1 == 'Y' && S2 == 'U' && S3 == 'V')) {
cp.decod_format = YUV_DFMT;
break;
}
fprintf(stderr,
"!! Unrecognized format for infile : %c%c%c [accept only *.yuv] !!\n\n",
S1, S2, S3);
return 1;
break;
/* ----------------------------------------------------- */
case 'o': /* OUT fill */
outfile = optarg;
while (*outfile) {
outfile++;
}
outfile--;
S3 = *outfile;
outfile--;
S2 = *outfile;
outfile--;
S1 = *outfile;
outfile = optarg;
if ((S1 == 'm' && S2 == 'j' && S3 == '2')
|| (S1 == 'M' && S2 == 'J' && S3 == '2'))
cp.cod_format = MJ2_CFMT;
else {
fprintf(stderr,
"Unknown output format image *.%c%c%c [only *.mj2]!! \n",
S1, S2, S3);
return 1;
}
break;
/* ----------------------------------------------------- */
case 'r': /* rates rates/distorsion */
s = optarg;
while (sscanf(s, "%d", &tcp_init->rates[tcp_init->numlayers])
== 1) {
tcp_init->numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
cp.disto_alloc = 1;
cp.matrice = NULL;
break;
/* ----------------------------------------------------- */
case 'q': /* add fixed_quality */
s = optarg;
while (sscanf
(s, "%f", &tcp_init->distoratio[tcp_init->numlayers]) == 1) {
tcp_init->numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s)
break;
s++;
}
cp.fixed_quality = 1;
cp.matrice = NULL;
break;
/* dda */
/* ----------------------------------------------------- */
case 'f': /* mod fixed_quality (before : -q) */
s = optarg;
sscanf(s, "%d", &tcp_init->numlayers);
s++;
if (tcp_init->numlayers > 9)
s++;
cp.matrice =
(int *) malloc(tcp_init->numlayers * NumResolution * 3 *
sizeof(int));
s = s + 2;
for (i = 0; i < tcp_init->numlayers; i++) {
tcp_init->rates[i] = 1;
sscanf(s, "%d,", &cp.matrice[i * NumResolution * 3]);
s += 2;
if (cp.matrice[i * NumResolution * 3] > 9)
s++;
cp.matrice[i * NumResolution * 3 + 1] = 0;
cp.matrice[i * NumResolution * 3 + 2] = 0;
for (j = 1; j < NumResolution; j++) {
sscanf(s, "%d,%d,%d",
&cp.matrice[i * NumResolution * 3 + j * 3 + 0],
&cp.matrice[i * NumResolution * 3 + j * 3 + 1],
&cp.matrice[i * NumResolution * 3 + j * 3 + 2]);
s += 6;
if (cp.matrice[i * NumResolution * 3 + j * 3] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 1] > 9)
s++;
if (cp.matrice[i * NumResolution * 3 + j * 3 + 2] > 9)
s++;
}
if (i < tcp_init->numlayers - 1)
s++;
}
cp.fixed_alloc = 1;
break;
/* ----------------------------------------------------- */
case 't': /* tiles */
sscanf(optarg, "%d,%d", &cp.tdx, &cp.tdy);
Tile_arg = 1;
break;
/* ----------------------------------------------------- */
case 'n': /* resolution */
sscanf(optarg, "%d", &NumResolution);
break;
/* ----------------------------------------------------- */
case 'c': /* precinct dimension */
s = optarg;
do {
sep = 0;
sscanf(s, "[%d,%d]%c", &prcw_init[res_spec],
&prch_init[res_spec], &sep);
CSty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
} while (sep == ',');
break;
/* ----------------------------------------------------- */
case 'b': /* code-block dimension */
sscanf(optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init * cblockh_init > 4096 || cblockw_init > 1024
|| cblockw_init < 4 || cblockh_init > 1024 || cblockh_init < 4) {
fprintf(stderr,
"!! Size of code_block error (option -b) !!\n\nRestriction :\n * width*height<=4096\n * 4<=width,height<= 1024\n\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'x': /* creation of index file */
index = optarg;
cp.index_on = 1;
break;
/* ----------------------------------------------------- */
case 'p': /* progression order */
s = optarg;
for (i = 0; i < 4; i++) {
progression[i] = *s;
s++;
}
Prog_order = give_progression(progression);
if (Prog_order == -1) {
fprintf(stderr,
"Unrecognized progression order [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 's': /* subsampling factor */
if (sscanf(optarg, "%d,%d", &subsampling_dx, &subsampling_dy)
!= 2) {
fprintf(stderr,
"'-s' sub-sampling argument error ! [-s dx,dy]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'd': /* coordonnate of the reference grid */
if (sscanf(optarg, "%d,%d", &Dim[0], &Dim[1]) != 2) {
fprintf(stderr,
"-d 'coordonnate of the reference grid' argument error !! [-d x0,y0]\n");
return 1;
}
break;
/* ----------------------------------------------------- */
case 'h': /* Display an help description */
help_display();
return 0;
break;
/* ----------------------------------------------------- */
case 'P': /* POC */
fprintf(stderr, "/----------------------------------\\\n");
fprintf(stderr, "| POC option not fully tested !! |\n");
fprintf(stderr, "\\----------------------------------/\n");
s = optarg;
while (sscanf(s, "T%d=%d,%d,%d,%d,%d,%s", &POC[numpocs].tile,
&POC[numpocs].resno0, &POC[numpocs].compno0,
&POC[numpocs].layno1, &POC[numpocs].resno1,
&POC[numpocs].compno1, POC[numpocs].progorder) == 7) {
POC[numpocs].prg = give_progression(POC[numpocs].progorder);
/* POC[numpocs].tile; */
numpocs++;
while (*s && *s != '/') {
s++;
}
if (!*s)
break;
s++;
}
break;
/* ------------------------------------------------------ */
case 'S': /* SOP marker */
CSty |= 0x02;
break;
/* ------------------------------------------------------ */
case 'E': /* EPH marker */
CSty |= 0x04;
break;
/* ------------------------------------------------------ */
case 'M': /* Mode switch pas tous au point !! */
if (sscanf(optarg, "%d", &value) == 1) {
for (i = 0; i <= 5; i++) {
int cache = value & (1 << i);
if (cache)
mode |= (1 << i);
}
}
break;
/* ------------------------------------------------------ */
case 'R': /* ROI */
if (sscanf(optarg, "OI:c=%d,U=%d", &ROI_compno, &ROI_shift) != 2) {
fprintf(stderr, "ROI error !! [-ROI:c='compno',U='shift']\n");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'T': /* Tile offset */
if (sscanf(optarg, "%d,%d", &TX0, &TY0) != 2) {
fprintf(stderr, "-T 'tile offset' argument error !! [-T X0,Y0]");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'C': /* Add a comment */
cp.comment = optarg;
break;
/* ------------------------------------------------------ */
case 'I': /* reversible or not */
ir = 1;
break;
/* ------------------------------------------------------ */
case 'W': /* Width and Height and Cb and Cr subsampling in case of YUV format files */
if (sscanf
(optarg, "%d,%d,%d,%d", &w, &h, &CbCr_subsampling_dx,
&CbCr_subsampling_dy) != 4) {
fprintf(stderr, "-W argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
case 'F': /* Video frame rate */
if (sscanf(optarg, "%d", &frame_rate) != 1) {
fprintf(stderr, "-F argument error");
return 1;
}
break;
/* ------------------------------------------------------ */
default:
return 1;
}
}
cp.tx0 = TX0;
cp.ty0 = TY0;
/* Error messages */
/* -------------- */
if (!infile || !outfile) {
fprintf(stderr,
"Correct usage: mj2_encoder -i yuv-file -o mj2-file (+ options)\n");
return 1;
}
if ((cp.disto_alloc || cp.fixed_alloc || cp.fixed_quality)
&& (!(cp.disto_alloc ^ cp.fixed_alloc ^ cp.fixed_quality))) {
fprintf(stderr,
"Error: options -r -q and -f can not be used together !!\n");
return 1;
} // mod fixed_quality
/* if no rate entered, lossless by default */
if (tcp_init->numlayers == 0) {
tcp_init->rates[tcp_init->numlayers] = 0; //MOD antonin : losslessbug
tcp_init->numlayers++;
cp.disto_alloc = 1;
}
if (TX0 > Dim[0] || TY0 > Dim[1]) {
fprintf(stderr,
"Error: Tile offset dimension is unnappropriate --> TX0(%d)<=IMG_X0(%d) TYO(%d)<=IMG_Y0(%d) \n",
TX0, Dim[0], TY0, Dim[1]);
return 1;
}
for (i = 0; i < numpocs; i++) {
if (POC[i].prg == -1) {
fprintf(stderr,
"Unrecognized progression order in option -P (POC n %d) [LRCP, RLCP, RPCL, PCRL, CPRL] !!\n",
i + 1);
}
}
/* to respect profile - 0 */
/* ---------------------- */
numD_min = 0;
x1 = !Dim[0] ? (w - 1) * subsampling_dx + 1 : Dim[0] + (w -
1) *
subsampling_dx + 1;
y1 = !Dim[1] ? (h - 1) * subsampling_dy + 1 : Dim[1] + (h -
1) *
subsampling_dy + 1;
if (Tile_arg == 1) {
cp.tw = int_ceildiv(x1 - cp.tx0, cp.tdx);
cp.th = int_ceildiv(y1 - cp.ty0, cp.tdy);
} else {
cp.tdx = x1 - cp.tx0;
cp.tdy = y1 - cp.ty0;
}
/* Initialization for PPM marker */
cp.ppm = 0;
cp.ppm_data = NULL;
cp.ppm_previous = 0;
cp.ppm_store = 0;
numcomps = 3; /* Because YUV files only have 3 components */
tcp_init->mct = 0; /* No component transform needed */
prec = 8; /* Because in YUV files, components have 8-bit depth */
/* Init the mutiple tiles */
/* ---------------------- */
cp.tcps = (j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
tcp = &cp.tcps[tileno];
tcp->numlayers = tcp_init->numlayers;
for (j = 0; j < tcp->numlayers; j++) {
if (cp.fixed_quality) // add fixed_quality
tcp->distoratio[j] = tcp_init->distoratio[j];
else
tcp->rates[j] = tcp_init->rates[j];
}
tcp->csty = CSty;
tcp->prg = Prog_order;
tcp->mct = tcp_init->mct;
tcp->ppt = 0;
tcp->ppt_data = NULL;
tcp->ppt_store = 0;
numpocs_tile = 0;
tcp->POC = 0;
if (numpocs) {
/* intialisation of POC */
tcp->POC = 1;
for (i = 0; i < numpocs; i++) {
if (tileno == POC[i].tile - 1 || POC[i].tile == -1) {
tcp_poc = &tcp->pocs[numpocs_tile];
tcp_poc->resno0 = POC[numpocs_tile].resno0;
tcp_poc->compno0 = POC[numpocs_tile].compno0;
tcp_poc->layno1 = POC[numpocs_tile].layno1;
tcp_poc->resno1 = POC[numpocs_tile].resno1;
tcp_poc->compno1 = POC[numpocs_tile].compno1;
tcp_poc->prg = POC[numpocs_tile].prg;
tcp_poc->tile = POC[numpocs_tile].tile;
numpocs_tile++;
}
}
}
tcp->numpocs = numpocs_tile;
tcp->tccps = (j2k_tccp_t *) malloc(numcomps * sizeof(j2k_tccp_t));
for (i = 0; i < numcomps; i++) {
tccp = &tcp->tccps[i];
tccp->csty = CSty & 0x01; /* 0 => one precinct || 1 => custom precinct */
tccp->numresolutions = NumResolution;
tccp->cblkw = int_floorlog2(cblockw_init);
tccp->cblkh = int_floorlog2(cblockh_init);
tccp->cblksty = mode;
tccp->qmfbid = ir ? 0 : 1;
tccp->qntsty = ir ? J2K_CCP_QNTSTY_SEQNT : J2K_CCP_QNTSTY_NOQNT;
tccp->numgbits = 2;
if (i == ROI_compno)
tccp->roishift = ROI_shift;
else
tccp->roishift = 0;
if (CSty & J2K_CCP_CSTY_PRT) {
int p = 0;
for (j = tccp->numresolutions - 1; j >= 0; j--) {
if (p < res_spec) {
if (prcw_init[p] < 1)
tccp->prcw[j] = 1;
else
tccp->prcw[j] = int_floorlog2(prcw_init[p]);
if (prch_init[p] < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(prch_init[p]);
} else {
int size_prcw, size_prch;
size_prcw = prcw_init[res_spec - 1] >> (p - (res_spec - 1));
size_prch = prch_init[res_spec - 1] >> (p - (res_spec - 1));
if (size_prcw < 1)
tccp->prcw[j] = 1;
else
tccp->prcw[j] = int_floorlog2(size_prcw);
if (size_prch < 1)
tccp->prch[j] = 1;
else
tccp->prch[j] = int_floorlog2(size_prch);
}
p++;
/*printf("\nsize precinct pour level %d : %d,%d\n", j,
tccp->prcw[j], tccp->prch[j]); */
}
} else {
for (j = 0; j < tccp->numresolutions; j++) {
tccp->prcw[j] = 15;
tccp->prch[j] = 15;
}
}
calc_explicit_stepsizes(tccp, prec);
}
}
mj2file = fopen(outfile, "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
movie.tk = (mj2_tk_t *) malloc(sizeof(mj2_tk_t));
movie.num_vtk = 1;
movie.num_stk = 0;
movie.num_htk = 0;
movie.tk[0].track_type = 0; // Video Track
movie.tk[0].track_ID = 1;
movie.tk[0].Dim[0] = Dim[0];
movie.tk[0].Dim[1] = Dim[1];
movie.tk[0].w = w;
movie.tk[0].h = h;
movie.tk[0].CbCr_subsampling_dx = CbCr_subsampling_dx;
movie.tk[0].CbCr_subsampling_dy = CbCr_subsampling_dy;
movie.tk[0].sample_rate = frame_rate;
movie.tk[0].jp2_struct.numcomps = 3; // NC
jp2_init_stdjp2(&movie.tk[0].jp2_struct);
movie.tk[0].jp2_struct.w = w;
movie.tk[0].jp2_struct.h = h;
movie.tk[0].jp2_struct.bpc = 7;
movie.tk[0].jp2_struct.meth = 1;
movie.tk[0].jp2_struct.enumcs = 18; // YUV
yuvfile = fopen(infile,"rb");
if (!yuvfile) {
fprintf(stderr, "failed to open %s for reading\n",infile);
return 1;
}
movie.tk[0].num_samples = yuv_num_frames(&movie.tk[0],yuvfile);
if (!movie.tk[0].num_samples) {
fprintf(stderr,"Unable to count the number of frames in YUV input file\n");
}
// One sample per chunk
movie.tk[0].chunk = (mj2_chunk_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_chunk_t));
movie.tk[0].sample = (mj2_sample_t*) malloc(movie.tk[0].num_samples * sizeof(mj2_sample_t));
if (mj2_init_stdmovie(&movie)) {
fprintf(stderr, "Error with movie initialization");
return 1;
};
// Writing JP, FTYP and MDAT boxes
buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
// boxes won't be longer than 300 bytes
cio_init(buf , 300);
mj2_write_jp();
mj2_write_ftyp(&movie);
mdat_initpos = cio_tell();
cio_skip(4);
cio_write(MJ2_MDAT, 4);
fwrite(buf,cio_tell(),1,mj2file);
offset = cio_tell();
free(buf);
for (i = 0; i < movie.num_stk + movie.num_htk + movie.num_vtk; i++) {
if (movie.tk[i].track_type != 0) {
fprintf(stderr, "Unable to write sound or hint tracks\n");
} else {
//j2k_cp_t cp_init;
mj2_tk_t *tk;
tk = &movie.tk[i];
//tk->sample =
//(mj2_sample_t *) malloc(tk->num_samples * sizeof(mj2_sample_t));
tk->num_chunks = tk->num_samples;
//tk->chunk =
//(mj2_chunk_t *) malloc(tk->num_chunks * sizeof(mj2_chunk_t));
fprintf(stderr, "Video Track number %d\n", i + 1);
// Copy the first tile coding parameters (tcp) to cp_init
//cp_init.tcps =
//(j2k_tcp_t *) malloc(cp.tw * cp.th * sizeof(j2k_tcp_t));
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
for (l = 0; l < cp.tcps[tileno].numlayers; l++) {
cp_init.tcps[tileno].rates[l] = cp.tcps[tileno].rates[l];
//tileno = cp.tcps[tileno].rates[l];
}
}
for (sampleno = 0; sampleno < tk->num_samples; sampleno++) {
buf = (char *) malloc(cp.tdx * cp.tdy * cp.th * cp.tw * 2);
cio_init(buf, cp.tdx * cp.tdy * cp.th * cp.tw * 2);
fprintf(stderr, "Frame number %d/%d: ", sampleno + 1, tk->num_samples);
if (!yuvtoimage(yuvfile, tk, &img, sampleno, subsampling_dx, subsampling_dy)) {
fprintf(stderr, "Error with frame number %d in YUV file\n", sampleno);
return 1;
}
jp2c_initpos = cio_tell();
cio_skip(4);
cio_write(JP2_JP2C, 4); // JP2C
len = j2k_encode(&img, &cp, buf, cp.tdx * cp.tdy * 2, index);
if (len ==0) {
fprintf(stderr,"Unable to encode image");
return 1;
}
cio_seek(jp2c_initpos);
cio_write(len+8,4);
for (m = 0; m < img.numcomps; m++) {
free(img.comps[m].data);
}
free(img.comps);
tk->sample[sampleno].sample_size = len+8;
tk->sample[sampleno].offset = offset;
tk->chunk[sampleno].offset = offset; // There is one sample per chunk
fwrite(buf, 1, len+8, mj2file);
offset += len+8;
free(buf);
// Copy the cp_init parameters to cp.tcps
for (tileno = 0; tileno < cp.tw * cp.th; tileno++) {
for (k = 0; k < cp.tcps[tileno].numlayers; k++) {
cp.tcps[tileno].rates[k] = cp_init.tcps[tileno].rates[k];
}
}
}
}
}
fseek(mj2file, mdat_initpos, SEEK_SET);
buf = (char*) malloc(4*sizeof(char));
cio_init(buf, 4); // Init a cio to write box length variable in a little endian way
cio_write(offset - mdat_initpos, 4);
fwrite(buf, 4, 1, mj2file);
fseek(mj2file,0,SEEK_END);
free(buf);
// Writing MOOV box
i=1;
buf = (char*) malloc (10000 * sizeof(char));
cio_init(buf , i*10000);
if (setjmp(j2k_error)) {
i++;
realloc(buf,i*10000* sizeof(char));
pos = cio_tell();
cio_init(buf , i*10000);
cio_seek(pos);
}
mj2_write_moov(&movie);
fwrite(buf,cio_tell(),1,mj2file);
// Ending program
mj2_memory_free(&movie);
free(cp_init.tcps);
if (tcp_init->numlayers > 9)
free(cp.matrice);
for (tileno = 0; tileno < cp.tw * cp.th; tileno++)
free(cp.tcps[tileno].tccps);
free(cp.tcps);
fclose(mj2file);
free(buf);
remove("Compo0");
remove("Compo1");
remove("Compo2");
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

269
mj2/frames_to_mj2.dsp Normal file
View File

@@ -0,0 +1,269 @@
# Microsoft Developer Studio Project File - Name="frames_to_mj2" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=frames_to_mj2 - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "frames_to_mj2.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "frames_to_mj2.mak" CFG="frames_to_mj2 - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "frames_to_mj2 - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "frames_to_mj2 - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "frames_to_mj2 - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "frames_to_mj2 - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "frames_to_mj2___Win32_Debug0"
# PROP BASE Intermediate_Dir "frames_to_mj2___Win32_Debug0"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "frames_to_mj2___Win32_Debug0"
# PROP Intermediate_Dir "frames_to_mj2___Win32_Debug0"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "frames_to_mj2 - Win32 Release"
# Name "frames_to_mj2 - Win32 Debug"
# Begin Group "MJ2"
# PROP Default_Filter ""
# Begin Group "MJ2 Header Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.h
# End Source File
# End Group
# Begin Group "MJ2 Source Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\frames_to_mj2.c
# End Source File
# Begin Source File
SOURCE=.\compat\getopt.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.c
# End Source File
# End Group
# End Group
# Begin Group "OpenJPEG"
# PROP Default_Filter ""
# Begin Group "OpenJPEG Header Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# Begin Group "OpenJPEG Source Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=..\libopenjpeg\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.c
# End Source File
# End Group
# End Group
# End Target
# End Project

2185
mj2/meta_out.c Normal file

File diff suppressed because it is too large Load Diff

13
mj2/meta_out.h Normal file
View File

@@ -0,0 +1,13 @@
/* meta_out.h */
/* Dump MJ2, JP2 metadata (partial so far) to xml file */
/* Callable from mj2_to_metadata */
/* Contributed to Open JPEG by Glenn Pearson, U.S. National Library of Medicine */
#define BOOL int
#define FALSE 0
#define TRUE 1
void xml_write_init(BOOL n, BOOL t, BOOL r, BOOL d);
int xml_write_struct(FILE *file, FILE *xmlout, mj2_movie_t * movie, unsigned int sampleframe, char* stringDTD);

2798
mj2/mj2.c Normal file

File diff suppressed because it is too large Load Diff

201
mj2/mj2.h Normal file
View File

@@ -0,0 +1,201 @@
/*
* Copyright (c) 2003-2004, Fran<61>ois-Olivier Devaux
* Copyright (c) 2003-2004, Communications and remote sensing Laboratory, Universite catholique de Louvain, Belgium
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <j2k.h>
#include <jp2.h>
#ifndef __MJ2_H
#define __MJ2_H
typedef struct { /* Time To Sample */
int sample_count;
int sample_delta;
} mj2_tts_t;
typedef struct { /* Chunk */
int num_samples;
int sample_descr_idx;
int offset;
} mj2_chunk_t;
typedef struct { /* Sample to chunk */
int first_chunk;
int samples_per_chunk;
int sample_descr_idx;
} mj2_sampletochunk_t;
typedef struct { /* Sample */
unsigned int sample_size;
unsigned int offset;
unsigned int sample_delta;
} mj2_sample_t;
typedef struct { /* URL */
int location[4];
} mj2_url_t;
typedef struct { /* URN */
int name[2];
int location[4];
} mj2_urn_t;
typedef struct { /* Video Track Parameters */
int track_ID;
int track_type;
unsigned int creation_time;
unsigned int modification_time;
int duration;
int timescale;
int layer;
int volume;
int language;
int balance;
int maxPDUsize;
int avgPDUsize;
int maxbitrate;
int avgbitrate;
int slidingavgbitrate;
int graphicsmode;
int opcolor[3];
int num_url;
mj2_url_t *url;
int num_urn;
mj2_urn_t *urn;
int Dim[2];
int w;
int h;
int visual_w;
int visual_h;
int CbCr_subsampling_dx;
int CbCr_subsampling_dy;
// int subsampling_dx;
// int subsampling_dy;
int sample_rate;
int sample_description;
int horizresolution;
int vertresolution;
int compressorname[8];
int depth;
unsigned char fieldcount;
unsigned char fieldorder;
unsigned char or_fieldcount;
unsigned char or_fieldorder;
int num_br;
unsigned int *br;
unsigned char num_jp2x;
unsigned char *jp2xdata;
unsigned char hsub;
unsigned char vsub;
unsigned char hoff;
unsigned char voff;
int trans_matrix[9];
unsigned int num_samples; /* Number of samples */
int transorm;
int handler_type;
int name_size;
unsigned char same_sample_size;
int num_tts;
mj2_tts_t *tts; /* Time to sample */
unsigned int num_chunks;
mj2_chunk_t *chunk;
int num_samplestochunk;
mj2_sampletochunk_t *sampletochunk;
char *name;
jp2_struct_t jp2_struct;
mj2_sample_t *sample; /* Sample parameters */
} mj2_tk_t; /* Track Parameters */
typedef struct { /* Movie */
unsigned int brand;
unsigned int minversion;
int num_cl;
unsigned int *cl;
unsigned int creation_time;
unsigned int modification_time;
int timescale;
unsigned int duration;
int rate;
int num_vtk;
int num_stk;
int num_htk;
int volume;
int trans_matrix[9];
int next_tk_id;
mj2_tk_t *tk; /* Track Parameters */
} mj2_movie_t;
typedef struct {
int length;
int type;
int init_pos;
} mj2_box_t;
void mj2_write_jp();
void mj2_write_ftyp(mj2_movie_t * movie);
/*
* Use this function to initialize a standard movie with standard values
* It has one sample per chunk
*/
int mj2_init_stdmovie(mj2_movie_t * movie);
/* int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index);
*
* Encode a MJ2 movie from a yuv file
* movie: an existing mj2_movie structure (to create a standard one, use mj2_init_stdmovie
* cp: coding parameters of j2k images
* index: index file name
*/
int mj2_encode(mj2_movie_t * movie, j2k_cp_t * cp, char *index);
/* int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
* j2k_cp_t * cp, char *outfile);
*
* Decode a MJ2 movie to a yuv file
* src: pointer to memory where frames will be stored
* movie: a mj2_movie structure
* cp: coding parameters of j2k images
* outfile: yuv file name
*/
int mj2_decode(unsigned char *src, int len, mj2_movie_t * movie,
j2k_cp_t * cp, char *outfile);
/*
* Free memory used to encode and decode mj2 files
*
*/
void mj2_memory_free(mj2_movie_t * movie);
int mj2_read_struct(FILE *file, mj2_movie_t * movie);
void mj2_write_moov(mj2_movie_t * movie);
#endif

318
mj2/mj2_convert.c Normal file
View File

@@ -0,0 +1,318 @@
#include <stdio.h>
#include <malloc.h>
#include "mj2.h"
#include <j2k.h>
#include <int.h>
/* ----------------------- */
/* */
/* */
/* Count the number of frames */
/* in a YUV file */
/* */
/* ----------------------- */
int yuv_num_frames(mj2_tk_t * tk, FILE *f)
{
int numimages, frame_size;
long end_of_f;
frame_size = (int) (tk->w * tk->h * (1.0 + (double) 2 / (double) (tk->CbCr_subsampling_dx * tk->CbCr_subsampling_dy))); /* Calculate frame size */
fseek(f, 0, SEEK_END);
end_of_f = ftell(f); /* Calculate file size */
if (end_of_f < frame_size) {
fprintf(stderr,
"YUV does not contains any frame of %d x %d size\n", tk->w,
tk->h);
return 0;
}
numimages = end_of_f / frame_size; /* Calculate number of images */
return numimages;
fclose(f);
}
// -----------------------
//
//
// YUV to IMAGE
//
// -----------------------
int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
int frame_num, int subsampling_dx, int subsampling_dy)
{
int i, j;
int offset;
long end_of_f, position;
FILE *Compo;
offset =
(int) ((double) (frame_num * tk->w * tk->h) *
(1.0 +
1.0 * (double) 2 / (double) (tk->CbCr_subsampling_dx *
tk->CbCr_subsampling_dy)));
fseek(yuvfile, 0, SEEK_END);
end_of_f = ftell(yuvfile);
fseek(yuvfile, sizeof(unsigned char) * offset, SEEK_SET);
position = ftell(yuvfile);
if (position >= end_of_f) {
fprintf(stderr, "Cannot reach frame number %d in yuv file !!\n",
frame_num);
return 0;
}
img->x0 = tk->Dim[0];
img->y0 = tk->Dim[1];
img->x1 = !tk->Dim[0] ? (tk->w - 1) * subsampling_dx + 1 : tk->Dim[0] +
(tk->w - 1) * subsampling_dx + 1;
img->y1 = !tk->Dim[1] ? (tk->h - 1) * subsampling_dy + 1 : tk->Dim[1] +
(tk->h - 1) * subsampling_dy + 1;
img->numcomps = 3;
img->color_space = 3;
img->comps = (j2k_comp_t *) malloc(img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < img->numcomps; i++) {
img->comps[i].data = (int *) malloc(sizeof(int) * tk->w * tk->h);
img->comps[i].prec = 8;
img->comps[i].bpp = 8;
img->comps[i].sgnd = 0;
if (i == 0) {
img->comps[i].dx = subsampling_dx;
img->comps[i].dy = subsampling_dy;
} else {
img->comps[i].dx = subsampling_dx * tk->CbCr_subsampling_dx;
img->comps[i].dy = subsampling_dy * tk->CbCr_subsampling_dy;
}
}
Compo = fopen("Compo0", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo0 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[0].dx * img->comps[0].dy))
&& !feof(yuvfile); i++) {
unsigned char y;
j = fread(&y, 1, 1, yuvfile);
fwrite(&y, 1, 1, Compo);
}
fclose(Compo);
Compo = fopen("Compo1", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo1 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[1].dx * img->comps[1].dy))
&& !feof(yuvfile); i++) {
unsigned char cb;
j = fread(&cb, sizeof(unsigned char), 1, yuvfile);
fwrite(&cb, 1, 1, Compo);
}
fclose(Compo);
Compo = fopen("Compo2", "wb");
if (!Compo) {
fprintf(stderr, "Failed to open Compo2 for writing !\n");
}
for (i = 0; i < (tk->w * tk->h / (img->comps[2].dx * img->comps[2].dy))
&& !feof(yuvfile); i++) {
unsigned char cr;
j = fread(&cr, sizeof(unsigned char), 1, yuvfile);
fwrite(&cr, 1, 1, Compo);
}
fclose(Compo);
return 1;
}
// -----------------------
//
//
// IMAGE to YUV
//
// -----------------------
int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile)
{
FILE *f;
int i;
if (img->numcomps == 3) {
if (img->comps[0].dx != img->comps[1].dx / 2
|| img->comps[1].dx != img->comps[2].dx) {
fprintf(stderr,
"Error with the input image components size: cannot create yuv file)\n");
return 1;
}
} else if (!(img->numcomps == 1)) {
fprintf(stderr,
"Error with the number of image components(must be one or three)\n");
return 1;
}
f = fopen(outfile, "a+b");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1;
}
for (i = 0; i < (img->comps[0].w * img->comps[0].h); i++) {
unsigned char y;
y = img->comps[0].data[i];
fwrite(&y, 1, 1, f);
}
if (img->numcomps == 3) {
for (i = 0; i < (img->comps[1].w * img->comps[1].h); i++) {
unsigned char cb;
cb = img->comps[1].data[i];
fwrite(&cb, 1, 1, f);
}
for (i = 0; i < (img->comps[2].w * img->comps[2].h); i++) {
unsigned char cr;
cr = img->comps[2].data[i];
fwrite(&cr, 1, 1, f);
}
} else if (img->numcomps == 1) {
for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
unsigned char cb = 125;
fwrite(&cb, 1, 1, f);
}
for (i = 0; i < (img->comps[0].w * img->comps[0].h * 0.25); i++) {
unsigned char cr = 125;
fwrite(&cr, 1, 1, f);
}
}
fclose(f);
return 0;
}
// -----------------------
//
//
// IMAGE to BMP
//
// -----------------------
int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile) {
int w,wr,h,hr,i,pad;
FILE *f;
if (img->numcomps == 3 && img->comps[0].dx == img->comps[1].dx
&& img->comps[1].dx == img->comps[2].dx
&& img->comps[0].dy == img->comps[1].dy
&& img->comps[1].dy == img->comps[2].dy
&& img->comps[0].prec == img->comps[1].prec
&& img->comps[1].prec == img->comps[2].prec) {
/* -->> -->> -->> -->>
24 bits color
<<-- <<-- <<-- <<-- */
f = fopen(outfile, "wb");
if (!f) {
fprintf(stderr, "failed to open %s for writing\n", outfile);
return 1;
}
w = img->comps[0].w;
wr = int_ceildivpow2(img->comps[0].w, img->comps[0].factor);
h = img->comps[0].h;
hr = int_ceildivpow2(img->comps[0].h, img->comps[0].factor);
fprintf(f, "BM");
/* FILE HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c",
(unsigned char) (hr * wr * 3 + 3 * hr * (wr % 2) +
54) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 8) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 16) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2) + 54)
>> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (54) & 0xff, ((54) >> 8) & 0xff,
((54) >> 16) & 0xff, ((54) >> 24) & 0xff);
/* INFO HEADER */
/* ------------- */
fprintf(f, "%c%c%c%c", (40) & 0xff, ((40) >> 8) & 0xff,
((40) >> 16) & 0xff, ((40) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((wr) & 0xff),
(unsigned char) ((wr) >> 8) & 0xff,
(unsigned char) ((wr) >> 16) & 0xff,
(unsigned char) ((wr) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (unsigned char) ((hr) & 0xff),
(unsigned char) ((hr) >> 8) & 0xff,
(unsigned char) ((hr) >> 16) & 0xff,
(unsigned char) ((hr) >> 24) & 0xff);
fprintf(f, "%c%c", (1) & 0xff, ((1) >> 8) & 0xff);
fprintf(f, "%c%c", (24) & 0xff, ((24) >> 8) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c",
(unsigned char) (3 * hr * wr +
3 * hr * (wr % 2)) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
8) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
16) & 0xff,
(unsigned char) ((hr * wr * 3 + 3 * hr * (wr % 2)) >>
24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (7834) & 0xff, ((7834) >> 8) & 0xff,
((7834) >> 16) & 0xff, ((7834) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
fprintf(f, "%c%c%c%c", (0) & 0xff, ((0) >> 8) & 0xff,
((0) >> 16) & 0xff, ((0) >> 24) & 0xff);
for (i = 0; i < wr * hr; i++) {
unsigned char R, G, B;
/* a modifier */
// R = img->comps[0].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
R = img->comps[0].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
// G = img->comps[1].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
G = img->comps[1].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
// B = img->comps[2].data[w * h - ((i) / (w) + 1) * w + (i) % (w)];
B = img->comps[2].data[w * hr - ((i) / (wr) + 1) * w + (i) % (wr)];
fprintf(f, "%c%c%c", B, G, R);
if ((i + 1) % wr == 0) {
for (pad = (3 * wr) % 4 ? 4 - (3 * wr) % 4 : 0; pad > 0; pad--) /* ADD */
fprintf(f, "%c", 0);
}
}
fclose(f);
free(img->comps[1].data);
free(img->comps[2].data);
}
return 0;
}

16
mj2/mj2_convert.h Normal file
View File

@@ -0,0 +1,16 @@
#include "mj2.h"
#ifndef __MJ2_CONVERT_H
#define __MJ2_CONVERT_H
int imagetoyuv(j2k_image_t * img, j2k_cp_t * cp, char *outfile);
int imagetobmp(j2k_image_t * img, j2k_cp_t * cp, char *outfile);
int yuvtoimage(FILE *yuvfile, mj2_tk_t * tk, j2k_image_t * img,
int frame_num, int subsampling_dx, int subsampling_dy);
int yuv_num_frames(mj2_tk_t * tk, FILE *f);
#endif

130
mj2/mj2_to_frames.c Normal file
View File

@@ -0,0 +1,130 @@
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include "mj2.h"
#include "mj2_convert.h"
#include <openjpeg.h>
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
int main(int argc, char *argv[]) {
unsigned int tnum, snum;
mj2_movie_t movie;
mj2_tk_t *track;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *file, *outfile;
char outfilename[50];
j2k_image_t img;
j2k_cp_t cp;
int i;
cp.layer=0;
cp.reduce=0;
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_decoder inputfile.mj2 outputfile.yuv\n");
printf("Example: MJ2_decoder foreman.mj2 foreman.yuv\n");
return 1;
}
file = fopen(argv[1], "rb");
if (!file) {
fprintf(stderr, "failed to open %s for reading\n", argv[1]);
return 1;
}
// Checking output file
outfile = fopen(argv[2], "w");
if (!file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
fclose(outfile);
if (mj2_read_struct(file, &movie)) // Creating the movie structure
return 1;
// Decode first video track
tnum = 0;
while (movie.tk[tnum].track_type != 0)
tnum ++;
track = &movie.tk[tnum];
// Output info on first video tracl
fprintf(stdout,"The first video track contains %d frames.\nWidth: %d, Height: %d \n\n",
track->num_samples, track->w, track->h);
for (snum=0; snum < track->num_samples; snum++)
{
fprintf(stdout,"Frame %d: ",snum+1);
sample = &track->sample[snum];
frame_codestream = (unsigned char*) malloc (sample->sample_size-8); // Skipping JP2C marker
fseek(file,sample->offset+8,SEEK_SET);
fread(frame_codestream,sample->sample_size-8,1, file); // Assuming that jp and ftyp markers size do
if (!j2k_decode(frame_codestream, sample->sample_size-8, &img, &cp)) // Decode J2K to image
return 1;
if (((img.numcomps == 3) && (img.comps[0].dx == img.comps[1].dx / 2)
&& (img.comps[0].dx == img.comps[2].dx / 2 ) && (img.comps[0].dx == 1))
|| (img.numcomps == 1)) {
if (imagetoyuv(&img, &cp, argv[2])) // Convert image to YUV
return 1;
}
else if ((img.numcomps == 3) &&
(img.comps[0].dx == 1) && (img.comps[1].dx == 1)&&
(img.comps[2].dx == 1))// If YUV 4:4:4 input --> to bmp
{
fprintf(stdout,"The frames will be output in a bmp format (output_1.bmp, ...)\n");
sprintf(outfilename,"output_%d.bmp",snum);
if (imagetobmp(&img, &cp, outfilename)) // Convert image to YUV
return 1;
}
else {
fprintf(stdout,"Image component dimensions are unknown. Unable to output image\n");
fprintf(stdout,"The frames will be output in a j2k file (output_1.j2k, ...)\n");
sprintf(outfilename,"output_%d.j2k",snum);
outfile = fopen(outfilename, "wb");
if (!outfile) {
fprintf(stderr, "failed to open %s for writing\n",outfilename);
return 1;
}
fwrite(frame_codestream,sample->sample_size-8,1,outfile);
fclose(outfile);
}
for (i=0; i<img.numcomps; i++)
free(img.comps[i].data);
j2k_dec_release();
free(frame_codestream);
}
fclose(file);
fprintf(stdout, "%d frame(s) correctly extracted\n", snum);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

305
mj2/mj2_to_frames.dsp Normal file
View File

@@ -0,0 +1,305 @@
# Microsoft Developer Studio Project File - Name="mj2_to_frames" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=mj2_to_frames - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "mj2_to_frames.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "mj2_to_frames.mak" CFG="mj2_to_frames - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "mj2_to_frames - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "mj2_to_frames - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "mj2_to_frames - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "mj2_to_frames___Win32_Debug0"
# PROP BASE Intermediate_Dir "mj2_to_frames___Win32_Debug0"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "mj2_to_frames___Win32_Debug0"
# PROP Intermediate_Dir "mj2_to_frames___Win32_Debug0"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "mj2_to_frames - Win32 Release"
# Name "mj2_to_frames - Win32 Debug"
# Begin Group "MJ2"
# PROP Default_Filter ""
# Begin Group "MJ2 Header Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.h
# End Source File
# End Group
# Begin Group "MJ2 Source Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.c
!IF "$(CFG)" == "mj2_to_frames - Win32 Release"
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\mj2.c
!IF "$(CFG)" == "mj2_to_frames - Win32 Release"
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.c
!IF "$(CFG)" == "mj2_to_frames - Win32 Release"
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
# Begin Source File
SOURCE=.\mj2_to_frames.c
!IF "$(CFG)" == "mj2_to_frames - Win32 Release"
!ELSEIF "$(CFG)" == "mj2_to_frames - Win32 Debug"
# ADD CPP /I "../libopenjpeg"
!ENDIF
# End Source File
# End Group
# End Group
# Begin Group "OpenJPEG"
# PROP Default_Filter ""
# Begin Group "OpenJPEG Header Files"
# PROP Default_Filter ".h"
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# Begin Group "OpenJPEG Source Files"
# PROP Default_Filter ".c"
# Begin Source File
SOURCE=..\libopenjpeg\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.c
# End Source File
# End Group
# End Group
# End Target
# End Project

281
mj2/mj2_to_metadata.c Normal file
View File

@@ -0,0 +1,281 @@
/* mj2_to_metadata.c */
/* Dump MJ2, JP2 metadata (partial so far) to xml file */
/* Contributed to Open JPEG by Glenn Pearson, contract software developer, U.S. National Library of Medicine.
The base code in this file was developed by the author as part of a video archiving
project for the U.S. National Library of Medicine, Bethesda, MD.
It is the policy of NLM (and U.S. government) to not assert copyright.
A non-exclusive copy of this code has been contributed to the Open JPEG project.
Except for copyright, inclusion of the code within Open JPEG for distribution and use
can be bound by the Open JPEG open-source license and disclaimer, expressed elsewhere.
*/
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include "mj2.h"
#include <openjpeg.h>
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
#include "mj2_to_metadata.h"
#include <string.h>
#ifndef DONT_HAVE_GETOPT
#include <getopt.h>
#else
#include "compat/getopt.h"
#endif
/* ------------- */
void help_display()
{
/* "1234567890123456789012345678901234567890123456789012345678901234567890123456789" */
fprintf(stdout," Help for the 'mj2_to_metadata' Program\n");
fprintf(stdout," ======================================\n");
fprintf(stdout,"The -h option displays this information on screen.\n\n");
fprintf(stdout,"mj2_to_metadata generates an XML file from a Motion JPEG 2000 file.\n");
fprintf(stdout,"The generated XML shows the structural, but not (yet) curatorial,\n");
fprintf(stdout,"metadata from the movie header and from the JPEG 2000 image and tile\n");
fprintf(stdout,"headers of a sample frame. Excluded: low-level packed-bits image data.\n\n");
fprintf(stdout,"By Default\n");
fprintf(stdout,"----------\n");
fprintf(stdout,"The metadata includes the jp2 image and tile headers of the first frame.\n");
fprintf(stdout,"\n");
fprintf(stdout,"Metadata values are shown in 'raw' form (e.g., hexidecimal) as stored in the\n");
fprintf(stdout,"file, and, if apt, in a 'derived' form that is more quickly grasped.\n");
fprintf(stdout,"\n");
fprintf(stdout,"Notes explaining the XML are embedded as terse comments. These include\n");
fprintf(stdout," meaning of non-obvious tag abbreviations;\n");
fprintf(stdout," range and precision of valid values;\n");
fprintf(stdout," interpretations of values, such as enumerations; and\n");
fprintf(stdout," current implementation limitations.\n");
fprintf(stdout,"\n");
fprintf(stdout,"The sample-size and chunk-offset tables, each with 1 row per frame, are not reported.\n");
fprintf(stdout,"\n");
fprintf(stdout,"The file is self-contained and no verification (e.g., against a DTD) is requested.\n");
fprintf(stdout,"\n");
fprintf(stdout,"Required Parameters (except with -h)\n");
fprintf(stdout,"------------------------------------\n");
fprintf(stdout,"[Caution: file strings that contain spaces should be wrapped with quotes.]\n");
fprintf(stdout,"-i input.mj2 : where 'input' is any source file name or path.\n");
fprintf(stdout," MJ2 files created with 'frames_to_mj2' are supported so far.\n");
fprintf(stdout," These are silent, single-track, 'MJ2 Simple Profile' videos.\n");
fprintf(stdout,"-o output.xml : where 'output' is any destination file name or path.\n");
fprintf(stdout,"\n");
fprintf(stdout,"Optional Parameters\n");
fprintf(stdout,"-------------------\n");
fprintf(stdout,"-h : Display this help information.\n");
fprintf(stdout,"-n : Suppress all mj2_to_metadata notes.\n");
fprintf(stdout,"-t : Include sample-size and chunk-offset tables.\n");
fprintf(stdout,"-f n : where n > 0. Include jp2 header info for frame n [default=1].\n");
fprintf(stdout,"-f 0 : No jp2 header info.\n");
fprintf(stdout,"-r : Suppress all 'raw' data for which a 'derived' form exists.\n");
fprintf(stdout,"-d : Suppress all 'derived' data.\n");
fprintf(stdout," (If both -r and -d given, -r will be ignored.)\n");
fprintf(stdout,"-v string : Verify against the DTD file located by the string.\n");
fprintf(stdout," Prepend quoted 'string' with either SYSTEM or PUBLIC keyword.\n");
fprintf(stdout," Thus, for the distributed DTD placed in the same directory as\n");
fprintf(stdout," the output file: -v \"SYSTEM mj2_to_metadata.dtd\"\n");
fprintf(stdout," \"PUBLIC\" is used with an access protocol (e.g., http:) + URL.\n");
/* More to come */
fprintf(stdout,"\n");
/* "1234567890123456789012345678901234567890123456789012345678901234567890123456789" */
}
/* ------------- */
int main(int argc, char *argv[]) {
FILE *file, *xmlout;
/* char xmloutname[50]; */
mj2_movie_t movie;
char* infile = 0;
char* outfile = 0;
char* s, S1, S2, S3;
int len;
unsigned int sampleframe = 1; /* First frame */
char* stringDTD = NULL;
BOOL notes = TRUE;
BOOL sampletables = FALSE;
BOOL raw = TRUE;
BOOL derived = TRUE;
#ifndef NO_PACKETS_DECODING
fprintf(stdout,"WARNING: For best performance, define NO_PACKETS_DECODING in preprocessing.\n");
#endif
while (TRUE) {
/* ':' after letter means it takes an argument */
int c = getopt(argc, argv, "i:o:f:v:hntrd");
/* FUTURE: Reserve 'p' for pruning file (which will probably make -t redundant) */
if (c == -1)
break;
switch (c) {
case 'i': /* IN file */
infile = optarg;
s = optarg;
while (*s) { s++; } /* Run to filename end */
s--;
S3 = *s;
s--;
S2 = *s;
s--;
S1 = *s;
if ((S1 == 'm' && S2 == 'j' && S3 == '2')
|| (S1 == 'M' && S2 == 'J' && S3 == '2')) {
break;
}
fprintf(stderr, "Input file name must have .mj2 extension, not .%c%c%c.\n", S1, S2, S3);
return 1;
/* ----------------------------------------------------- */
case 'o': /* OUT file */
outfile = optarg;
while (*outfile) { outfile++; } /* Run to filename end */
outfile--;
S3 = *outfile;
outfile--;
S2 = *outfile;
outfile--;
S1 = *outfile;
outfile = optarg;
if ((S1 == 'x' && S2 == 'm' && S3 == 'l')
|| (S1 == 'X' && S2 == 'M' && S3 == 'L'))
break;
fprintf(stderr,
"Output file name must have .xml extension, not .%c%c%c\n", S1, S2, S3);
return 1;
/* ----------------------------------------------------- */
case 'f': /* Choose sample frame. 0 = none */
sscanf(optarg, "%u", &sampleframe);
break;
/* ----------------------------------------------------- */
case 'v': /* Verification by DTD. */
stringDTD = optarg;
/* We will not insist upon last 3 chars being "dtd", since non-file
access protocol may be used. */
if(strchr(stringDTD,'"') != NULL) {
fprintf(stderr, "-D's string must not contain any embedded double-quote characters.\n");
return 1;
}
if (strncmp(stringDTD,"PUBLIC ",7) == 0 || strncmp(stringDTD,"SYSTEM ",7) == 0)
break;
fprintf(stderr, "-D's string must start with \"PUBLIC \" or \"SYSTEM \"\n");
return 1;
/* ----------------------------------------------------- */
case 'n': /* Suppress comments */
notes = FALSE;
break;
/* ----------------------------------------------------- */
case 't': /* Show sample size and chunk offset tables */
sampletables = TRUE;
break;
/* ----------------------------------------------------- */
case 'h': /* Display an help description */
help_display();
return 0;
/* ----------------------------------------------------- */
case 'r': /* Suppress raw data */
raw = FALSE;
break;
/* ----------------------------------------------------- */
case 'd': /* Suppress derived data */
derived = FALSE;
break;
/* ----------------------------------------------------- */
default:
return 1;
} /* switch */
} /* while */
if(!raw && !derived)
raw = TRUE; /* At least one of 'raw' and 'derived' must be true */
/* Error messages */
/* -------------- */
if (!infile || !outfile) {
fprintf(stderr,"Correct usage: mj2_to_metadata -i mj2-file -o xml-file (plus options)\n");
return 1;
}
/* was:
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_to_metadata inputfile.mj2 outputfile.xml\n");
printf("Example: MJ2_to_metadata foreman.mj2 foreman.xml\n");
return 1;
}
*/
len = strlen(infile);
if(infile[0] == ' ')
{
infile++; /* There may be a leading blank if user put space after -i */
}
file = fopen(infile, "rb"); /* was: argv[1] */
if (!file) {
fprintf(stderr, "Failed to open %s for reading.\n", infile); /* was: argv[1] */
return 1;
}
len = strlen(outfile);
if(outfile[0] == ' ')
{
outfile++; /* There may be a leading blank if user put space after -o */
}
// Checking output file
xmlout = fopen(outfile, "w"); /* was: argv[2] */
if (!xmlout) {
fprintf(stderr, "Failed to open %s for writing.\n", outfile); /* was: argv[2] */
return 1;
}
// Leave it open
if (mj2_read_struct(file, &movie)) // Creating the movie structure
{
fclose(xmlout);
return 1;
}
xml_write_init(notes, sampletables, raw, derived);
xml_write_struct(file, xmlout, &movie, sampleframe, stringDTD);
fclose(xmlout);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}

276
mj2/mj2_to_metadata.dsp Normal file
View File

@@ -0,0 +1,276 @@
# Microsoft Developer Studio Project File - Name="mj2_to_metadata" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
CFG=mj2_to_metadata - Win32 Debug
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "mj2_to_metadata.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "mj2_to_metadata.mak" CFG="mj2_to_metadata - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "mj2_to_metadata - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "mj2_to_metadata - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "mj2_to_metadata - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c
# ADD CPP /nologo /W3 /GX /O2 /I "../libopenjpeg" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /YX /FD /c
# ADD BASE RSC /l 0x809 /d "NDEBUG"
# ADD RSC /l 0x809 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
!ELSEIF "$(CFG)" == "mj2_to_metadata - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "mj2_to_metadata___Win32_Debug0"
# PROP BASE Intermediate_Dir "mj2_to_metadata___Win32_Debug0"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "mj2_to_metadata___Win32_Debug0"
# PROP Intermediate_Dir "mj2_to_metadata___Win32_Debug0"
# PROP Target_Dir ""
# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c
# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "../libopenjpeg" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "DONT_HAVE_GETOPT" /FR /YX /FD /GZ /c
# ADD BASE RSC /l 0x809 /d "_DEBUG"
# ADD RSC /l 0x809 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept
!ENDIF
# Begin Target
# Name "mj2_to_metadata - Win32 Release"
# Name "mj2_to_metadata - Win32 Debug"
# Begin Group "MJ2"
# PROP Default_Filter ""
# Begin Group "MJ2 Header Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.h
# End Source File
# Begin Source File
SOURCE=.\meta_out.h
# End Source File
# Begin Source File
SOURCE=.\mj2.h
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.h
# End Source File
# End Group
# Begin Group "MJ2 Source Files"
# PROP Default_Filter ""
# Begin Source File
SOURCE=.\compat\getopt.c
# End Source File
# Begin Source File
SOURCE=.\meta_out.c
# End Source File
# Begin Source File
SOURCE=.\mj2.c
# End Source File
# Begin Source File
SOURCE=.\mj2_convert.c
# End Source File
# Begin Source File
SOURCE=.\mj2_to_metadata.c
# End Source File
# End Group
# End Group
# Begin Group "OpenJPEG"
# PROP Default_Filter ""
# Begin Group "OpenJPEG Header Files"
# PROP Default_Filter ".h"
# Begin Source File
SOURCE=..\libopenjpeg\bio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\openjpeg.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.h
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.h
# End Source File
# End Group
# Begin Group "OpenJPEG Source Files"
# PROP Default_Filter ".c"
# Begin Source File
SOURCE=..\libopenjpeg\bio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\cio.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\dwt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\fix.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\int.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\j2k.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jp2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\jpt.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mct.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\mqc.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\pi.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\raw.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t1.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\t2.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tcd.c
# End Source File
# Begin Source File
SOURCE=..\libopenjpeg\tgt.c
# End Source File
# End Group
# End Group
# End Target
# End Project

425
mj2/mj2_to_metadata.dtd Normal file
View File

@@ -0,0 +1,425 @@
<!--
Preliminary DTD for Open JPEG "mj2_to_metadata" function.
Last revised: April 20, 2005
Author: Glenn Pearson, at U.S. National Library of Medicine.
See mj2_to_metadata.c regarding copyright, license, disclaimer status.
While the vocabulary of this DTD is based on the relevant portions of the
ISO/IEC JPEG 200 standard, the detailed representation is the author's own.
It is neither an ISO/IEC nor NLM official or consensus representation.
Furthermore, it deviates from a pure representation of the ISO/IEC standard
in these aspects:
- it is incomplete in a number of ways (which to some extent may be
addressed over time);
- it has extensions for "derived" data and "statistics";
- it is more flexible. That is, some elements are marked as optional
not because they are optional in an MJ2 file, but because reporting
of them is optional based on current or projected mj2_to_metadata
command-line flags.
-->
<!ELEMENT MJ2_File (JP2?, FileType?, MovieBox?)>
<!ELEMENT JP2 EMPTY>
<!ATTLIST JP2 BoxType CDATA #FIXED "jP[space][space]">
<!ATTLIST JP2 Signature CDATA #FIXED "0x0d0a870a">
<!ELEMENT FileType (Brand, MinorVersion, CompatibilityList)>
<!ATTLIST FileType BoxType CDATA #FIXED "ftyp">
<!ELEMENT Brand (#PCDATA)> <!-- 4 characters max -->
<!ELEMENT MinorVersion (#PCDATA)> <!-- 4 chararcters max -->
<!ELEMENT CompatibilityList (CompatibleBrand)*>
<!ATTLIST CompatibilityList Count CDATA #REQUIRED> <!-- Count >= 0 -->
<!ELEMENT CompatibleBrand (#PCDATA)> <!-- 4 characters max -->
<!ELEMENT MovieBox (MovieHeader, Statistics?, Track*)>
<!ATTLIST MovieBox BoxType CDATA #FIXED "moov">
<!ELEMENT MovieHeader (CreationTime, ModificationTime, Timescale, Rate, Duration, Volume, TransformationMatrix)>
<!ATTLIST MovieHeader BoxType CDATA #FIXED "mvhd">
<!ELEMENT CreationTime (InSeconds?,AsLocalTime?)>
<!ELEMENT InSeconds (#PCDATA)>
<!ELEMENT AsLocalTime (#PCDATA)>
<!ELEMENT ModificationTime (InSeconds?,AsLocalTime?)>
<!ELEMENT Timescale (#PCDATA)> <!-- Timescale defines time units in one second -->
<!ELEMENT Rate (AsHex | (AsHex, AsDecimal) | AsDecimal)> <!-- Decimal is Approximation; Optional on input. -->
<!ELEMENT AsHex (#PCDATA)>
<!ELEMENT AsDecimal (#PCDATA)>
<!ELEMENT Duration (InTimeUnits | (InTimeUnits, InSeconds) | InSeconds)> <!-- InSeconds Optional on input. -->
<!ELEMENT InTimeUnits (#PCDATA)>
<!ELEMENT Volume (AsHex | (AsHex, AsDecimal) | AsDecimal)> <!-- hex default = 0x0100 -->
<!-- Fixed 8.8 value of audio volume. Full, normal value is 1.0 (0x0100) -->
<!ELEMENT TransformationMatrix (TMa,TMb,TMu,TMc,TMd,TMv,TMx,TMy,TMw)> <!-- for video -->
<!-- 3 x 3 Video Transformation Matrix {a,b,u,c,d,v,x,y,w}. Required: u=0, v=0, w=1 -->
<!-- Maps decompressed point (p,q) to rendered point (ap + cq + x, bp + dq + y) -->
<!-- Stored as Fixed Point Hex: all are 16.16, except u,v,w are 2.30 -->
<!-- Unity = 0x00010000,0,0,0,0x00010000,0,0,0,0x40000000 -->
<!ELEMENT TMa (#PCDATA)>
<!ELEMENT TMb (#PCDATA)>
<!ELEMENT TMu (#PCDATA)> <!--Always "0x00000000" -->
<!ELEMENT TMc (#PCDATA)>
<!ELEMENT TMd (#PCDATA)>
<!ELEMENT TMv (#PCDATA)> <!--Always "0x00000000" -->
<!ELEMENT TMx (#PCDATA)>
<!ELEMENT TMy (#PCDATA)>
<!ELEMENT TMw (#PCDATA)> <!--Always "0x40000000" -->
<!ELEMENT Statistics (TracksFound)>
<!ELEMENT TracksFound (Video,Audio,Hint)>
<!ELEMENT Video (#PCDATA)>
<!ELEMENT Audio (#PCDATA)>
<!ELEMENT Hint (#PCDATA)>
<!-- For now, output info on at most one video track -->
<!ELEMENT Track (TrackHeader, TrackReferenceContainer?, EditListContainer?, Media, JP2_Frame?)> <!-- JP2_Frame is mj2_to_metadata extension -->
<!ATTLIST Track BoxType CDATA #FIXED "trak">
<!ATTLIST Track Instance CDATA #REQUIRED>
<!ELEMENT TrackHeader (TrackID, TrackLayer?, Volume?, TransformationMatrix?, Width?, Height?)>
<!ATTLIST TrackHeader BoxType CDATA #FIXED "tkhd">
<!-- Not shown here: CreationTime, ModificationTime, Duration. -->
<!-- These 3 fields are reported under MediaHeader below. When reading these 3, -->
<!-- m2j_to_metadata currently doesn't distinguish between TrackHeader and MediaHeader source. -->
<!-- If both found, value read from MediaHeader is used. -->
<!ELEMENT TrackID (#PCDATA)>
<!ELEMENT TrackLayer (#PCDATA)> <!-- front-to-back ordering of video tracks. 0 = normal, -1 is closer, etc. -->
<!-- "Volume" element described above; here it is for particular audio track. Full, normal (default = 0x0100) -->
<!-- "TransformationMatrix" element described above; matrix here is applied before MovieHeader one. -->
<!ELEMENT Width (AsHex | (AsHex, AsDecimal) | AsDecimal)>
<!ELEMENT Height (AsHex | (AsHex, AsDecimal) | AsDecimal)>
<!-- AsHex, AsDecimal already defined above -->
<!-- Width and Height are for the presentation; frames will be scaled to this -->
<!-- /TrackHeader -->
<!ELEMENT TrackReferenceContainer ANY> <!-- TO DO: TrackReferenceContainer 'tref' just used in hint track -->
<!ELEMENT EditListContainer ANY> <!-- TO DO: EditListContainer 'edts', contains EditList 'elst' with media-time, segment-duration, media-rate -->
<!ELEMENT Media (MediaHeader, HandlerReference,MediaInfoContainer)>
<!ATTLIST Media BoxType CDATA #FIXED "mdia">
<!ELEMENT MediaHeader (CreationTime,ModificationTime,Timescale,Duration,Language)>
<!ATTLIST MediaHeader BoxType CDATA #FIXED "mdhd">
<!-- Elements already defined above: CreationTime, ModificationTime, Timescale, Duration -->
<!ELEMENT Language (#PCDATA)> <!-- 3 chars max. There's an enumeration available -->
<!ELEMENT HandlerReference (HandlerType)>
<!ATTLIST HandlerReference BoxType CDATA #FIXED "hdlr">
<!ELEMENT HandlerType (#PCDATA)>
<!ATTLIST HandlerType Code ( vide | soun | hint ) "vide">
<!-- make the media headers multiple? -->
<!ELEMENT MediaInfoContainer ((VideoMediaHeader | SoundMediaHeader | HintMediaHeader), DataInfo, SampleTable)>
<!ATTLIST MediaInfoContainer BoxType CDATA #FIXED "minf">
<!ELEMENT VideoMediaHeader (GraphicsMode, Opcolor)>
<!ATTLIST VideoMediaHeader BoxType CDATA #FIXED "vmhd">
<!ELEMENT GraphicsMode (#PCDATA)>
<!-- Enumerated values of graphics mode: -->
<!-- 0x00 = copy (over existing image); -->
<!-- 0x24 = transparent; 'blue-screen' this image using opcolor; -->
<!-- 0x100 = alpha; alpha-blend this image -->
<!-- 0x101 = whitealpha; alpha-blend this image, which has been blended with white; -->
<!-- 0x102 = blackalpha; alpha-blend this image, which has been blended with black. -->
<!ELEMENT Opcolor (Red,Green,Blue)>
<!ELEMENT Red (#PCDATA)>
<!ELEMENT Green (#PCDATA)>
<!ELEMENT Blue (#PCDATA)>
<!ELEMENT SoundMediaHeader (Balance)>
<!ATTLIST SoundMediaHeader BoxType CDATA #FIXED "smhd">
<!ELEMENT Balance (#PCDATA)>
<!-- Fixed Point 8.8, fixes mono track in stereo space. -->
<!-- 0.0 = center, -1.0 = full left, 1.0 = full right -->
<!ELEMENT HintMediaHeader (MaxPDU_Size, AvgPDU_Size, MaxBitRate, AvgBitRate, SlidingAvgBitRate)>
<!ATTLIST HintMediaHeader BoxType CDATA #FIXED "hmhd">
<!ELEMENT MaxPDU_Size (#PCDATA)>
<!-- Size in bytes of largest PDU in this hint stream. -->
<!ELEMENT AvgPDU_Size (#PCDATA)>
<!-- Average size in bytes of a PDU over the entire presentation. -->
<!ELEMENT MaxBitRate (#PCDATA)>
<!-- Maximum rate in bits per second over any window of 1 second. -->
<!ELEMENT AvgBitRate (#PCDATA)>
<!-- Averate rate in bits per second over the entire presentation. -->
<!ELEMENT SlidingAvgBit (#PCDATA)>
<!-- Maximum rate in bits per second over any window of one minute. -->
<!ELEMENT DataInfo (DataReference)>
<!ATTLIST DataInfo BoxType CDATA #FIXED "dinf">
<!ELEMENT DataReference (DataEntryUrlBox | DataEntryUrnBox )*>
<!ATTLIST DataReference BoxType CDATA #FIXED "dref">
<!ATTLIST DataReference URL_Count CDATA #REQUIRED>
<!ATTLIST DataReference URN_Count CDATA #REQUIRED> <!-- table w. flags, URLs, URNs -->
<!-- // Data structure does not distinguish between single URL, single URN, or DREF table or URLs & URNs.
// We could infer those, but for now just present everything as a DREF table.
-->
<!-- No entries here mean that file is self-contained, as required by Simple Profile. -->
<!ELEMENT DataEntryUrlBox (Location)>
<!ATTLIST DataEntryUrlBox BoxType CDATA #FIXED "url[space]"> <!-- table w. flags, URLs, URNs -->
<!-- Only the first 16 bytes of URL location are recorded in mj2_to_metadata data structure. -->
<!ELEMENT DataEntryUrnBox (Name, Location?)>
<!ATTLIST DataEntryUrnBox BoxType CDATA #FIXED "urn[space]\">
<!-- Only the first 16 bytes each of URN name and optional location are recorded in mj2_to_metadata data structure. -->
<!ELEMENT SampleTable (VisualSampleEntry,TimeToSample,SampleToChunk,SampleSize,ChunkOffset)> <!-- structure doesn't do non-visual sample entry yet -->
<!ATTLIST SampleTable BoxType CDATA #FIXED "stbl"> <!-- to add: entry count -->
<!-- Next are instances of generic SampleDescription BoxType=\"stsd\" -->
<!-- There could be multiple instances of this, but "entry_count" is just a local at read-time.
And it's used wrong, too, as count of just visual type, when it's really all 3 types.
This is referred to as "smj2" within mj2.c -->
<!ELEMENT VisualSampleEntry (WidthAsInteger, HeightAsInteger, HorizontalRes, VerticalRes, CompressorName, Depth, JP2Header?, FieldCoding?, MJP2_Profile?, MJP2_Prefix?, MJP2_SubSampling?, MJP2_OriginalFormat?)>
<!ATTLIST VisualSampleEntry BoxType CDATA #FIXED "mjp2">
<!-- If multiple instances of this, only first is shown here. -->
<!ELEMENT WidthAsInteger (#PCDATA)>
<!ELEMENT HeightAsInteger (#PCDATA)>
<!ELEMENT HorizontalRes (AsHex | (AsHex, AsDecimal) | AsDecimal)>
<!ELEMENT VerticalRes (AsHex | (AsHex, AsDecimal) | AsDecimal)>
<!-- Typical value for both resolution is 72 (0x00480000) -->
<!ELEMENT CompressorName (#PCDATA)>
<!-- Compressor name for debugging. Standard restricts max length to 31 bytes. -->
<!-- Usually blank or \"Motion JPEG2000\" -->
<!ELEMENT Depth (#PCDATA)>
<!-- Depth is: -->
<!-- 0x20: alpha channels present (color or grayscale) -->
<!-- 0x28: grayscale without alpha -->
<!-- 0x18: color without alpha -->
<!-- TODO somewhere: tk->jp2_struct.numcomps -->
<!ELEMENT JP2Header (ImageHeader, ColourSpecification)>
<!ATTLIST JP2Header BoxType CDATA #FIXED "jp2h">
<!ELEMENT ImageHeader (HEIGHT, WIDTH, NC, BPC, C, UnkC, IPR)>
<!ATTLIST ImageHeader BoxType CDATA #FIXED "ihdr">
<!ELEMENT HEIGHT (#PCDATA)> <!-- If 2 fields/frame, total deinterlaced height -->
<!ELEMENT WIDTH (#PCDATA)>
<!ELEMENT NC (#PCDATA)> <!-- number of components -->
<!ELEMENT BPC (AsHex | (AsHex,BitsPerPixel,Signed) | (BitsPerPixel,Signed))>
<!ELEMENT BitsPerPixel (#PCDATA)>
<!ELEMENT Signed (#PCDATA)>
<!ELEMENT C (#PCDATA)> <!-- Compression type. Only "7" defined -->
<!ELEMENT UnkC (#PCDATA)> <!-- Colourspace Unknown. 1 = unknown, 0 = known -->
<!ELEMENT IPR (#PCDATA)> <!-- 1 = frame has Intellectual Prop. box; otherwise 0 -->
<!ELEMENT ColourSpecification (METH, PREC, APPROX, EnumCS)>
<!ATTLIST ColourSpecification BoxType CDATA #FIXED "colr">
<!ELEMENT METH (#PCDATA)> <!-- 1 = EnumCS field; 2 = PROFILE field (not yet generated) -->
<!ELEMENT PREC (#PCDATA)> <!-- precedence must be 0 so far -->
<!ELEMENT APPROX (#PCDATA)> <!-- colourspace approximation must be 0 so far -->
<!ELEMENT EnumCS (#PCDATA)> <!-- Valid enumerated MJ2 colourspaces: 16 (sRGB), 17 (grey sRGB), 18 (YCC) -->
<!-- Following subboxes are optional -->
<!ELEMENT FieldCoding (FieldCount, FieldOrder)>
<!ATTLIST FieldCoding BoxType CDATA #FIXED "fiel">
<!ELEMENT FieldCount (#PCDATA)>
<!-- Must be either 1 or 2 -->
<!ELEMENT FieldOrder (#PCDATA)>
<!-- When FieldCount=2, FieldOrder means: -->
<!-- 0: Field coding unknown -->
<!-- 1: Field with topmost line is stored first in sample; fields are in temporal order -->
<!-- 6: Field with topmost line is stored second in sample; fields are in temporal order -->
<!-- Defaults: FieldCount=1, FieldOrder=0 if FieldCoding box not present -->
<!-- Current implementation doesn't retain whether box was actually present. -->
<!ELEMENT MJP2_Profile (CompatibleBrand*)>
<!ATTLIST MJP2_Profile BoxType CDATA #FIXED "jp2p">
<!ATTLIST MJP2_Profile Count CDATA #REQUIRED>
<!ELEMENT MJP2_Prefix (Data*)>
<!ATTLIST MJP2_Prefix BoxType CDATA #FIXED "jp2x">
<!ATTLIST MJP2_Prefix Count CDATA #REQUIRED>
<!-- We'll probably need better formatting than this -->
<!ELEMENT Data (#PCDATA)> <!-- Multiple. Each entry is single byte -->
<!ELEMENT MJP2_SubSampling (HorizontalSub, VerticalSub, HorizontalOffset, VerticalOffset)>
<!ATTLIST MJP2_SubSampling BoxType CDATA #FIXED "jsub">
<!-- These values are all 1 byte -->
<!-- Typical subsample value is 2 for 4:2:0 -->
<!ELEMENT HorizontalSub (#PCDATA)>
<!ELEMENT VerticalSub (#PCDATA)>
<!ELEMENT HorizontalOffset (#PCDATA)>
<!ELEMENT VerticalOffset (#PCDATA)>
<!ELEMENT MJP2_OriginalFormat (OriginalFieldCount, OriginalFieldOrder)>
<!ATTLIST MJP2_OriginalFormat BoxType CDATA #FIXED "orfo"> <!-- Part III Appx. 2 -->
<!ELEMENT OriginalFieldCount (#PCDATA)>
<!-- In original material before encoding. Must be either 1 or 2 -->
<!ELEMENT OriginalFieldOrder (#PCDATA)>
<!-- When FieldCount=2, FieldOrder means: -->
<!-- 0: Field coding unknown -->
<!-- 11: Topmost line came from the earlier field; -->
<!-- 16: Topmost line came form the later field. -->
<!-- Defaults: FieldCount=1, FieldOrder=0 if FieldCoding box not present -->
<!-- Current implementation doesn't retain whether box was actually present. -->
<!-- mj2_to_metadata's data structure doesn't record Audio and Hint sample data currently. -->
<!-- Within SampleTable: -->
<!ELEMENT TimeToSample (SampleStatistics, SampleEntries)>
<!ATTLIST TimeToSample BoxType CDATA #FIXED "stts">
<!ELEMENT SampleStatistics (TotalSamples)> <!-- Not part of standard -->
<!ELEMENT TotalSamples (#PCDATA)>
<!-- For video, gives the total frames in the track, by summing all entries in the Sample Table -->
<!ELEMENT SampleEntries (Table*)>
<!ATTLIST SampleEntries EntryCount CDATA #REQUIRED>
<!ELEMENT Table EMPTY> <!-- Multiple. Attributes have values -->
<!ATTLIST Table Entry CDATA #REQUIRED>
<!ATTLIST Table SampleCount CDATA #REQUIRED>
<!ATTLIST Table SampleDelta CDATA #REQUIRED>
<!-- Within SampleTable: -->
<!ELEMENT SampleToChunk (FirstChunk,SamplesPerChunk,SampleDescrIndex)>
<!ATTLIST SampleToChunk BoxType CDATA #FIXED "stsc">
<!ATTLIST SampleToChunk Count CDATA #REQUIRED>
<!ELEMENT FirstChunk (#PCDATA)>
<!ELEMENT SamplesPerChunk (#PCDATA)>
<!ELEMENT SampleDescrIndex (#PCDATA)>
<!ELEMENT SampleSize (Sample_Size,Sample_Count,EntrySize*)>
<!ATTLIST SampleSize BoxType CDATA #FIXED "stsz">
<!ELEMENT Sample_Size (#PCDATA)>
<!ELEMENT Sample_Count (#PCDATA)>
<!ELEMENT EntrySize (#PCDATA)> <!-- appears multiply, but only with mj2_to_metadata option -t -->
<!ATTLIST EntrySize Num CDATA #REQUIRED>
<!ELEMENT ChunkOffset (EntryCount, Chunk_Offset*)>
<!ATTLIST ChunkOffset BoxType CDATA #FIXED "stco">
<!ELEMENT EntryCount (#PCDATA)>
<!ELEMENT Chunk_Offset (#PCDATA)> <!-- appears multiply, but only with mj2_to_metadata option -t -->
<!ATTLIST Chunk_Offset Num CDATA #REQUIRED>
<!-- </SampleTable> </MediaInfoContainer> </Media> -->
<!-- TO DO: optional UserData 'udat', can contain multiple Copyright 'cprt' -->
<!-- Optional, and only for Visual Track: given individual frame -->
<!ELEMENT JP2_Frame (MainHeader, TilePartHeaders)>
<!ATTLIST JP2_Frame Num CDATA #REQUIRED>
<!ELEMENT MainHeader (StartOfCodestream,ImageAndFileSize,CodingStyleDefault,QuantizationDefault,QuantizationComponent*,RegionOfInterest?,ProgressionOrderChange*)>
<!ELEMENT StartOfCodestream EMPTY>
<!ATTLIST StartOfCodestream Marker CDATA #FIXED "SOC">
<!ELEMENT ImageAndFileSize (Xsiz,Ysiz,XOsiz,YOsiz,XTsiz,YTsiz,XTOsiz,YTOsiz,Csiz,Component+)>
<!ATTLIST ImageAndFileSize Marker CDATA #FIXED "SIZ">
<!ELEMENT Xsiz (#PCDATA)>
<!ELEMENT Ysiz (#PCDATA)> <!-- Xsiz, Ysiz is the size of the reference grid. -->
<!ELEMENT XOsiz (#PCDATA)>
<!ELEMENT YOsiz (#PCDATA)> <!-- XOsiz, YOsiz are offsets from grid origin to image origin. -->
<!ELEMENT XTsiz (#PCDATA)>
<!ELEMENT YTsiz (#PCDATA)> <!-- XTsiz, YTsiz is the size of one tile with respect to the grid. -->
<!ELEMENT XTOsiz (#PCDATA)>
<!ELEMENT YTOsiz (#PCDATA)> <!-- XTOsiz, YTOsiz are offsets from grid origin to first tile origin. -->
<!ELEMENT Csiz (#PCDATA)> <!-- Csiz is the number of components in the image. -->
<!-- For image components next -->
<!ELEMENT Component (Ssiz,XRsiz,YRsiz,WidthOfData,HeightOfData)>
<!ATTLIST Component Num CDATA #REQUIRED>
<!ELEMENT Ssiz (AsHex | (AsHex,Signed,PrecisionInBits) | (Signed,PrecisionInBits))>
<!-- Signed already defined -->
<!ELEMENT PrecisionInBits (#PCDATA)> <!-- Bits per pixel (bpp) or pixel depth. -->
<!ELEMENT XRsiz (#PCDATA)>
<!ELEMENT YRsiz (#PCDATA)> <!-- XRsiz, YRsiz denote pixel-sample-spacing on the grid, per Part I Annex B. -->
<!ELEMENT WidthOfData (#PCDATA)>
<!ELEMENT HeightOfData (#PCDATA)> <!-- WidthOfData and HeightOfData are calculated values, e.g.: w = roundup((Xsiz - XOsiz)/ XRsiz) -->
<!-- -->
<!ELEMENT CodingStyleDefault (Scod,SGcod,SPcod)>
<!ATTLIST CodingStyleDefault Marker CDATA #FIXED "COD">
<!ELEMENT Scod (#PCDATA)>
<!-- For Scod, specific bits mean (where bit 0 is lowest or rightmost): -->
<!-- bit 0: Defines entropy coder precincts -->
<!-- 0 = (PPx=15, PPy=15); 1 = precincts defined below. -->
<!-- bit 1: 1 = SOP marker may be used; 0 = not. -->
<!-- bit 2: 1 = EPH marker may be used; 0 = not. -->
<!ELEMENT SGcod (ProgressionOrder,NumberOfLayers,MultipleComponentTransformation)>
<!ELEMENT ProgressionOrder (#PCDATA)>
<!-- Defined Progression Order Values are: -->
<!-- 0 = LRCP; 1 = RLCP; 2 = RPCL; 3 = PCRL; 4 = CPRL -->
<!-- where L = "layer", R = "resolution level", C = "component", P = "position". -->
<!ELEMENT NumberOfLayers (#PCDATA)>
<!ELEMENT MultipleComponentTransformation (#PCDATA)>
<!-- For MCT, 0 = none, 1 = transform first 3 components for efficiency, per Part I Annex G -->
<!ELEMENT SPcod (NumberOfDecompositionLevels,CodeblockWidth,CodeblockHeight,CodeblockStyle,Transformation)>
<!ELEMENT NumberOfDecompositionLevels (#PCDATA)>
<!ELEMENT CodeblockWidth (#PCDATA)> <!-- CBW and CBH are non-negative, and summed cannot exceed 8 -->
<!ELEMENT CodeblockHeight (#PCDATA)> <!-- Codeblock dimension is 2^(value + 2) -->
<!ELEMENT CodeblockStyle (#PCDATA)>
<!-- For CodeblockStyle, bits mean (with value 1=feature on, 0=off): -->
<!-- bit 0: Selective arithmetic coding bypass. -->
<!-- bit 1: Reset context probabilities on coding pass boundaries. -->
<!-- bit 2: Termination on each coding pass. -->
<!-- bit 3: Vertically causal context. -->
<!-- bit 4: Predictable termination. -->
<!-- bit 5: Segmentation symbols are used. -->
<!ELEMENT Transformation (#PCDATA)> <!-- For Transformation, 0="9-7 irreversible filter", 1="5-3 reversible filter" -->
<!-- mj2_to_metadata implementation always reports component[0] as using default COD, -->
<!-- and any other component, with main-header style values different from [0], as COC. -->
<!ELEMENT QuantizationDefault (Sqcd,SPqcd)>
<!ATTLIST QuantizationDefault Marker CDATA #FIXED "QCD">
<!ELEMENT Sqcd (AsHex | (AsHex,QuantizationStyle,NumberOfGuardBits) | (QuantizationStyle,NumberOfGuardBits))>
<!ELEMENT QuantizationStyle (#PCDATA)> <!-- Default quantization style for all components. -->
<!-- Quantization style (in Sqcd's low 5 bits) may be: -->
<!-- 0 = No quantization. SPqcd size = 8 bits-->
<!-- 1 = Scalar derived (values signaled for N(L)LL subband only). Use Eq. E.5. SPqcd size = 16. -->
<!-- 2 = Scalar expounded (values signaled for each subband). SPqcd size = 16. -->
<!ELEMENT NumberOfGuardBits (#PCDATA)> <!-- 0-7 guard bits allowed (stored in Sqcd's high 3 bits) -->
<!ELEMENT SPqcd (ReversibleStepSizeValue | QuantizationStepSizeValues )> <!-- TO DO: Irreversible choices -->
<!ELEMENT ReversibleStepSizeValue (DynamicRangeExponent+)>
<!-- Current mj2_to_metadata implementation dumps entire internal table, -->
<!-- until an exponent with zero value is reached. -->
<!-- Exponent epsilon(b) of reversible dynamic range. -->
<!-- Hex value is as stored, in high-order 5 bits. -->
<!ELEMENT DynamicRangeExponent (AsHex | (AsHex, AsDecimal) | AsDecimal)>
<!ATTLIST DynamicRangeExponent Subband CDATA #REQUIRED>
<!ELEMENT QuantizationStepSizeValues (QuantizationValues+, CalculatedExponent*)> <!-- Calculated exponents iff only subband 0 reported -->
<!ELEMENT QuantizationValues (AsHex | (AsHex,Exponent,Mantissa) | (Exponent,Mantissa))>
<!ATTLIST QuantizationValues Subband CDATA #REQUIRED>
<!ELEMENT Exponent (#PCDATA)>
<!ELEMENT Mantissa (#PCDATA)>
<!ELEMENT CalculatedExponent (#PCDATA)>
<!ATTLIST CalculatedExponent Subband CDATA #REQUIRED>
<!-- /QuantizationDefault -->
<!-- mj2_to_metadata implementation always reports component[0] as using default QCD, -->
<!-- and any other component, with main-header quantization values different from [0], as QCC. -->
<!ELEMENT QuantizationComponent (Sqcc,SPqcc)>
<!ATTLIST QuantizationComponent Marker CDATA #FIXED "QCC">
<!ATTLIST QuantizationComponent Component CDATA #REQUIRED>
<!ELEMENT Sqcc (AsHex | (AsHex,QuantizationStyle,NumberOfGuardBits) | (QuantizationStyle,NumberOfGuardBits))>
<!ELEMENT SPqcc (ReversibleStepSizeValue | QuantizationStepSizeValues )> <!-- TO DO: Irreversible choices -->
<!-- /QuantizationComponent -->
<!-- Don't know if MJ2 files can have regions of interest. Assume yes -->
<!ELEMENT RegionOfInterest (Srgn,Crgn,Sprgn)> <!-- Optional in main header, at most 1 per component -->
<!ATTLIST RegionOfInterest Marker CDATA #FIXED "RGN">
<!ELEMENT Srgn (#PCDATA)> <!-- ROI style. Only style=0 defined: Implicit ROI (max. shift) -->
<!ELEMENT Crgn (#PCDATA)> <!-- Zero-based component number. -->
<!ELEMENT SPrgn (#PCDATA)> <!-- Implicit ROI shift, i.e., binary shifting of ROI coefficients above background. -->
<!-- </RegionOfInterest> -->
<!ELEMENT ProgressionOrderChange (Progression+)> <!-- Optional in main header, at most 1 per component (but impl allows more?) -->
<!ATTLIST ProgressionOrderChange Marker CDATA #REQUIRED>
<!ELEMENT Progression (RSpoc,CSpoc,LYEpoc,REpoc,CEpoc,Ppoc)>
<!ATTLIST Progression Num CDATA #REQUIRED>
<!ELEMENT RSpoc (#PCDATA)> <!-- Resolution level index (inclusive) for progression start. Range: 0 to 33 -->
<!ELEMENT CSpoc (#PCDATA)> <!-- Component index (inclusive) for progression start. -->
<!ELEMENT LYEpoc (#PCDATA)> <!-- Layer index (exclusive) for progression end. -->
<!ELEMENT REpoc (#PCDATA)> <!-- Resolution level index (exclusive) for progression end. Range: RSpoc to 33 -->
<!ELEMENT CEpoc (#PCDATA)> <!-- Component index (exclusive) for progression end. Minimum: CSpoc -->
<!ELEMENT Ppoc (#PCDATA)> <!-- Defined Progression Order Values are: -->
<!-- 0 = LRCP; 1 = RLCP; 2 = RPCL; 3 = PCRL; 4 = CPRL -->
<!-- where L = "layer", R = "resolution level", C = "component", P = "position". -->
<!-- </Progression>, </ProgressionOrderChange -->
<!-- /MainHeader -->
<!ELEMENT TilePartHeaders (TilePartHeader+)>
<!ATTLIST TilePartHeaders Count CDATA #REQUIRED>
<!ELEMENT TilePartHeader (StartOfTilePart,CodingStyleDefault,QuantizationDefault,QuantizationComponent*,RegionOfInterest?,ProgressionOrderChange*,StartOfData)>
<!ATTLIST TilePartHeader Num CDATA #REQUIRED>
<!ATTLIST TilePartHeader ID CDATA #REQUIRED>
<!ELEMENT StartOfTilePart EMPTY>
<!ATTLIST StartOfTilePart Marker CDATA #FIXED "SOT">
<!-- CodingStyleDefault, QuantizationDefault, QuantizationComponent already defined -->
<!-- mj2_to_metadata implementation always reports component[0] as using default QCD, -->
<!-- and any other component, with tile-part-header quantization values different from [0], as QCC. -->
<!ELEMENT StartOfData EMPTY> <!-- always empty for now -->
<!ATTLIST StartOfData Marker CDATA #FIXED "SOD">
<!-- Tile-part bitstream, not shown, follows tile-part header and SOD marker. -->
<!-- /TilePartHeader, /TilePartHeaders, /JP2_Frame -->
<!-- </Track> -->
<!-- to come:
<MovieExtends mvek> // possibly not in Simple Profile
<UserDataBox udat> contains <CopyrightBox cprt>
-->
<!-- /MovieBox -->
<!-- To come:
<mdat>
<moof> // probably not in Simple Profile
<free>
<skip>
-->
<!-- </MJ2_File> -->

9
mj2/mj2_to_metadata.h Normal file
View File

@@ -0,0 +1,9 @@
/* mj2_to_metadata.h */
/* Dump MJ2, JP2 metadata (partial so far) to xml file */
/* Contributed to Open JPEG by Glenn Pearson, U.S. National Library of Medicine */
#define BOOL int
#define FALSE 0
#define TRUE 1
#include "meta_out.h"

324
mj2/mj2_to_metadata.vcproj Normal file
View File

@@ -0,0 +1,324 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="mj2_to_metadata"
ProjectGUID="{338C1241-ACB9-469F-98DF-3B9D79ADD1A0}"
SccProjectName=""
SccLocalPath="">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Release|Win32"
OutputDirectory=".\Release"
IntermediateDirectory=".\Release"
ConfigurationType="1"
UseOfMFC="0"
ATLMinimizesCRunTimeLibraryUsage="FALSE"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
InlineFunctionExpansion="1"
AdditionalIncludeDirectories="../libopenjpeg"
PreprocessorDefinitions="WIN32,NDEBUG,_CONSOLE,DONT_HAVE_GETOPT,NO_PACKETS_DECODING"
StringPooling="TRUE"
RuntimeLibrary="4"
EnableFunctionLevelLinking="TRUE"
UsePrecompiledHeader="2"
PrecompiledHeaderFile=".\Release/mj2_to_metadata.pch"
AssemblerListingLocation=".\Release/"
ObjectFile=".\Release/"
ProgramDataBaseFileName=".\Release/"
WarningLevel="3"
SuppressStartupBanner="TRUE"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalOptions="/MACHINE:I386"
AdditionalDependencies="odbc32.lib odbccp32.lib"
OutputFile=".\Release/mj2_to_metadata.exe"
LinkIncremental="1"
SuppressStartupBanner="TRUE"
ProgramDatabaseFile=".\Release/mj2_to_metadata.pdb"
SubSystem="1"/>
<Tool
Name="VCMIDLTool"
TypeLibraryName=".\Release/mj2_to_metadata.tlb"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"
PreprocessorDefinitions="NDEBUG"
Culture="2057"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Debug|Win32"
OutputDirectory=".\mj2_to_metadata___Win32_Debug0"
IntermediateDirectory=".\mj2_to_metadata___Win32_Debug0"
ConfigurationType="1"
UseOfMFC="0"
ATLMinimizesCRunTimeLibraryUsage="FALSE"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="../libopenjpeg"
PreprocessorDefinitions="WIN32,_DEBUG,_CONSOLE,DONT_HAVE_GETOPT,NO_PACKETS_DECODING"
BasicRuntimeChecks="3"
RuntimeLibrary="5"
UsePrecompiledHeader="2"
PrecompiledHeaderFile=".\mj2_to_metadata___Win32_Debug0/mj2_to_metadata.pch"
AssemblerListingLocation=".\mj2_to_metadata___Win32_Debug0/"
ObjectFile=".\mj2_to_metadata___Win32_Debug0/"
ProgramDataBaseFileName=".\mj2_to_metadata___Win32_Debug0/"
WarningLevel="3"
SuppressStartupBanner="TRUE"
DebugInformationFormat="4"/>
<Tool
Name="VCCustomBuildTool"/>
<Tool
Name="VCLinkerTool"
AdditionalOptions="/MACHINE:I386"
AdditionalDependencies="odbc32.lib odbccp32.lib"
OutputFile=".\mj2_to_metadata___Win32_Debug0/mj2_to_metadata.exe"
LinkIncremental="2"
SuppressStartupBanner="TRUE"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile=".\mj2_to_metadata___Win32_Debug0/mj2_to_metadata.pdb"
SubSystem="1"/>
<Tool
Name="VCMIDLTool"
TypeLibraryName=".\mj2_to_metadata___Win32_Debug0/mj2_to_metadata.tlb"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"
PreprocessorDefinitions="_DEBUG"
Culture="2057"
AdditionalIncludeDirectories=""
ResourceOutputFileName=""/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="MJ2"
Filter="">
<Filter
Name="MJ2 Header Files"
Filter="">
<File
RelativePath=".\compat\getopt.h">
</File>
<File
RelativePath="meta_out.h">
</File>
<File
RelativePath=".\mj2.h">
</File>
<File
RelativePath=".\mj2_convert.h">
</File>
<File
RelativePath="mj2_to_metadata.h">
</File>
</Filter>
<Filter
Name="MJ2 Source Files"
Filter="">
<File
RelativePath=".\compat\getopt.c">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../libopenjpeg;$(NoInherit)"
CompileAs="0"/>
</FileConfiguration>
</File>
<File
RelativePath="meta_out.c">
</File>
<File
RelativePath=".\mj2.c">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../libopenjpeg;$(NoInherit)"
CompileAs="0"/>
</FileConfiguration>
</File>
<File
RelativePath=".\mj2_convert.c">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../libopenjpeg;$(NoInherit)"
CompileAs="0"/>
</FileConfiguration>
</File>
<File
RelativePath=".\mj2_to_metadata.c">
<FileConfiguration
Name="Debug|Win32">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="../libopenjpeg;$(NoInherit)"
CompileAs="0"/>
</FileConfiguration>
</File>
</Filter>
</Filter>
<Filter
Name="OpenJPEG"
Filter="">
<Filter
Name="OpenJPEG Header Files"
Filter=".h">
<File
RelativePath="..\libopenjpeg\bio.h">
</File>
<File
RelativePath="..\libopenjpeg\cio.h">
</File>
<File
RelativePath="..\libopenjpeg\dwt.h">
</File>
<File
RelativePath="..\libopenjpeg\fix.h">
</File>
<File
RelativePath="..\libopenjpeg\int.h">
</File>
<File
RelativePath="..\libopenjpeg\j2k.h">
</File>
<File
RelativePath="..\libopenjpeg\jp2.h">
</File>
<File
RelativePath="..\libopenjpeg\jpt.h">
</File>
<File
RelativePath="..\libopenjpeg\mct.h">
</File>
<File
RelativePath="..\libopenjpeg\mqc.h">
</File>
<File
RelativePath="..\libopenjpeg\openjpeg.h">
</File>
<File
RelativePath="..\libopenjpeg\pi.h">
</File>
<File
RelativePath="..\libopenjpeg\raw.h">
</File>
<File
RelativePath="..\libopenjpeg\t1.h">
</File>
<File
RelativePath="..\libopenjpeg\t2.h">
</File>
<File
RelativePath="..\libopenjpeg\tcd.h">
</File>
<File
RelativePath="..\libopenjpeg\tgt.h">
</File>
</Filter>
<Filter
Name="OpenJPEG Source Files"
Filter=".c">
<File
RelativePath="..\libopenjpeg\bio.c">
</File>
<File
RelativePath="..\libopenjpeg\cio.c">
</File>
<File
RelativePath="..\libopenjpeg\dwt.c">
</File>
<File
RelativePath="..\libopenjpeg\fix.c">
</File>
<File
RelativePath="..\libopenjpeg\int.c">
</File>
<File
RelativePath="..\libopenjpeg\j2k.c">
</File>
<File
RelativePath="..\libopenjpeg\jp2.c">
</File>
<File
RelativePath="..\libopenjpeg\jpt.c">
</File>
<File
RelativePath="..\libopenjpeg\mct.c">
</File>
<File
RelativePath="..\libopenjpeg\mqc.c">
</File>
<File
RelativePath="..\libopenjpeg\pi.c">
</File>
<File
RelativePath="..\libopenjpeg\raw.c">
</File>
<File
RelativePath="..\libopenjpeg\t1.c">
</File>
<File
RelativePath="..\libopenjpeg\t2.c">
</File>
<File
RelativePath="..\libopenjpeg\tcd.c">
</File>
<File
RelativePath="..\libopenjpeg\tgt.c">
</File>
</Filter>
</Filter>
<File
RelativePath="resource.h">
</File>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

Binary file not shown.

287
mj2/wrap_j2k_in_mj2.c Normal file
View File

@@ -0,0 +1,287 @@
#include <stdio.h>
#include <malloc.h>
#include <setjmp.h>
#include <string.h>
#include "mj2.h"
#include <cio.h>
#include <j2k.h>
#include <int.h>
#define MJ2_MJ2 0x6d6a7032
#define MJ2_MJ2S 0x6d6a3273
#define JP2_JP2C 0x6a703263
#define MJ2_MDAT 0x6d646174
//MEMORY LEAK
#ifdef _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h> // Must be included first
#include <crtdbg.h>
#endif
//MEM
jmp_buf j2k_error;
void j2k_read_siz_marker(FILE *file, j2k_image_t *j2k_img)
{
int len,i;
char buf, buf2[2];
char *siz_buffer;
fseek(file, 0, SEEK_SET);
do {
fread(&buf,1,1, file);
if (buf==(char)0xff)
fread(&buf,1,1, file);
}
while (!(buf==(char)0x51));
fread(buf2,2,1,file); /* Lsiz */
len = ((buf2[0])<<8) + buf2[1];
siz_buffer = (char*) malloc(len * sizeof(char));
fread(siz_buffer,len, 1, file);
cio_init(siz_buffer,len);
cio_read(2); /* Rsiz (capabilities) */
j2k_img->x1 = cio_read(4); /* Xsiz */
j2k_img->y1 = cio_read(4); /* Ysiz */
j2k_img->x0 = cio_read(4); /* X0siz */
j2k_img->y0 = cio_read(4); /* Y0siz */
cio_skip(16); /* XTsiz, YTsiz, XT0siz, YT0siz */
j2k_img->numcomps = cio_read(2); /* Csiz */
j2k_img->comps =
(j2k_comp_t *) malloc(j2k_img->numcomps * sizeof(j2k_comp_t));
for (i = 0; i < j2k_img->numcomps; i++) {
int tmp;
tmp = cio_read(1); /* Ssiz_i */
j2k_img->comps[i].prec = (tmp & 0x7f) + 1;
j2k_img->comps[i].sgnd = tmp >> 7;
j2k_img->comps[i].dx = cio_read(1); /* XRsiz_i */
j2k_img->comps[i].dy = cio_read(1); /* YRsiz_i */
j2k_img->comps[i].resno_decoded = 0; /* number of resolution decoded */
j2k_img->comps[i].factor = 0; /* reducing factor by component */
}
free(siz_buffer);
fseek(file, 0, SEEK_SET);
}
void setparams(mj2_movie_t *movie, j2k_image_t *img) {
int i, depth_0, depth, sign;
movie->tk[0].sample_rate = 25;
movie->tk[0].w = int_ceildiv(img->x1 - img->x0, img->comps[0].dx);
movie->tk[0].h = int_ceildiv(img->y1 - img->y0, img->comps[0].dy);
mj2_init_stdmovie(movie);
movie->tk[0].depth = img->comps[0].prec;
if (img->numcomps==3) {
if ((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[1].dx == 1))
movie->tk[0].CbCr_subsampling_dx = 1;
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[1].dx == 2))
movie->tk[0].CbCr_subsampling_dx = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
if ((img->comps[0].dy == 1) && (img->comps[1].dy == 1) && (img->comps[1].dy == 1))
movie->tk[0].CbCr_subsampling_dy = 1;
else if ((img->comps[0].dy == 1) && (img->comps[1].dy == 2) && (img->comps[1].dy == 2))
movie->tk[0].CbCr_subsampling_dy = 2;
else
fprintf(stderr,"Image component sizes are incoherent\n");
}
movie->tk[0].sample_rate = 25;
movie->tk[0].jp2_struct.numcomps = img->numcomps; // NC
jp2_init_stdjp2(&movie->tk[0].jp2_struct);
movie->tk[0].jp2_struct.w = int_ceildiv(img->x1 - img->x0, img->comps[0].dx);
movie->tk[0].jp2_struct.h = int_ceildiv(img->y1 - img->y0, img->comps[0].dy);
depth_0 = img->comps[0].prec - 1;
sign = img->comps[0].sgnd;
movie->tk[0].jp2_struct.bpc = depth_0 + (sign << 7);
for (i = 1; i < img->numcomps; i++) {
depth = img->comps[i].prec - 1;
sign = img->comps[i].sgnd;
if (depth_0 != depth)
movie->tk[0].jp2_struct.bpc = 255;
}
for (i = 0; i < img->numcomps; i++)
movie->tk[0].jp2_struct.comps[i].bpcc =
img->comps[i].prec - 1 + (img->comps[i].sgnd << 7);
if ((img->numcomps == 1 || img->numcomps == 3)
&& (movie->tk[0].jp2_struct.bpc != 255))
movie->tk[0].jp2_struct.meth = 1;
else
movie->tk[0].jp2_struct.meth = 2;
if (img->numcomps == 1)
movie->tk[0].jp2_struct.enumcs = 17; // Grayscale
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 1) && (img->comps[1].dx == 1) &&
(img->comps[0].dy == 1) && (img->comps[1].dy == 1) && (img->comps[1].dy == 1))
movie->tk[0].jp2_struct.enumcs = 16; // RGB
else if ((img->comps[0].dx == 1) && (img->comps[1].dx == 2) && (img->comps[1].dx == 2) &&
(img->comps[0].dy == 1) && (img->comps[1].dy == 2) && (img->comps[1].dy == 2))
movie->tk[0].jp2_struct.enumcs = 18; // YUV
else
movie->tk[0].jp2_struct.enumcs = 0; // Unkown profile */
}
int main(int argc, char *argv[]) {
unsigned int snum;
mj2_movie_t movie;
mj2_sample_t *sample;
unsigned char* frame_codestream;
FILE *mj2file, *j2kfile;
char j2kfilename[50];
char *buf;
int offset, mdat_initpos;
j2k_image_t img;
int i;
if (argc != 3) {
printf("Bad syntax: Usage: MJ2_Wrapper source_location mj2_filename\n");
printf("Example: MJ2_Wrapper input/input output.mj2\n");
return 1;
}
mj2file = fopen(argv[2], "wb");
if (!mj2file) {
fprintf(stderr, "failed to open %s for writing\n", argv[2]);
return 1;
}
// Initialing the movie (parameters used in the JP and FTYP boxes
movie.num_htk = 0; // No hint tracks
movie.num_stk = 0; // No sound tracks
movie.num_vtk = 1; // One video track
movie.tk = (mj2_tk_t*) malloc (sizeof(mj2_tk_t)); //Memory allocation for the video track
movie.tk[0].sample = (mj2_sample_t*) malloc (sizeof(mj2_sample_t));
movie.tk[0].chunk = (mj2_chunk_t *) malloc(sizeof(mj2_chunk_t));
movie.tk[0].track_type = 0; // Video track
movie.tk[0].track_ID = 1; // Track ID = 1
movie.brand = MJ2_MJ2; // One brand: MJ2
movie.num_cl = 2; // Two compatible brands: MJ2 and MJ2S
movie.cl = (unsigned int *) malloc(movie.num_cl * sizeof(unsigned int));
movie.cl[0] = MJ2_MJ2;
movie.cl[1] = MJ2_MJ2S;
movie.minversion = 0; // Minimum version: 0
// Writing JP, FTYP and MDAT boxes
buf = (char*) malloc (300 * sizeof(char)); // Assuming that the JP and FTYP
// boxes won't be longer than 300 bytes
cio_init(buf , 300);
mj2_write_jp();
mj2_write_ftyp(&movie);
mdat_initpos = cio_tell();
cio_skip(4);
cio_write(MJ2_MDAT, 4);
fwrite(buf,cio_tell(),1,mj2file);
free(buf);
// Insert each j2k codestream in a JP2C box
snum=0;
offset = 0;
while(1)
{
sample = &movie.tk[0].sample[snum];
sprintf(j2kfilename,"%s_%d.j2k",argv[1],snum);
j2kfile = fopen(j2kfilename, "rb");
if (!j2kfile) {
if (snum==0) { // Could not open a single codestream
fprintf(stderr, "failed to open %s for reading\n",j2kfilename);
return 1;
}
else { // Tried to open a inexistant codestream
fprintf(stdout,"%d frames created\n",snum);
break;
}
}
// Calculating offset for samples and chunks
offset += cio_tell();
sample->offset = offset;
movie.tk[0].chunk[snum].offset = offset; // There will be one sample per chunk
// Calculating sample size
fseek(j2kfile,0,SEEK_END);
sample->sample_size = ftell(j2kfile) + 8; // Sample size is codestream + JP2C box header
fseek(j2kfile,0,SEEK_SET);
// Reading siz marker of j2k image for the first codestream
if (snum==0)
j2k_read_siz_marker(j2kfile, &img);
// Writing JP2C box header
frame_codestream = (unsigned char*) malloc (sample->sample_size+8);
cio_init(frame_codestream, sample->sample_size);
cio_write(sample->sample_size, 4); // Sample size
cio_write(JP2_JP2C, 4); // JP2C
// Writing codestream from J2K file to MJ2 file
fread(frame_codestream+8,sample->sample_size-8,1,j2kfile);
fwrite(frame_codestream,sample->sample_size,1,mj2file);
cio_skip(sample->sample_size-8);
// Ending loop
fclose(j2kfile);
snum++;
movie.tk[0].sample = realloc(movie.tk[0].sample, (snum+1) * sizeof(mj2_sample_t));
movie.tk[0].chunk = realloc(movie.tk[0].chunk, (snum+1) * sizeof(mj2_chunk_t));
free(frame_codestream);
}
// Writing the MDAT box length in header
offset += cio_tell();
buf = (char*) malloc (4 * sizeof(char));
cio_init(buf,4);
cio_write(offset-mdat_initpos,4); // Write MDAT length in MDAT box header
fseek(mj2file,(long)mdat_initpos,SEEK_SET);
fwrite(buf,4,1,mj2file);
fseek(mj2file,0,SEEK_END);
free(buf);
// Setting movie parameters
movie.tk[0].num_samples=snum;
movie.tk[0].num_chunks=snum;
setparams(&movie, &img);
// Writing MOOV box
i=1;
buf = (char*) malloc (10000 * sizeof(char));
cio_init(buf , i*10000);
if (setjmp(j2k_error)) {
i++;
buf = realloc(buf,i*10000* sizeof(char));
cio_init(buf , i*10000);
}
mj2_write_moov(&movie);
fwrite(buf,cio_tell(),1,mj2file);
// Ending program
fclose(mj2file);
free(img.comps);
free(buf);
mj2_memory_free(&movie);
//MEMORY LEAK
#ifdef _DEBUG
_CrtDumpMemoryLeaks();
#endif
//MEM
return 0;
}