corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | #region Using Statements |
2 | using System; |
||
3 | using System.Collections.Generic; |
||
4 | using System.Drawing; |
||
5 | using System.IO; |
||
6 | using CSJ2K.j2k.quantization.quantizer; |
||
7 | using CSJ2K.j2k.image.forwcomptransf; |
||
8 | using CSJ2K.j2k.codestream.writer; |
||
9 | using CSJ2K.j2k.fileformat.writer; |
||
10 | using CSJ2K.j2k.wavelet.analysis; |
||
11 | using CSJ2K.j2k.entropy.encoder; |
||
12 | using CSJ2K.j2k.entropy; |
||
13 | using CSJ2K.j2k.quantization; |
||
14 | using CSJ2K.j2k.image.input; |
||
15 | using CSJ2K.j2k.roi.encoder; |
||
16 | using CSJ2K.j2k.roi; |
||
17 | using CSJ2K.j2k.codestream; |
||
18 | using CSJ2K.j2k.image; |
||
19 | using CSJ2K.j2k.util; |
||
20 | using CSJ2K.j2k.encoder; |
||
21 | using CSJ2K.j2k; |
||
22 | #endregion |
||
23 | |||
24 | namespace CSJ2K |
||
25 | { |
||
26 | public static class J2KEncoder |
||
27 | { |
||
28 | #region Default Encoding Parameters |
||
29 | |||
30 | private readonly static string[][] pinfo = { |
||
31 | new string[] { "debug", null, |
||
32 | "Print debugging messages when an error is encountered.","off"}, |
||
33 | new string[] { "disable_jp2_extension", "[on|off]", |
||
34 | "JJ2000 automatically adds .jp2 extension when using 'file_format'"+ |
||
35 | "option. This option disables it when on.", "off"}, |
||
36 | new string[] { "file_format", "[on|off]", |
||
37 | "Puts the JPEG 2000 codestream in a JP2 file format wrapper.","off"}, |
||
38 | new string[] { "pph_tile", "[on|off]", |
||
39 | "Packs the packet headers in the tile headers.","off"}, |
||
40 | new string[] { "pph_main", "[on|off]", |
||
41 | "Packs the packet headers in the main header.","off"}, |
||
42 | new string[] { "pfile", "<filename of arguments file>", |
||
43 | "Loads the arguments from the specified file. Arguments that are "+ |
||
44 | "specified on the command line override the ones from the file.\n"+ |
||
45 | "The arguments file is a simple text file with one argument per "+ |
||
46 | "line of the following form:\n" + |
||
47 | " <argument name>=<argument value>\n"+ |
||
48 | "If the argument is of boolean type (i.e. its presence turns a "+ |
||
49 | "feature on), then the 'on' value turns it on, while the 'off' "+ |
||
50 | "value turns it off. The argument name does not include the '-' "+ |
||
51 | "or '+' character. Long lines can be broken into several lines "+ |
||
52 | "by terminating them with '\'. Lines starting with '#' are "+ |
||
53 | "considered as comments. This option is not recursive: any 'pfile' "+ |
||
54 | "argument appearing in the file is ignored.",null}, |
||
55 | new string[] { "tile_parts", "<packets per tile-part>", |
||
56 | "This option specifies the maximum number of packets to have in "+ |
||
57 | "one tile-part. 0 means include all packets in first tile-part "+ |
||
58 | "of each tile","0"}, |
||
59 | new string[] { "tiles", "<nominal tile width> <nominal tile height>", |
||
60 | "This option specifies the maximum tile dimensions to use. "+ |
||
61 | "If both dimensions are 0 then no tiling is used.","0 0"}, |
||
62 | new string[] { "ref", "<x> <y>", |
||
63 | "Sets the origin of the image in the canvas system. It sets the "+ |
||
64 | "coordinate of the top-left corner of the image reference grid, "+ |
||
65 | "with respect to the canvas origin","0 0"}, |
||
66 | new string[] { "tref", "<x> <y>", |
||
67 | "Sets the origin of the tile partitioning on the reference grid, "+ |
||
68 | "with respect to the canvas origin. The value of 'x' ('y') "+ |
||
69 | "specified can not be larger than the 'x' one specified in the ref "+ |
||
70 | "option.","0 0"}, |
||
71 | new string[] { "rate", "<output bitrate in bpp>", |
||
72 | "This is the output bitrate of the codestream in bits per pixel."+ |
||
73 | " When equal to -1, no image information (beside quantization "+ |
||
74 | "effects) is discarded during compression.\n"+ |
||
75 | "Note: In the case where '-file_format' option is used, the "+ |
||
76 | "resulting file may have a larger bitrate.","-1"}, |
||
77 | new string[] { "lossless", "[on|off]", |
||
78 | "Specifies a lossless compression for the encoder. This options"+ |
||
79 | " is equivalent to use reversible quantization ('-Qtype "+ |
||
80 | "reversible')"+ |
||
81 | " and 5x3 wavelet filters pair ('-Ffilters w5x3'). Note that "+ |
||
82 | "this option cannot be used with '-rate'. When this option is "+ |
||
83 | "off, the quantization type and the filters pair is defined by "+ |
||
84 | "'-Qtype' and '-Ffilters' respectively.","off"}, |
||
85 | new string[] { "i", "<image file> [,<image file> [,<image file> ... ]]", |
||
86 | "Mandatory argument. This option specifies the name of the input "+ |
||
87 | "image files. If several image files are provided, they have to be"+ |
||
88 | " separated by commas in the command line. Supported formats are "+ |
||
89 | "PGM (raw), PPM (raw) and PGX, "+ |
||
90 | "which is a simple extension of the PGM file format for single "+ |
||
91 | "component data supporting arbitrary bitdepths. If the extension "+ |
||
92 | "is '.pgm', PGM-raw file format is assumed, if the extension is "+ |
||
93 | "'.ppm', PPM-raw file format is assumed, otherwise PGX file "+ |
||
94 | "format is assumed. PGM and PPM files are assumed to be 8 bits "+ |
||
95 | "deep. A multi-component image can be specified by either "+ |
||
96 | "specifying several PPM and/or PGX files, or by specifying one "+ |
||
97 | "PPM file.",null}, |
||
98 | new string[] { "o", "<file name>", |
||
99 | "Mandatory argument. This option specifies the name of the output "+ |
||
100 | "file to which the codestream will be written.",null}, |
||
101 | new string[] { "verbose", null, |
||
102 | "Prints information about the obtained bit stream.","on"}, |
||
103 | new string[] { "v", "[on|off]", |
||
104 | "Prints version and copyright information.","off"}, |
||
105 | new string[] { "u", "[on|off]", |
||
106 | "Prints usage information. "+ |
||
107 | "If specified all other arguments (except 'v') are ignored","off"}, |
||
108 | }; |
||
109 | |||
110 | #endregion Default Encoding Parameters |
||
111 | |||
112 | private readonly static ParameterList pl; |
||
113 | |||
114 | static J2KEncoder() |
||
115 | { |
||
116 | pl = new ParameterList(); |
||
117 | string[][] parameters = GetAllParameters(); |
||
118 | for (int i = 0; i < parameters.Length; i++) |
||
119 | { |
||
120 | string[] param = parameters[i]; |
||
121 | pl.Set(param[0], param[3]); |
||
122 | } |
||
123 | |||
124 | // Custom parameters |
||
125 | pl.Set("Aptype", "layer"); |
||
126 | pl.Set("Qguard_bits", "1"); |
||
127 | pl.Set("Alayers", "sl"); |
||
128 | //pl.Set("lossless", "on"); |
||
129 | } |
||
130 | |||
131 | public static byte[] EncodeJPEG(Image jpgImage) |
||
132 | { |
||
133 | Tiler imgtiler; |
||
134 | ForwCompTransf fctransf; |
||
135 | ImgDataConverter converter; |
||
136 | EncoderSpecs encSpec; |
||
137 | ForwardWT dwt; |
||
138 | Quantizer quant; |
||
139 | ROIScaler rois; |
||
140 | EntropyCoder ecoder; |
||
141 | PostCompRateAllocator ralloc; |
||
142 | HeaderEncoder headenc; |
||
143 | CodestreamWriter bwriter; |
||
144 | |||
145 | float rate = Single.MaxValue; |
||
146 | |||
147 | ImgReaderGDI imgsrc = new ImgReaderGDI(jpgImage); |
||
148 | |||
149 | imgtiler = new Tiler(imgsrc, 0, 0, 0, 0, jpgImage.Width, jpgImage.Height); |
||
150 | int ntiles = imgtiler.getNumTiles(); |
||
151 | |||
152 | encSpec = new EncoderSpecs(ntiles, 3, imgsrc, pl); |
||
153 | |||
154 | fctransf = new ForwCompTransf(imgtiler, encSpec); |
||
155 | converter = new ImgDataConverter(fctransf); |
||
156 | dwt = ForwardWT.createInstance(converter, pl, encSpec); |
||
157 | quant = Quantizer.createInstance(dwt, encSpec); |
||
158 | rois = ROIScaler.createInstance(quant, pl, encSpec); |
||
159 | ecoder = EntropyCoder.createInstance(rois, pl, encSpec.cblks, |
||
160 | encSpec.pss, encSpec.bms, |
||
161 | encSpec.mqrs, encSpec.rts, |
||
162 | encSpec.css, encSpec.sss, |
||
163 | encSpec.lcs, encSpec.tts); |
||
164 | |||
165 | using (MemoryStream stream = new MemoryStream()) |
||
166 | { |
||
167 | bwriter = new FileCodestreamWriter(stream, Int32.MaxValue); |
||
168 | ralloc = PostCompRateAllocator.createInstance(ecoder, pl, rate, bwriter, encSpec); |
||
169 | |||
170 | headenc = new HeaderEncoder(imgsrc, new bool[3], dwt, imgtiler, encSpec, rois, ralloc, pl); |
||
171 | ralloc.HeaderEncoder = headenc; |
||
172 | headenc.encodeMainHeader(); |
||
173 | ralloc.initialize(); |
||
174 | headenc.reset(); |
||
175 | headenc.encodeMainHeader(); |
||
176 | bwriter.commitBitstreamHeader(headenc); |
||
177 | |||
178 | ralloc.runAndWrite(); |
||
179 | bwriter.close(); |
||
180 | |||
181 | return stream.ToArray(); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | private static string[][] GetAllParameters() |
||
186 | { |
||
187 | List<string[]> parameters = new List<string[]>(); |
||
188 | |||
189 | string[][] str = pinfo; |
||
190 | for (int i = 0; i < str.Length; i++) |
||
191 | parameters.Add(str[i]); |
||
192 | |||
193 | str = ForwCompTransf.ParameterInfo; |
||
194 | for (int i = 0; i < str.Length; i++) |
||
195 | parameters.Add(str[i]); |
||
196 | |||
197 | str = AnWTFilter.ParameterInfo; |
||
198 | for (int i = 0; i < str.Length; i++) |
||
199 | parameters.Add(str[i]); |
||
200 | |||
201 | str = ForwardWT.ParameterInfo; |
||
202 | for (int i = 0; i < str.Length; i++) |
||
203 | parameters.Add(str[i]); |
||
204 | |||
205 | str = Quantizer.ParameterInfo; |
||
206 | for (int i = 0; i < str.Length; i++) |
||
207 | parameters.Add(str[i]); |
||
208 | |||
209 | str = ROIScaler.ParameterInfo; |
||
210 | for (int i = 0; i < str.Length; i++) |
||
211 | parameters.Add(str[i]); |
||
212 | |||
213 | str = EntropyCoder.ParameterInfo; |
||
214 | for (int i = 0; i < str.Length; i++) |
||
215 | parameters.Add(str[i]); |
||
216 | |||
217 | str = HeaderEncoder.ParameterInfo; |
||
218 | for (int i = 0; i < str.Length; i++) |
||
219 | parameters.Add(str[i]); |
||
220 | |||
221 | str = PostCompRateAllocator.ParameterInfo; |
||
222 | for (int i = 0; i < str.Length; i++) |
||
223 | parameters.Add(str[i]); |
||
224 | |||
225 | str = PktEncoder.ParameterInfo; |
||
226 | for (int i = 0; i < str.Length; i++) |
||
227 | parameters.Add(str[i]); |
||
228 | |||
229 | return parameters.ToArray(); |
||
230 | } |
||
231 | } |
||
232 | } |