• R/O
  • SSH
  • HTTPS

jyeipegyuu: Commit


Commit MetaInfo

Révision7 (tree)
l'heure2009-09-01 02:22:53
Auteurberupon

Message de Log

ちょっとだけ圧縮率向上
昔のソースからマージしたのでデグレ有り。GCCではビルド出来ないようになった。

Change Summary

Modification

--- BZip2Compressor.cpp (revision 6)
+++ BZip2Compressor.cpp (nonexistent)
@@ -1,31 +0,0 @@
1-//#include "stdafx.h"
2-
3-#include "BZip2Compressor.h"
4-
5-#include <bzlib.h>
6-#pragma comment(lib, "libbz2.lib")
7-
8-size_t BZip2Compressor::Compress(
9- const unsigned char* src,
10- size_t srcLen,
11- unsigned char* dest,
12- size_t destLen
13-)
14-{
15- size_t compressedLen = destLen;
16- BZ2_bzBuffToBuffCompress((char*)dest, (unsigned int*)&compressedLen, (char*)src, (unsigned int)srcLen, 9, 0, 30);
17- return compressedLen;
18-}
19-
20-size_t BZip2Compressor::Decompress(
21- const unsigned char* src,
22- size_t srcLen,
23- unsigned char* dest,
24- size_t destLen
25-)
26-{
27- size_t decompressedLen = destLen;
28- BZ2_bzBuffToBuffDecompress((char*)dest, (unsigned int*)&decompressedLen, (char*)src, (unsigned int)srcLen, 0, 0);
29- return decompressedLen;
30-}
31-
--- BZip2Compressor.h (revision 6)
+++ BZip2Compressor.h (nonexistent)
@@ -1,24 +0,0 @@
1-#pragma once
2-
3-#include "ICompressor.h"
4-
5-class BZip2Compressor : public ICompressor
6-{
7-public:
8- // overrides
9- size_t Compress(
10- const unsigned char* src,
11- size_t srcLen,
12- unsigned char* dest,
13- size_t destLen
14- );
15-
16- size_t Decompress(
17- const unsigned char* src,
18- size_t srcLen,
19- unsigned char* dest,
20- size_t destLen
21- );
22-
23-};
24-
--- ICompressor.h (revision 6)
+++ ICompressor.h (nonexistent)
@@ -1,22 +0,0 @@
1-#pragma once
2-
3-#include <stdlib.h>
4-
5-class ICompressor
6-{
7-public:
8- virtual size_t Compress(
9- const unsigned char* src,
10- size_t srcLen,
11- unsigned char* dest,
12- size_t destLen
13- ) = 0;
14-
15- virtual size_t Decompress(
16- const unsigned char* src,
17- size_t srcLen,
18- unsigned char* dest,
19- size_t destLen
20- ) = 0;
21-};
22-
--- stdafx.h (revision 6)
+++ stdafx.h (revision 7)
@@ -1,6 +1,6 @@
1-// stdafx.h : 標準のシステム インクルード ファイルのインクルード ファイル、または
2-// 参照回数が多く、かつあまり変更されない、プロジェクト専用のインクルード ファイル
3-// を記述します。
1+// stdafx.h : ?W???̃V?X?e?? ?C???N???[?h ?t?@?C???̃C???N???[?h ?t?@?C???A?܂???
2+// ?Q?Ɖ񐔂??????A???‚??܂?ύX????Ȃ??A?v???W?F?N?g??p?̃C???N???[?h ?t?@?C??
3+// ???L?q???܂??B
44 //
55
66 #pragma once
@@ -8,14 +8,14 @@
88 #include "targetver.h"
99
1010 #include <stdio.h>
11-//#include <tchar.h>
11+#include <tchar.h>
1212
1313
1414 template <typename T>
15-inline void OffsetPtr(T*& ptr, int offsetBytes)
15+__forceinline void OffsetPtr(T*& ptr, int offsetBytes)
1616 {
1717 ptr = (T*) ((const char*)ptr + offsetBytes);
1818 }
1919
2020
21-// TODO: プログラムに必要な追加ヘッダーをここで参照してください。
21+// TODO: ?v???O?????ɕK?v?Ȓlj??w?b?_?[???????ŎQ?Ƃ??Ă????????B
--- misc.h (revision 6)
+++ misc.h (revision 7)
@@ -1,5 +1,3 @@
1-
2-#include <string.h>
31
42 template <typename T>
53 void gather(
@@ -31,6 +29,33 @@
3129 }
3230 }
3331
32+void cutoff(
33+ size_t hBlockCount, size_t vBlockCount,
34+ int* data, int lineOffsetBytes,
35+ const int table[8][8]
36+ )
37+{
38+
39+ int* srcLine = data;
40+ for (size_t y=0; y<vBlockCount; ++y) {
41+ int* workSrcLine = srcLine;
42+ for (size_t yi=0; yi<8; ++yi) {
43+ int* pWork = workSrcLine;
44+ const int* tableLine = table[yi];
45+ for (size_t x=0; x<hBlockCount; ++x) {
46+ for (size_t xi=0; xi<8; ++xi) {
47+ int value = *pWork;
48+ int newValue = std::min(tableLine[xi], std::abs(value));
49+ *pWork = (value < 0) ? -newValue : newValue;
50+ ++pWork;
51+ }
52+ }
53+ OffsetPtr(workSrcLine, lineOffsetBytes);
54+ }
55+ OffsetPtr(srcLine, lineOffsetBytes * 8);
56+ }
57+}
58+
3459 int paethPredictor(int left, int above, int upperLeft)
3560 {
3661 int initial = left + (above - upperLeft);
@@ -53,6 +78,7 @@
5378 :
5479 nBits_(0),
5580 dest_(dest),
81+ initialDest_(dest),
5682 counter_(0)
5783 {
5884 *dest_ = 0;
@@ -70,10 +96,12 @@
7096 ++nBits_;
7197 }
7298 size_t nBits() const { return nBits_; }
99+ size_t nBytes() const { return (dest_ - initialDest_) + (counter_ ? 1 : 0); }
73100 private:
74101 size_t nBits_;
75102 unsigned char counter_;
76103 unsigned char* dest_;
104+ unsigned char* initialDest_;
77105 };
78106
79107 class BitReader
@@ -82,6 +110,7 @@
82110 BitReader(const unsigned char* src)
83111 :
84112 src_(src),
113+ initialSrc_(src),
85114 counter_(0)
86115 {
87116 }
@@ -96,9 +125,13 @@
96125 }
97126 return ret;
98127 }
128+
129+ size_t nBytes() const { return src_ - initialSrc_; }
130+
99131 private:
100132 unsigned char counter_;
101133 const unsigned char* src_;
134+ const unsigned char* initialSrc_;
102135 };
103136
104137 class RiceCoder
@@ -142,28 +175,83 @@
142175 };
143176
144177 enum RiceCoderFlag {
145- RiceCoderFlag_None = 0xFF,
178+ None = 0xF,
146179 };
147180
181+size_t repeationCompress(
182+ const unsigned char* src, size_t srcLen,
183+ unsigned char* tmp
184+ )
185+{
186+ int repeatHist[1024] = {0};
187+ int repeat = 0;
188+
189+ BitWriter bitWriter(tmp);
190+ RiceCoder riceCoder(2);
191+
192+ for (size_t i=0; i<srcLen; ++i) {
193+ unsigned char d = src[i];
194+ for (size_t j=0; j<8; ++j) {
195+ if (d & (1 << (7-j))) {
196+ ++repeat;
197+ }else {
198+ riceCoder.Encode(repeat, bitWriter);
199+ ++repeatHist[repeat];
200+ repeat = 0;
201+ }
202+ }
203+ }
204+ if (repeat) {
205+ riceCoder.Encode(repeat, bitWriter);
206+ ++repeatHist[repeat];
207+ repeat = 0;
208+ }
209+
210+ size_t len = bitWriter.nBytes();
211+ return len;
212+}
213+
214+size_t repeationDecompress(
215+ const unsigned char* src, size_t srcLen,
216+ unsigned char* dst
217+ )
218+{
219+ RiceCoder riceCoder(2);
220+ BitReader bitReader(src);
221+ BitWriter bitWriter(dst);
222+ while (bitReader.nBytes() <= srcLen) {
223+ size_t v = riceCoder.Decode(bitReader);
224+ for (size_t i=0; i<v; ++i) {
225+ bitWriter.putBit(true);
226+ }
227+ bitWriter.putBit(false);
228+ }
229+ return bitWriter.nBytes();
230+}
231+
148232 size_t compressSub(
149233 ICompressor& compressor,
150234 int* src, size_t srcLen,
151235 unsigned char* dest, size_t destLen,
152- unsigned char* tmp, size_t tmpLen
236+ unsigned char* tmp,
237+ unsigned char* tmp2
153238 )
154239 {
155240 unsigned char* initialDest = dest;
156241
157242 assert(destLen > 4);
158-
243+
159244 int mini = boost::integer_traits<int>::const_max;
160245 int maxi = boost::integer_traits<int>::const_min;
161-// int table[4096] = {0};
162-
246+
247+ int hists[1024] = {0};
163248 BitWriter signFlags(dest+4);
164249 for (size_t i=0; i<srcLen; ++i) {
165250 int val = src[i];
251+ mini = std::min(val, mini);
252+ maxi = std::max(val, maxi);
166253 if (val == 0) {
254+ ;
167255 }else if (val < 0) {
168256 signFlags.putBit(false);
169257 }else {
@@ -170,19 +258,20 @@
170258 signFlags.putBit(true);
171259 }
172260 val = std::abs(val);
173- maxi = std::max(val, maxi);
174261 src[i] = val;
175-// ++table[val];
262+
263+ ++hists[val];
176264 }
177- size_t signFlagCount = signFlags.nBits();
178- size_t signFlagBytes = signFlagCount/8 + ((signFlagCount%8) ? 1 : 0);
265+
266+ uint32_t signFlagBytes = signFlags.nBytes();
179267 *((size_t*)dest) = signFlagBytes;
268+
180269 dest += (4 + signFlagBytes);
181270 destLen -= (4 + signFlagBytes);
182271
183- mini = 0;
272+ int b = 0;
184273 size_t max = std::max(maxi, std::abs(mini));
185- int b;
274+
186275 if (max > 2048) {
187276 b = 7;
188277 }else if (max > 1024) {
@@ -201,48 +290,53 @@
201290 b = 0;
202291 }
203292
204- size_t initialCompressedLen = compressor.Compress((const unsigned char*)src, srcLen*4, tmp, tmpLen);
293+ size_t initialCompressedLen = compressor.Compress((const unsigned char*)src, srcLen*4, tmp, -1);
205294
206- BitWriter bitWriter(dest);
295+ BitWriter bitWriter(tmp2);
207296 RiceCoder riceCoder(b);
208297 for (size_t i=0; i<srcLen; ++i) {
209298 riceCoder.Encode(src[i], bitWriter);
210299 }
211- //BitReader bitReader(dest);
212- //for (size_t i=0; i<srcLen; ++i) {
213- // size_t val = riceCoder.Decode(bitReader);
214- // assert(src[i] == val);
215- //}
216- size_t bitsLen = bitWriter.nBits();
217- size_t bytesLen = bitsLen / 8;
218- if (bitsLen % 8) ++bytesLen;
300+ size_t bytesLen = bitWriter.nBytes();
301+
302+ size_t compressedLen = 0;
219303
220- memcpy(src, dest, bytesLen);
221- size_t compressedLen = compressor.Compress((const unsigned char*)src, bytesLen, dest+2+4, destLen-2-4);
304+ compressedLen = compressor.Compress(tmp2, bytesLen, dest+6, -1);
305+ unsigned char* dest2 = (compressedLen < initialCompressedLen) ? tmp : (dest+6);
306+ size_t repeationCompressedLen = repeationCompress(tmp2, bytesLen, dest2);
222307
223308 size_t len = 0;
224- if (initialCompressedLen < compressedLen && initialCompressedLen < bytesLen) {
309+ if (initialCompressedLen < compressedLen && initialCompressedLen < bytesLen && initialCompressedLen < repeationCompressedLen) {
225310 *dest++ = 1;
226- *dest++ = RiceCoderFlag_None;
311+ *dest++ = RiceCoderFlag::None;
227312 len = initialCompressedLen;
228313 memcpy(dest+4, tmp, len);
229- }else if (compressedLen < bytesLen) {
314+ }else if (compressedLen < bytesLen && compressedLen < repeationCompressedLen) {
230315 *dest++ = 1;
231316 *dest++ = b;
232317 len = compressedLen;
233- }else {
318+ }else if (bytesLen < repeationCompressedLen) {
234319 *dest++ = 0;
235320 *dest++ = b;
236321 len = bytesLen;
237- memcpy(dest+4, src, len);
322+ memcpy(dest+4, tmp2, len);
323+ }else {
324+ *dest++ = 2;
325+ *dest++ = b;
326+ len = repeationCompressedLen;
327+ memcpy(dest+4, dest2, len);
238328 }
239329 *((size_t*)dest) = len;
240330 dest += 4;
241331 dest += len;
242-
243-// printf("%d %d %d\n", max, b, len + signFlagBytes);
244-
245- return dest - initialDest;
332+
333+ size_t destDiff = dest - initialDest;
334+ printf(
335+ "[%d %d %d %d %d] %d %d %d\n",
336+ hists[0], hists[1], hists[2], hists[3], hists[4],
337+ max, b, destDiff);
338+
339+ return destDiff;
246340 }
247341
248342 size_t compress(
@@ -251,7 +345,8 @@
251345 size_t vBlockCount,
252346 const int* src,
253347 int* tmp,
254- unsigned char* tmp2, size_t tmp2Len,
348+ unsigned char* tmp2,
349+ unsigned char* tmp3,
255350 unsigned char* dest, size_t destLen
256351 )
257352 {
@@ -310,7 +405,7 @@
310405 }
311406
312407 size_t progress = 0;
313- progress += compressSub(compressor, tmp, totalBlockCount, dest+progress, destLen-progress, tmp2, tmp2Len);
408+ progress += compressSub(compressor, tmp, totalBlockCount, dest+progress, destLen-progress, tmp2, tmp3);
314409 to = tmp;
315410
316411 // AC1
@@ -325,7 +420,7 @@
325420 }
326421 readPos += fromWidth8;
327422 }
328- progress += compressSub(compressor, tmp, totalBlockCount*3, dest+progress, destLen-progress, tmp2, tmp2Len);
423+ progress += compressSub(compressor, tmp, totalBlockCount*3, dest+progress, destLen-progress, tmp2, tmp3);
329424 to = tmp;
330425
331426 // AC2
@@ -342,7 +437,7 @@
342437 }
343438 readPos += fromWidth8;
344439 }
345- progress += compressSub(compressor, tmp, totalBlockCount*5, dest+progress, destLen-progress, tmp2, tmp2Len);
440+ progress += compressSub(compressor, tmp, totalBlockCount*5, dest+progress, destLen-progress, tmp2, tmp3);
346441 to = tmp;
347442
348443 // AC3
@@ -361,7 +456,7 @@
361456 }
362457 readPos += fromWidth8;
363458 }
364- progress += compressSub(compressor, tmp, totalBlockCount*7, dest+progress, destLen-progress, tmp2, tmp2Len);
459+ progress += compressSub(compressor, tmp, totalBlockCount*7, dest+progress, destLen-progress, tmp2, tmp3);
365460 to = tmp;
366461
367462 // AC4
@@ -382,7 +477,7 @@
382477 }
383478 readPos += fromWidth8;
384479 }
385- progress += compressSub(compressor, tmp, totalBlockCount*9, dest+progress, destLen-progress, tmp2, tmp2Len);
480+ progress += compressSub(compressor, tmp, totalBlockCount*9, dest+progress, destLen-progress, tmp2, tmp3);
386481 to = tmp;
387482
388483 // AC5
@@ -405,7 +500,7 @@
405500 }
406501 readPos += fromWidth8;
407502 }
408- progress += compressSub(compressor, tmp, totalBlockCount*11, dest+progress, destLen-progress, tmp2, tmp2Len);
503+ progress += compressSub(compressor, tmp, totalBlockCount*11, dest+progress, destLen-progress, tmp2, tmp3);
409504 to = tmp;
410505
411506 // AC6
@@ -430,7 +525,7 @@
430525 }
431526 readPos += fromWidth8;
432527 }
433- progress += compressSub(compressor, tmp, totalBlockCount*13, dest+progress, destLen-progress, tmp2, tmp2Len);
528+ progress += compressSub(compressor, tmp, totalBlockCount*13, dest+progress, destLen-progress, tmp2, tmp3);
434529 to = tmp;
435530
436531 // AC7
@@ -457,7 +552,7 @@
457552 }
458553 readPos += fromWidth8;
459554 }
460- progress += compressSub(compressor, tmp, totalBlockCount*15, dest+progress, destLen-progress, tmp2, tmp2Len);
555+ progress += compressSub(compressor, tmp, totalBlockCount*15, dest+progress, destLen-progress, tmp2, tmp3);
461556
462557 return progress;
463558 }
@@ -464,25 +559,33 @@
464559
465560 size_t decompressSub(ICompressor& compressor, const unsigned char* src, unsigned char* tmp, int* dest, size_t destLen)
466561 {
562+ const unsigned char* initialSrc = src;
563+
467564 size_t signFlagBytes = *(size_t*)src;
468565 src += 4;
469566 BitReader signFlags(src);
470567 src += signFlagBytes;
471-
472- bool isCompressed = *src++ != 0;
568+
569+ unsigned char compressFlag = *src++;
473570 unsigned char b = *src++;
474571 size_t len = *(size_t*)src;
475572 src += 4;
476573
477574 size_t len2 = 0;
478- if (isCompressed) {
479- len2 = compressor.Decompress(src, len, tmp, destLen*4);
480- }else {
575+ switch (compressFlag) {
576+ case 0:
481577 memcpy(tmp, src, len);
482578 len2 = len;
579+ break;
580+ case 1:
581+ len2 = compressor.Decompress(src, len, tmp, -1);
582+ break;
583+ case 2:
584+ len2 = repeationDecompress(src, len, tmp);
585+ break;
483586 }
484-
485- if (b == RiceCoderFlag_None) {
587+
588+ if (b == RiceCoderFlag::None) {
486589 memcpy(dest, tmp, len2);
487590 for (size_t i=0; i<len2/4; ++i) {
488591 int val = dest[i];
@@ -507,8 +610,8 @@
507610 }
508611 }
509612 // showMinus(dest, dest, destLen);
510-
511- return 4 + signFlagBytes + 6 + len;
613+
614+ return 4 + signFlagBytes + + 6 + len;
512615 }
513616
514617 void decompress(
--- Compressor/BZip2Compressor.cpp (nonexistent)
+++ Compressor/BZip2Compressor.cpp (revision 7)
@@ -0,0 +1,31 @@
1+#include "stdafx.h"
2+
3+#include "BZip2Compressor.h"
4+
5+#include "bzlib.h"
6+#pragma comment(lib, "libbz2.lib")
7+
8+size_t BZip2Compressor::Compress(
9+ const unsigned char* src,
10+ size_t srcLen,
11+ unsigned char* dest,
12+ size_t destLen
13+)
14+{
15+ size_t compressedLen = destLen;
16+ int ret = BZ2_bzBuffToBuffCompress((char*)dest, &compressedLen, (char*)src, srcLen, 9, 0, 30);
17+ return compressedLen;
18+}
19+
20+size_t BZip2Compressor::Decompress(
21+ const unsigned char* src,
22+ size_t srcLen,
23+ unsigned char* dest,
24+ size_t destLen
25+)
26+{
27+ size_t decompressedLen = destLen;
28+ int ret = BZ2_bzBuffToBuffDecompress((char*)dest, &decompressedLen, (char*)src, srcLen, 0, 0);
29+ return decompressedLen;
30+}
31+
--- Compressor/bzlib.h (nonexistent)
+++ Compressor/bzlib.h (revision 7)
@@ -0,0 +1,282 @@
1+
2+/*-------------------------------------------------------------*/
3+/*--- Public header file for the library. ---*/
4+/*--- bzlib.h ---*/
5+/*-------------------------------------------------------------*/
6+
7+/* ------------------------------------------------------------------
8+ This file is part of bzip2/libbzip2, a program and library for
9+ lossless, block-sorting data compression.
10+
11+ bzip2/libbzip2 version 1.0.5 of 10 December 2007
12+ Copyright (C) 1996-2007 Julian Seward <jseward@bzip.org>
13+
14+ Please read the WARNING, DISCLAIMER and PATENTS sections in the
15+ README file.
16+
17+ This program is released under the terms of the license contained
18+ in the file LICENSE.
19+ ------------------------------------------------------------------ */
20+
21+
22+#ifndef _BZLIB_H
23+#define _BZLIB_H
24+
25+#ifdef __cplusplus
26+extern "C" {
27+#endif
28+
29+#define BZ_RUN 0
30+#define BZ_FLUSH 1
31+#define BZ_FINISH 2
32+
33+#define BZ_OK 0
34+#define BZ_RUN_OK 1
35+#define BZ_FLUSH_OK 2
36+#define BZ_FINISH_OK 3
37+#define BZ_STREAM_END 4
38+#define BZ_SEQUENCE_ERROR (-1)
39+#define BZ_PARAM_ERROR (-2)
40+#define BZ_MEM_ERROR (-3)
41+#define BZ_DATA_ERROR (-4)
42+#define BZ_DATA_ERROR_MAGIC (-5)
43+#define BZ_IO_ERROR (-6)
44+#define BZ_UNEXPECTED_EOF (-7)
45+#define BZ_OUTBUFF_FULL (-8)
46+#define BZ_CONFIG_ERROR (-9)
47+
48+typedef
49+ struct {
50+ char *next_in;
51+ unsigned int avail_in;
52+ unsigned int total_in_lo32;
53+ unsigned int total_in_hi32;
54+
55+ char *next_out;
56+ unsigned int avail_out;
57+ unsigned int total_out_lo32;
58+ unsigned int total_out_hi32;
59+
60+ void *state;
61+
62+ void *(*bzalloc)(void *,int,int);
63+ void (*bzfree)(void *,void *);
64+ void *opaque;
65+ }
66+ bz_stream;
67+
68+
69+#ifndef BZ_IMPORT
70+#define BZ_EXPORT
71+#endif
72+
73+#ifndef BZ_NO_STDIO
74+/* Need a definitition for FILE */
75+#include <stdio.h>
76+#endif
77+
78+#ifdef _WIN32
79+# include <windows.h>
80+# ifdef small
81+ /* windows.h define small to char */
82+# undef small
83+# endif
84+# ifdef BZ_EXPORT
85+# define BZ_API(func) WINAPI func
86+# define BZ_EXTERN extern
87+# else
88+ /* import windows dll dynamically */
89+# define BZ_API(func) (WINAPI * func)
90+# define BZ_EXTERN
91+# endif
92+#else
93+# define BZ_API(func) func
94+# define BZ_EXTERN extern
95+#endif
96+
97+
98+/*-- Core (low-level) library functions --*/
99+
100+BZ_EXTERN int BZ_API(BZ2_bzCompressInit) (
101+ bz_stream* strm,
102+ int blockSize100k,
103+ int verbosity,
104+ int workFactor
105+ );
106+
107+BZ_EXTERN int BZ_API(BZ2_bzCompress) (
108+ bz_stream* strm,
109+ int action
110+ );
111+
112+BZ_EXTERN int BZ_API(BZ2_bzCompressEnd) (
113+ bz_stream* strm
114+ );
115+
116+BZ_EXTERN int BZ_API(BZ2_bzDecompressInit) (
117+ bz_stream *strm,
118+ int verbosity,
119+ int small
120+ );
121+
122+BZ_EXTERN int BZ_API(BZ2_bzDecompress) (
123+ bz_stream* strm
124+ );
125+
126+BZ_EXTERN int BZ_API(BZ2_bzDecompressEnd) (
127+ bz_stream *strm
128+ );
129+
130+
131+
132+/*-- High(er) level library functions --*/
133+
134+#ifndef BZ_NO_STDIO
135+#define BZ_MAX_UNUSED 5000
136+
137+typedef void BZFILE;
138+
139+BZ_EXTERN BZFILE* BZ_API(BZ2_bzReadOpen) (
140+ int* bzerror,
141+ FILE* f,
142+ int verbosity,
143+ int small,
144+ void* unused,
145+ int nUnused
146+ );
147+
148+BZ_EXTERN void BZ_API(BZ2_bzReadClose) (
149+ int* bzerror,
150+ BZFILE* b
151+ );
152+
153+BZ_EXTERN void BZ_API(BZ2_bzReadGetUnused) (
154+ int* bzerror,
155+ BZFILE* b,
156+ void** unused,
157+ int* nUnused
158+ );
159+
160+BZ_EXTERN int BZ_API(BZ2_bzRead) (
161+ int* bzerror,
162+ BZFILE* b,
163+ void* buf,
164+ int len
165+ );
166+
167+BZ_EXTERN BZFILE* BZ_API(BZ2_bzWriteOpen) (
168+ int* bzerror,
169+ FILE* f,
170+ int blockSize100k,
171+ int verbosity,
172+ int workFactor
173+ );
174+
175+BZ_EXTERN void BZ_API(BZ2_bzWrite) (
176+ int* bzerror,
177+ BZFILE* b,
178+ void* buf,
179+ int len
180+ );
181+
182+BZ_EXTERN void BZ_API(BZ2_bzWriteClose) (
183+ int* bzerror,
184+ BZFILE* b,
185+ int abandon,
186+ unsigned int* nbytes_in,
187+ unsigned int* nbytes_out
188+ );
189+
190+BZ_EXTERN void BZ_API(BZ2_bzWriteClose64) (
191+ int* bzerror,
192+ BZFILE* b,
193+ int abandon,
194+ unsigned int* nbytes_in_lo32,
195+ unsigned int* nbytes_in_hi32,
196+ unsigned int* nbytes_out_lo32,
197+ unsigned int* nbytes_out_hi32
198+ );
199+#endif
200+
201+
202+/*-- Utility functions --*/
203+
204+BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffCompress) (
205+ char* dest,
206+ unsigned int* destLen,
207+ char* source,
208+ unsigned int sourceLen,
209+ int blockSize100k,
210+ int verbosity,
211+ int workFactor
212+ );
213+
214+BZ_EXTERN int BZ_API(BZ2_bzBuffToBuffDecompress) (
215+ char* dest,
216+ unsigned int* destLen,
217+ char* source,
218+ unsigned int sourceLen,
219+ int small,
220+ int verbosity
221+ );
222+
223+
224+/*--
225+ Code contributed by Yoshioka Tsuneo (tsuneo@rr.iij4u.or.jp)
226+ to support better zlib compatibility.
227+ This code is not _officially_ part of libbzip2 (yet);
228+ I haven't tested it, documented it, or considered the
229+ threading-safeness of it.
230+ If this code breaks, please contact both Yoshioka and me.
231+--*/
232+
233+BZ_EXTERN const char * BZ_API(BZ2_bzlibVersion) (
234+ void
235+ );
236+
237+#ifndef BZ_NO_STDIO
238+BZ_EXTERN BZFILE * BZ_API(BZ2_bzopen) (
239+ const char *path,
240+ const char *mode
241+ );
242+
243+BZ_EXTERN BZFILE * BZ_API(BZ2_bzdopen) (
244+ int fd,
245+ const char *mode
246+ );
247+
248+BZ_EXTERN int BZ_API(BZ2_bzread) (
249+ BZFILE* b,
250+ void* buf,
251+ int len
252+ );
253+
254+BZ_EXTERN int BZ_API(BZ2_bzwrite) (
255+ BZFILE* b,
256+ void* buf,
257+ int len
258+ );
259+
260+BZ_EXTERN int BZ_API(BZ2_bzflush) (
261+ BZFILE* b
262+ );
263+
264+BZ_EXTERN void BZ_API(BZ2_bzclose) (
265+ BZFILE* b
266+ );
267+
268+BZ_EXTERN const char * BZ_API(BZ2_bzerror) (
269+ BZFILE *b,
270+ int *errnum
271+ );
272+#endif
273+
274+#ifdef __cplusplus
275+}
276+#endif
277+
278+#endif
279+
280+/*-------------------------------------------------------------*/
281+/*--- end bzlib.h ---*/
282+/*-------------------------------------------------------------*/
--- Compressor/BZip2Compressor.h (nonexistent)
+++ Compressor/BZip2Compressor.h (revision 7)
@@ -0,0 +1,24 @@
1+#pragma once
2+
3+#include "ICompressor.h"
4+
5+class BZip2Compressor : public ICompressor
6+{
7+public:
8+ // overrides
9+ size_t Compress(
10+ const unsigned char* src,
11+ size_t srcLen,
12+ unsigned char* dest,
13+ size_t destLen
14+ );
15+
16+ size_t Decompress(
17+ const unsigned char* src,
18+ size_t srcLen,
19+ unsigned char* dest,
20+ size_t destLen
21+ );
22+
23+};
24+
--- Compressor/ICompressor.h (nonexistent)
+++ Compressor/ICompressor.h (revision 7)
@@ -0,0 +1,20 @@
1+#pragma once
2+
3+class ICompressor
4+{
5+public:
6+ virtual size_t Compress(
7+ const unsigned char* src,
8+ size_t srcLen,
9+ unsigned char* dest,
10+ size_t destLen
11+ ) = 0;
12+
13+ virtual size_t Decompress(
14+ const unsigned char* src,
15+ size_t srcLen,
16+ unsigned char* dest,
17+ size_t destLen
18+ ) = 0;
19+};
20+
--- Quantizer.cpp (revision 6)
+++ Quantizer.cpp (revision 7)
@@ -1,29 +1,32 @@
11 #include "stdafx.h"
22 #include "Quantizer.h"
33
4-static const int quant8_scan[16] = {
5- 0,3,4,3, 3,1,5,1, 4,5,2,5, 3,1,5,1
6-};
4+static const int QUANT8_SCAN[16] = {
5+ 0,3,4,3,
6+ 3,1,5,1,
7+ 4,5,2,5,
8+ 3,1,5,1
9+};
710
8-static const int dequant8_scale[6][6] = {
9- { 20, 18, 32, 19, 25, 24 },
10- { 22, 19, 35, 21, 28, 26 },
11- { 26, 23, 42, 24, 33, 31 },
12- { 28, 25, 45, 26, 35, 33 },
13- { 32, 28, 51, 30, 40, 38 },
14- { 36, 32, 58, 34, 46, 43 },
15-};
16-static const int quant8_scale[6][6] = {
17- { 13107, 11428, 20972, 12222, 16777, 15481 },
18- { 11916, 10826, 19174, 11058, 14980, 14290 },
19- { 10082, 8943, 15978, 9675, 12710, 11985 },
20- { 9362, 8228, 14913, 8931, 11984, 11259 },
21- { 8192, 7346, 13159, 7740, 10486, 9777 },
22- { 7282, 6428, 11570, 6830, 9118, 8640 }
23-};
11+static const int DEQUANT8_SCALE[6][6] = {
12+ { 20, 18, 32, 19, 25, 24 },
13+ { 22, 19, 35, 21, 28, 26 },
14+ { 26, 23, 42, 24, 33, 31 },
15+ { 28, 25, 45, 26, 35, 33 },
16+ { 32, 28, 51, 30, 40, 38 },
17+ { 36, 32, 58, 34, 46, 43 },
18+};
19+static const int QUANT8_SCALE[6][6] = {
20+ { 13107, 11428, 20972, 12222, 16777, 15481 },
21+ { 11916, 10826, 19174, 11058, 14980, 14290 },
22+ { 10082, 8943, 15978, 9675, 12710, 11985 },
23+ { 9362, 8228, 14913, 8931, 11984, 11259 },
24+ { 8192, 7346, 13159, 7740, 10486, 9777 },
25+ { 7282, 6428, 11570, 6830, 9118, 8640 }
26+};
2427
25-static const unsigned char quant8_intra_matrix[8][8] = {
26- 6, 10, 13, 16, 18, 23, 25, 27,
28+static const unsigned char QUANT8_INTRA_MATRIX[8][8] = {
29+ 6, 10, 13, 16, 18, 23, 25, 27,
2730 10, 11, 16, 18, 23, 25, 27, 29,
2831 13, 16, 18, 23, 25, 27, 29, 31,
2932 16, 18, 23, 25, 27, 29, 31, 33,
@@ -33,17 +36,6 @@
3336 27, 29, 31, 33, 36, 38, 40, 42,
3437 };
3538
36-static const unsigned char quant8_inter_matrix[8][8] = {
37- 9, 13, 15, 17, 19, 21, 22, 24,
38- 13, 13, 17, 19, 21, 22, 24, 25,
39- 15, 17, 19, 21, 22, 24, 25, 27,
40- 17, 19, 21, 22, 24, 25, 27, 28,
41- 19, 21, 22, 24, 25, 27, 28, 30,
42- 21, 22, 24, 25, 27, 28, 30, 32,
43- 22, 24, 25, 27, 28, 30, 32, 33,
44- 24, 25, 27, 28, 30, 32, 33, 35,
45-};
46-
4739 namespace {
4840
4941 int div(int n, int d)
@@ -59,14 +51,14 @@
5951 QP_shift = QP / 6;
6052 QP_remain = QP % 6;
6153
62- for (size_t i=0; i<64; ++i) {
63- int j = quant8_scan[((i>>1)&12) | (i&3)];
54+ for (size_t i=0; i<64; ++i) {
55+ int j = QUANT8_SCAN[((i>>1)&12) | (i&3)];
6456 // printf("%d,", j);
65- quant8_table[i] = quant8_scale[QP_remain][j];
66- dequant8_table[i] = dequant8_scale[QP_remain][j] << QP_shift;
57+ quant8_table[i] = QUANT8_SCALE[QP_remain][j];
58+ dequant8_table[i] = DEQUANT8_SCALE[QP_remain][j] << QP_shift;
6759 }
6860
69- // 量子化テーブル操作
61+ // ?ʎq???e?[?u??????
7062 for (size_t i=0; i<8; ++i) {
7163 if (i>=(8-hBlockness)) {
7264 for (size_t j=0; j<8; ++j) {
@@ -80,12 +72,12 @@
8072 }
8173 }
8274
83- // 高周波を荒くする量子化マトリクスを適用
75+ // ?????g???r??????ʎq???}?g???N?X??K?p
8476 if (useQuantMatrix) {
8577 for (size_t i=0; i<8; ++i) {
8678 for (size_t j=0; j<8; ++j) {
87- quant8_table[i*8+j] = div(quant8_table[i*8+j] << 4, quant8_intra_matrix[i][j]);
88- dequant8_table[i*8+j] *= quant8_intra_matrix[i][j];
79+ quant8_table[i*8+j] = div(quant8_table[i*8+j] << 4, QUANT8_INTRA_MATRIX[i][j]);
80+ dequant8_table[i*8+j] *= QUANT8_INTRA_MATRIX[i][j];
8981 }
9082 }
9183 }else {
--- main.cpp (revision 6)
+++ main.cpp (revision 7)
@@ -10,7 +10,10 @@
1010 #include <cmath>
1111
1212 #include <boost/integer_traits.hpp>
13-#include "BZip2Compressor.h"
13+using namespace boost;
14+#include <boost/cstdint.hpp>
15+
16+#include "Compressor/BZip2Compressor.h"
1417 #include "misc.h"
1518 #include "decode.h"
1619 #include "encode.h"
@@ -18,16 +21,16 @@
1821 #include "ReadImage/ReadImage.h"
1922 #include "ReadImage/File.h"
2023
21-int main(int argc, char* argv[])
24+int _tmain(int argc, _TCHAR* argv[])
2225 {
2326 if (argc < 2) {
24- printf("specify filename\n");
27+ _tprintf(_T("specify filename\n"));
2528 return 1;
2629 }
2730
28- FILE* f = fopen(argv[1], "rb");
31+ FILE* f = _tfopen(argv[1], _T("rb"));
2932 if (!f) {
30- printf("failed to open file : %s\n", argv[1]);
33+ _tprintf(_T("failed to open file : %s\n"), argv[1]);
3134 return 1;
3235 }
3336 File fo(f);
@@ -52,7 +55,7 @@
5255 }
5356
5457 Quantizer quantizer;
55- quantizer.init(6*8+0, 0, 0, true);
58+ quantizer.init(6*8+1, 0, 0, false);
5659
5760 const size_t hBlockCount = width / 8 + ((width % 8) ? 1 : 0);
5861 const size_t vBlockCount = height / 8 + ((height % 8) ? 1 : 0);
@@ -59,6 +62,28 @@
5962 const size_t totalBlockCount = hBlockCount * vBlockCount;
6063
6164 encode(quantizer, hBlockCount, vBlockCount, &in[0], width, &work[0], width*sizeof(int));
65+
66+ if (0) {
67+ int cutoff_table[8][8] = {
68+ -1,-1,-1,-1,-1,-1, 1, 1,
69+ -1,-1,-1,-1,-1,-1, 1, 1,
70+ -1,-1,-1,-1,-1,-1, 1, 1,
71+ -1,-1,-1,-1,-1,-1, 1, 1,
72+ -1,-1,-1,-1,-1,-1, 1, 1,
73+ -1,-1,-1,-1,-1,-1, 1, 1,
74+ 1, 1, 1, 1, 1, 1, 1, 1,
75+ 1, 1, 1, 1, 1, 1, 1, 1,
76+ };
77+ for (size_t i=0; i<8; ++i) {
78+ for (size_t j=0; j<8; ++j) {
79+ if (cutoff_table[i][j] < 0) {
80+ cutoff_table[i][j] = boost::integer_traits<int>::const_max;
81+ }
82+ }
83+ }
84+ cutoff(hBlockCount, vBlockCount, &work[0], width*sizeof(int), cutoff_table);
85+ }
86+
6287 int* pWork = &work[0];
6388
6489 BZip2Compressor compressor;
@@ -66,8 +91,9 @@
6691
6792 size_t storageSize = work.size()*4*1.1+600;
6893 std::vector<unsigned char> work3(storageSize);
94+ std::vector<unsigned char> work4(storageSize);
6995 std::vector<unsigned char> compressed(storageSize);
70- compressedLen = compress(compressor, hBlockCount, vBlockCount, &work[0], &work2[0], &work3[0], work3.size(), &compressed[0], compressed.size());
96+ compressedLen = compress(compressor, hBlockCount, vBlockCount, &work[0], &work2[0], &work3[0], &work4[0], &compressed[0], compressed.size());
7197
7298 std::fill(work.begin(), work.end(), 0);
7399 std::fill(work2.begin(), work2.end(), 0);
@@ -82,6 +108,6 @@
82108 //fwrite(pOutput, 1, size, of);
83109 //fclose(of);
84110
85- printf("%f%% %zu bytes", (100.0 * compressedLen) / size, compressedLen);
111+ _tprintf(_T("%f%% %d bytes"), (100.0 * compressedLen) / size, compressedLen);
86112 return 0;
87113 }
--- ReadImage/ReadImage.cpp (revision 6)
+++ ReadImage/ReadImage.cpp (revision 7)
@@ -4,38 +4,8 @@
44
55 #define TRACE printf
66 #include <assert.h>
7-#include <stdio.h>
87
9-//#include <windows.h>
10-
11-#define BI_BITFIELDS 3
12-
13-typedef unsigned short WORD;
14-typedef unsigned int DWORD;
15-typedef long LONG;
16-
17-typedef struct tagBITMAPINFOHEADER {
18- DWORD biSize;
19- LONG biWidth;
20- LONG biHeight;
21- WORD biPlanes;
22- WORD biBitCount;
23- DWORD biCompression;
24- DWORD biSizeImage;
25- LONG biXPelsPerMeter;
26- LONG biYPelsPerMeter;
27- DWORD biClrUsed;
28- DWORD biClrImportant;
29-} BITMAPINFOHEADER;
30-
31-typedef struct tagBITMAPFILEHEADER {
32- WORD bfType;
33- DWORD bfSize; /* DWORD (4バイト) 境界にアラインされていない点に注意.*/
34- WORD bfReserved1;
35- WORD bfReserved2;
36- DWORD bfOffBits; /* DWORD (4バイト) 境界にアラインされていない点に注意.*/
37-} BITMAPFILEHEADER;
38-
8+#include <windows.h>
399
4010 bool Read_BITMAPINFOHEADER(IFile& file, BITMAPINFOHEADER& bmih)
4111 {
@@ -77,7 +47,7 @@
7747
7848 bool ReadImageData_BMP(IFile& file, unsigned char* dest, int lineOffset, void* palettes)
7949 {
80- file.Seek(0, SEEK_SET);
50+ file.Seek(0, FILE_BEGIN);
8151 BITMAPINFOHEADER bmih;
8252 if (!Read_BITMAPINFOHEADER(file, bmih)) {
8353 return false;
--- ReadImage/File.cpp (revision 6)
+++ ReadImage/File.cpp (revision 7)
@@ -1,52 +1,60 @@
11 #include "stdafx.h"
22 #include "File.h"
33
4-//#include <io.h>
4+#include <io.h>
55 #include <assert.h>
66 #include <memory.h>
77 #include <stdio.h>
88
9-File::File(FILE* file)
9+#include <windows.h>
10+
11+File::File(HANDLE hFile)
12+ :
13+ hFile_(hFile)
1014 {
11- file_ = file;
1215 }
1316
17+File::File(FILE* pFile)
18+{
19+ hFile_ = (HANDLE) _get_osfhandle(_fileno(pFile));
20+}
21+
1422 bool File::Read(void* pBuffer, size_t nNumberOfBytesToRead, size_t& nNumberOfBytesRead)
15-{
16- nNumberOfBytesRead = fread(pBuffer, 1, nNumberOfBytesToRead, file_);
17- return nNumberOfBytesRead;
23+{
24+ return ReadFile(hFile_, pBuffer, nNumberOfBytesToRead, (LPDWORD)&nNumberOfBytesRead, NULL);
1825 }
1926
2027 bool File::Write(const void* pBuffer, size_t nNumberOfBytesToWrite, size_t& nNumberOfBytesWritten)
21-{
22- nNumberOfBytesWritten = fwrite(pBuffer, 1, nNumberOfBytesToWrite, file_);
23- return nNumberOfBytesWritten;
28+{
29+ return WriteFile(hFile_, pBuffer, nNumberOfBytesToWrite, (LPDWORD)&nNumberOfBytesWritten, NULL);
2430 }
2531
2632 size_t File::Seek(long lDistanceToMove, size_t dwMoveMethod)
27-{
28- return fseek(file_, lDistanceToMove, dwMoveMethod);
33+{
34+ return SetFilePointer(hFile_, lDistanceToMove, NULL, dwMoveMethod);
2935 }
3036
3137 size_t File::Tell() const
32-{
33- return ftell(file_);
38+{
39+ /*
40+ -- Reference --
41+ http://nukz.net/reference/fileio/hh/winbase/filesio_3vhu.htm
42+ */
43+ return SetFilePointer(
44+ hFile_, // must have GENERIC_READ and/or GENERIC_WRITE
45+ 0, // do not move pointer
46+ NULL, // hFile is not large enough to need this pointer
47+ FILE_CURRENT
48+ ); // provides offset from current position
3449 }
3550
3651 size_t File::Size() const
37-{
38- fpos_t pos;
39- fgetpos( file_, &pos );
40- fseek( file_, 0, SEEK_END );
41- long pos2 = ftell(file_);
42-// fgetpos( file_, &pos2 );
43- fsetpos( file_, &pos );
44- return pos2;
45-// return GetFileSize(hFile_, NULL);
52+{
53+ return GetFileSize(hFile_, NULL);
4654 }
4755
4856 bool File::Flush()
4957 {
50- fflush(file_);
58+ return FlushFileBuffers(hFile_);
5159 }
5260
--- ReadImage/File.h (revision 6)
+++ ReadImage/File.h (revision 7)
@@ -2,13 +2,16 @@
22
33 #include "IFile.h"
44
5+#include <windows.h>
56 #include <stdio.h>
67
78 class File : public IFile
89 {
910 public:
10- File(FILE* file);
11+ File(HANDLE hFile);
1112
13+ File(FILE* pFile);
14+
1215 bool Read(void* pBuffer, size_t nNumberOfBytesToRead, size_t& nNumberOfBytesRead);
1316
1417 bool Write(const void* pBuffer, size_t nNumberOfBytesToWrite, size_t& nNumberOfBytesWritten);
@@ -25,6 +28,6 @@
2528 virtual const void* GetBuffer() const { return 0; }
2629
2730 private:
28- FILE* file_;
31+ HANDLE hFile_;
2932 };
3033
--- ReadImage/IFile.h (revision 6)
+++ ReadImage/IFile.h (revision 7)
@@ -2,21 +2,19 @@
22
33 /*!
44
5-概要
6- 画像の入出力用に仮想的にFileを扱えるようにする
5+?T?v
6+ ?摜?̓??o?͗p?ɉ??z?I??File????????悤?ɂ???
77
8-制限
9- 非同期操作には対応しない
10- 2GBまで
8+????
9+ ?񓯊?????ɂ͑Ή????Ȃ?
10+ 2GB?܂?
1111
12-備考
13- CxImage by Davide Pizzolato (http://www.xdp.it/cximage.htm) を参考にしました。
12+???l
13+ CxImage by Davide Pizzolato (http://www.xdp.it/cximage.htm) ???Q?l?ɂ??܂????B
1414
15- InterfaceをWindowsAPIのFile関数に似せています
15+ Interface??WindowsAPI??File?֐??Ɏ????Ă??܂?
1616
1717 */
18-
19-#include <stdlib.h>
2018
2119 class IFile
2220 {
Afficher sur ancien navigateur de dépôt.