~ubuntu-branches/debian/sid/wordpress/sid

« back to all changes in this revision

Viewing changes to debian/missing-sources/plupload-1.5.7/csharp/Plupload/FJCore/Decoder/HuffmanTable.cs

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2013-09-04 23:18:58 UTC
  • mfrom: (1.2.28)
  • Revision ID: package-import@ubuntu.com-20130904231858-nljmn1buzswh63jk
Tags: 3.6+dfsg-1
* New upstream release.
* Improve wp-settings to verify that $_SERVER['HTTP_X_FORWARDED_PROTO']
  exists before accessing it (avoids a PHP notice).
  Thanks to Paul Dreik <slask@pauldreik.se> for the report and the patch.
* Document in README.Debian the need to login to /wp-admin/ to complete
  an upgrade.
* Drop useless debian/README.source
* Drop 008CVE2008-2392.patch since upstream now disables unfiltered
  uploads by default. See http://core.trac.wordpress.org/ticket/10692
* Drop 009CVE2008-6767.patch since the backto parameter is validated
  against a whitelist, and externally triggered upgrades are not a
  security problem as long as they work.
* Update debian/missing-sources with latest versions.
* Update upstream l10n.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/// Copyright (c) 2008 Jeffrey Powers for Fluxcapacity Open Source.
 
2
/// Under the MIT License, details: License.txt.
 
3
 
 
4
// Partially derives from a Java encoder, JpegEncoder.java by James R Weeks.
 
5
// Implements Baseline JPEG Encoding http://www.opennet.ru/docs/formats/jpeg.txt
 
6
 
 
7
using System;
 
8
 
 
9
using FluxJpeg.Core.IO;
 
10
using System.IO;
 
11
using System.Collections.Generic;
 
12
 
 
13
namespace FluxJpeg.Core
 
14
{
 
15
    internal class HuffmanTable
 
16
    {
 
17
        public static int HUFFMAN_MAX_TABLES = 4;
 
18
 
 
19
        private short[] huffcode = new short[256];
 
20
        private short[] huffsize = new short[256];
 
21
        private short[] valptr = new short[16];
 
22
        private short[] mincode = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1,-1};
 
23
        private short[] maxcode = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
 
24
 
 
25
        private short[] huffval;
 
26
        private short[] bits;
 
27
 
 
28
        int bufferPutBits, bufferPutBuffer;
 
29
        internal int ImageHeight;
 
30
        internal int ImageWidth;
 
31
        internal int[,] DC_matrix0;
 
32
        internal int[,] AC_matrix0;
 
33
        internal int[,] DC_matrix1;
 
34
        internal int[,] AC_matrix1;
 
35
        internal int[][,] DC_matrix;
 
36
        internal int[][,] AC_matrix;
 
37
        internal int NumOfDCTables;
 
38
        internal int NumOfACTables;
 
39
 
 
40
        public List<short[]> bitsList;
 
41
        public List<short[]> val;
 
42
 
 
43
 
 
44
        public static byte JPEG_DC_TABLE = 0;
 
45
        public static byte JPEG_AC_TABLE = 1;
 
46
 
 
47
        private short lastk = 0;
 
48
 
 
49
        internal HuffmanTable(JpegHuffmanTable table)
 
50
        {
 
51
            if (table != null)
 
52
            {
 
53
                huffval = table.Values;
 
54
                bits = table.Lengths;
 
55
 
 
56
                GenerateSizeTable();
 
57
                GenerateCodeTable();
 
58
                GenerateDecoderTables();
 
59
            }
 
60
            else
 
61
            {
 
62
                // Encode initialization
 
63
 
 
64
                bitsList = new List<short[]>();
 
65
                bitsList.Add(JpegHuffmanTable.StdDCLuminance.Lengths);
 
66
                bitsList.Add(JpegHuffmanTable.StdACLuminance.Lengths);
 
67
                bitsList.Add(JpegHuffmanTable.StdDCChrominance.Lengths);
 
68
                bitsList.Add(JpegHuffmanTable.StdACChrominance.Lengths);
 
69
 
 
70
                val = new List<short[]>();
 
71
                val.Add(JpegHuffmanTable.StdDCLuminance.Values);
 
72
                val.Add(JpegHuffmanTable.StdACLuminance.Values);
 
73
                val.Add(JpegHuffmanTable.StdDCChrominance.Values);
 
74
                val.Add(JpegHuffmanTable.StdACChrominance.Values);
 
75
 
 
76
                initHuf();
 
77
            }
 
78
        }
 
79
 
 
80
        /// <summary>See Figure C.1</summary>
 
81
        private void GenerateSizeTable()
 
82
        {
 
83
            short index = 0;
 
84
            for (short i = 0; i < bits.Length; i++)
 
85
            {
 
86
                for (short j = 0; j < bits[i]; j++)
 
87
                {
 
88
                    huffsize[index] = (short)(i + 1);
 
89
                    index++;
 
90
                }
 
91
            }
 
92
            lastk = index;
 
93
        }
 
94
 
 
95
        /// <summary>See Figure C.2</summary>
 
96
        private void GenerateCodeTable()
 
97
        {
 
98
            short k = 0;
 
99
            short si = huffsize[0];
 
100
            short code = 0;
 
101
            for (short i = 0; i < huffsize.Length; i++)
 
102
            {
 
103
                while (huffsize[k] == si)
 
104
                {
 
105
                    huffcode[k] = code;
 
106
                    code++;
 
107
                    k++;
 
108
                }
 
109
                code <<= 1;
 
110
                si++;
 
111
            }
 
112
        }
 
113
 
 
114
        /// <summary>See figure F.15</summary>
 
115
        private void GenerateDecoderTables()
 
116
        {
 
117
            short bitcount = 0;
 
118
            for (int i = 0; i < 16; i++)
 
119
            {
 
120
                if (bits[i] != 0)
 
121
                    valptr[i] = bitcount;
 
122
                for (int j = 0; j < bits[i]; j++)
 
123
                {
 
124
                    if (huffcode[j + bitcount] < mincode[i] || mincode[i] == -1)
 
125
                        mincode[i] = huffcode[j + bitcount];
 
126
 
 
127
                    if (huffcode[j + bitcount] > maxcode[i])
 
128
                        maxcode[i] = huffcode[j + bitcount];
 
129
                }
 
130
                if (mincode[i] != -1)
 
131
                    valptr[i] = (short)(valptr[i] - mincode[i]);
 
132
                bitcount += bits[i];
 
133
            }
 
134
        }
 
135
 
 
136
        /// <summary>Figure F.12</summary>
 
137
        public static int Extend(int diff, int t)
 
138
        {
 
139
            // here we use bitshift to implement 2^ ... 
 
140
            // NOTE: Math.Pow returns 0 for negative powers, which occassionally happen here!
 
141
 
 
142
            int Vt = 1 << t - 1;
 
143
            // WAS: int Vt = (int)Math.Pow(2, (t - 1));
 
144
 
 
145
            if (diff < Vt)
 
146
            {
 
147
                Vt = (-1 << t) + 1;
 
148
                diff = diff + Vt;
 
149
            }
 
150
            return diff;
 
151
        }
 
152
 
 
153
        /// <summary>Figure F.16 - Reads the huffman code bit-by-bit.</summary>
 
154
        /*public int Decode(JPEGBinaryReader JPEGStream)
 
155
        {
 
156
            int i = 0;
 
157
            short code = (short)JPEGStream.ReadBits(1);
 
158
            while (code > maxcode[i])
 
159
            {
 
160
                i++;
 
161
                code <<= 1;
 
162
                code |= (short)JPEGStream.ReadBits(1);
 
163
            }
 
164
            int val = huffval[code + (valptr[i])];
 
165
            if (val < 0)
 
166
                val = 256 + val;
 
167
            return val;
 
168
        }*/
 
169
 
 
170
                /// <summary>
 
171
        /// HuffmanBlockEncoder run length encodes and Huffman encodes the quantized data.
 
172
        /// </summary>
 
173
        internal void HuffmanBlockEncoder(Stream outStream, int[] zigzag, int prec, int DCcode, int ACcode)
 
174
        {
 
175
            int temp, temp2, nbits, k, r, i;
 
176
 
 
177
            NumOfDCTables = 2;
 
178
            NumOfACTables = 2;
 
179
 
 
180
            // The DC portion
 
181
 
 
182
            temp = temp2 = zigzag[0] - prec;
 
183
            if (temp < 0)
 
184
            {
 
185
                temp = -temp;
 
186
                temp2--;
 
187
            }
 
188
            nbits = 0;
 
189
            while (temp != 0)
 
190
            {
 
191
                nbits++;
 
192
                temp >>= 1;
 
193
            }
 
194
            //            if (nbits > 11) nbits = 11;
 
195
            bufferIt(outStream, 
 
196
                DC_matrix[DCcode][nbits, 0],
 
197
                DC_matrix[DCcode][nbits, 1]);
 
198
 
 
199
            // The arguments in bufferIt are code and size.
 
200
            if (nbits != 0)
 
201
            {
 
202
                bufferIt(outStream, temp2, nbits);
 
203
            }
 
204
 
 
205
            // The AC portion
 
206
 
 
207
            r = 0;
 
208
 
 
209
            for (k = 1; k < 64; k++)
 
210
            {
 
211
                if ((temp = zigzag[ ZigZag.ZigZagMap[k] ]) == 0)
 
212
                {
 
213
                    r++;
 
214
                }
 
215
                else
 
216
                {
 
217
                    while (r > 15)
 
218
                    {
 
219
                        bufferIt(outStream, 
 
220
                            AC_matrix[ACcode][0xF0, 0], 
 
221
                            AC_matrix[ACcode][0xF0, 1]);
 
222
 
 
223
                        r -= 16;
 
224
                    }
 
225
                    temp2 = temp;
 
226
                    if (temp < 0)
 
227
                    {
 
228
                        temp = -temp;
 
229
                        temp2--;
 
230
                    }
 
231
                    nbits = 1;
 
232
                    while ((temp >>= 1) != 0)
 
233
                    {
 
234
                        nbits++;
 
235
                    }
 
236
                    i = (r << 4) + nbits;
 
237
                    bufferIt(outStream, 
 
238
                        AC_matrix[ACcode][i, 0], 
 
239
                        AC_matrix[ACcode][i, 1]);
 
240
                    bufferIt(outStream, temp2, nbits);
 
241
 
 
242
                    r = 0;
 
243
                }
 
244
            }
 
245
 
 
246
            if (r > 0)
 
247
            {
 
248
                bufferIt(outStream, 
 
249
                    AC_matrix[ACcode][0, 0], 
 
250
                    AC_matrix[ACcode][0, 1]);
 
251
            }
 
252
        }
 
253
 
 
254
        /// <summary>
 
255
        /// Uses an integer long (32 bits) buffer to store the Huffman encoded bits
 
256
        /// and sends them to outStream by the byte.
 
257
        /// </summary>
 
258
        void bufferIt(Stream outStream, int code, int size)
 
259
        {
 
260
            int PutBuffer = code;
 
261
            int PutBits = bufferPutBits;
 
262
 
 
263
            PutBuffer &= (1 << size) - 1;
 
264
            PutBits += size;
 
265
            PutBuffer <<= 24 - PutBits;
 
266
            PutBuffer |= bufferPutBuffer;
 
267
 
 
268
            while (PutBits >= 8)
 
269
            {
 
270
                int c = ((PutBuffer >> 16) & 0xFF);
 
271
                outStream.WriteByte((byte)c);
 
272
                
 
273
                // FF must be escaped
 
274
                if (c == 0xFF) outStream.WriteByte(0);
 
275
                
 
276
                PutBuffer <<= 8;
 
277
                PutBits -= 8;
 
278
            }
 
279
            bufferPutBuffer = PutBuffer;
 
280
            bufferPutBits = PutBits;
 
281
 
 
282
        }
 
283
 
 
284
        public void FlushBuffer(Stream outStream)
 
285
        {
 
286
            int PutBuffer = bufferPutBuffer;
 
287
            int PutBits = bufferPutBits;
 
288
            while (PutBits >= 8)
 
289
            {
 
290
                int c = ((PutBuffer >> 16) & 0xFF);
 
291
                outStream.WriteByte((byte)c);
 
292
 
 
293
                // FF must be escaped
 
294
                if (c == 0xFF) outStream.WriteByte(0);
 
295
 
 
296
                PutBuffer <<= 8;
 
297
                PutBits -= 8;
 
298
            }
 
299
            if (PutBits > 0)
 
300
            {
 
301
                int c = ((PutBuffer >> 16) & 0xFF);
 
302
                outStream.WriteByte((byte)c);
 
303
            }
 
304
        }
 
305
 
 
306
 
 
307
        /// <summary>
 
308
        /// Initialisation of the Huffman codes for Luminance and Chrominance.
 
309
        /// This code results in the same tables created in the IJG Jpeg-6a
 
310
        /// library.
 
311
        /// </summary>
 
312
        public void initHuf()
 
313
        {
 
314
            DC_matrix0 = new int[12, 2];
 
315
            DC_matrix1 = new int[12, 2];
 
316
            AC_matrix0 = new int[255, 2];
 
317
            AC_matrix1 = new int[255, 2];
 
318
            DC_matrix = new int[2][,];
 
319
            AC_matrix = new int[2][,];
 
320
            int p, l, i, lastp, si, code;
 
321
            int[] huffsize = new int[257];
 
322
            int[] huffcode = new int[257];
 
323
 
 
324
            short[] bitsDCchrominance = JpegHuffmanTable.StdDCChrominance.Lengths;
 
325
            short[] bitsACchrominance = JpegHuffmanTable.StdACChrominance.Lengths;
 
326
            short[] bitsDCluminance = JpegHuffmanTable.StdDCLuminance.Lengths;
 
327
            short[] bitsACluminance = JpegHuffmanTable.StdACLuminance.Lengths;
 
328
 
 
329
 
 
330
            short[] valDCchrominance = JpegHuffmanTable.StdDCChrominance.Values;
 
331
            short[] valACchrominance = JpegHuffmanTable.StdACChrominance.Values;
 
332
            short[] valDCluminance = JpegHuffmanTable.StdDCLuminance.Values;
 
333
            short[] valACluminance = JpegHuffmanTable.StdACLuminance.Values;
 
334
 
 
335
 
 
336
            /*
 
337
            * init of the DC values for the chrominance
 
338
            * [,0] is the code   [,1] is the number of bit
 
339
            */
 
340
 
 
341
            p = 0;
 
342
            for (l = 0; l < 16; l++)
 
343
            {
 
344
                for (i = 1; i <= bitsDCchrominance[l]; i++)
 
345
                {
 
346
                    huffsize[p++] = l+1;
 
347
                }
 
348
            }
 
349
 
 
350
            huffsize[p] = 0;
 
351
            lastp = p;
 
352
 
 
353
            code = 0;
 
354
            si = huffsize[0];
 
355
            p = 0;
 
356
            while (huffsize[p] != 0)
 
357
            {
 
358
                while (huffsize[p] == si)
 
359
                {
 
360
                    huffcode[p++] = code;
 
361
                    code++;
 
362
                }
 
363
                code <<= 1;
 
364
                si++;
 
365
            }
 
366
 
 
367
            for (p = 0; p < lastp; p++)
 
368
            {
 
369
                DC_matrix1[valDCchrominance[p], 0] = huffcode[p];
 
370
                DC_matrix1[valDCchrominance[p], 1] = huffsize[p];
 
371
            }
 
372
 
 
373
            /*
 
374
            * Init of the AC hufmann code for the chrominance
 
375
            * matrix [,,0] is the code & matrix[,,1] is the number of bit needed
 
376
            */
 
377
 
 
378
            p = 0;
 
379
            for (l = 0; l < 16; l++)
 
380
            {
 
381
                for (i = 1; i <= bitsACchrominance[l]; i++)
 
382
                {
 
383
                    huffsize[p++] = l+1;
 
384
                }
 
385
            }
 
386
            huffsize[p] = 0;
 
387
            lastp = p;
 
388
 
 
389
            code = 0;
 
390
            si = huffsize[0];
 
391
            p = 0;
 
392
            while (huffsize[p] != 0)
 
393
            {
 
394
                while (huffsize[p] == si)
 
395
                {
 
396
                    huffcode[p++] = code;
 
397
                    code++;
 
398
                }
 
399
                code <<= 1;
 
400
                si++;
 
401
            }
 
402
 
 
403
            for (p = 0; p < lastp; p++)
 
404
            {
 
405
                AC_matrix1[valACchrominance[p], 0] = huffcode[p];
 
406
                AC_matrix1[valACchrominance[p], 1] = huffsize[p];
 
407
            }
 
408
 
 
409
            /*
 
410
            * init of the DC values for the luminance
 
411
            * [,0] is the code   [,1] is the number of bit
 
412
            */
 
413
            p = 0;
 
414
            for (l = 0; l < 16; l++)
 
415
            {
 
416
                for (i = 1; i <= bitsDCluminance[l]; i++)
 
417
                {
 
418
                    huffsize[p++] = l+1;
 
419
                }
 
420
            }
 
421
            huffsize[p] = 0;
 
422
            lastp = p;
 
423
 
 
424
            code = 0;
 
425
            si = huffsize[0];
 
426
            p = 0;
 
427
            while (huffsize[p] != 0)
 
428
            {
 
429
                while (huffsize[p] == si)
 
430
                {
 
431
                    huffcode[p++] = code;
 
432
                    code++;
 
433
                }
 
434
                code <<= 1;
 
435
                si++;
 
436
            }
 
437
 
 
438
            for (p = 0; p < lastp; p++)
 
439
            {
 
440
                DC_matrix0[valDCluminance[p], 0] = huffcode[p];
 
441
                DC_matrix0[valDCluminance[p], 1] = huffsize[p];
 
442
            }
 
443
 
 
444
            /*
 
445
            * Init of the AC hufmann code for luminance
 
446
            * matrix [,,0] is the code & matrix[,,1] is the number of bit
 
447
            */
 
448
 
 
449
            p = 0;
 
450
            for (l = 0; l < 16; l++)
 
451
            {
 
452
                for (i = 1; i <= bitsACluminance[l]; i++)
 
453
                {
 
454
                    huffsize[p++] = l+1;
 
455
                }
 
456
            }
 
457
            huffsize[p] = 0;
 
458
            lastp = p;
 
459
 
 
460
            code = 0;
 
461
            si = huffsize[0];
 
462
            p = 0;
 
463
            while (huffsize[p] != 0)
 
464
            {
 
465
                while (huffsize[p] == si)
 
466
                {
 
467
                    huffcode[p++] = code;
 
468
                    code++;
 
469
                }
 
470
                code <<= 1;
 
471
                si++;
 
472
            }
 
473
            for (int q = 0; q < lastp; q++)
 
474
            {
 
475
                AC_matrix0[valACluminance[q], 0] = huffcode[q];
 
476
                AC_matrix0[valACluminance[q], 1] = huffsize[q];
 
477
            }
 
478
 
 
479
            DC_matrix[0] = DC_matrix0;
 
480
            DC_matrix[1] = DC_matrix1;
 
481
            AC_matrix[0] = AC_matrix0;
 
482
            AC_matrix[1] = AC_matrix1;
 
483
        }
 
484
 
 
485
 
 
486
    }
 
487
}