減色プログラム
Revision | 96481f8cc65d486c61adae5a17f7a16fb6cd3c40 (tree) |
---|---|
Time | 2011-05-29 17:03:27 |
Author | berupon <berupon@gmai...> |
Commiter | berupon |
確認がし易いように出力ファイルフォーマットをRAWからBMPに変更。
@@ -0,0 +1,19 @@ | ||
1 | +#include "stdafx.h" | |
2 | + | |
3 | +#include "ImageIO_bmp.h" | |
4 | + | |
5 | +bool ReadImageInfo(IFile& file, ImageInfo& info) | |
6 | +{ | |
7 | + return ReadImageInfo_bmp(file, info); | |
8 | +} | |
9 | + | |
10 | +bool ReadImage(IFile& file, ImageInfo& info, void* dest, int lineOffset, void* palettes) | |
11 | +{ | |
12 | + return ReadImage_bmp(file, info, dest, lineOffset, palettes); | |
13 | +} | |
14 | + | |
15 | +bool WriteImage(IFile& file, const ImageInfo& info, const void* data, int lineOffset) | |
16 | +{ | |
17 | + return WriteImage_bmp(file, info, data, lineOffset); | |
18 | +} | |
19 | + |
@@ -0,0 +1,24 @@ | ||
1 | +#pragma once | |
2 | + | |
3 | +#include "IFile.h" | |
4 | + | |
5 | +enum ImageFileFormatType { | |
6 | + ImageFileFormatType_Unknown, | |
7 | + ImageFileFormatType_BMP, | |
8 | + ImageFileFormatType_PNG, | |
9 | +}; | |
10 | + | |
11 | +struct ImageInfo | |
12 | +{ | |
13 | + ImageFileFormatType fileFormatType; | |
14 | + size_t width; | |
15 | + size_t height; | |
16 | + size_t bitsPerSample; | |
17 | + size_t samplesPerPixel; | |
18 | +}; | |
19 | + | |
20 | +bool ReadImageInfo(IFile& file, ImageInfo& info); | |
21 | +bool ReadImage(IFile& file, ImageInfo& info, void* dest, int lineOffset, void* palettes); | |
22 | + | |
23 | +bool WriteImage(IFile& file, const ImageInfo& info, const void* data, int lineOffset); | |
24 | + |
@@ -1,130 +0,0 @@ | ||
1 | - | |
2 | -#include "ReadImage.h" | |
3 | - | |
4 | -#include <stdio.h> | |
5 | -#define TRACE printf | |
6 | -#include <assert.h> | |
7 | - | |
8 | -#include <windows.h> | |
9 | - | |
10 | -bool Read_BITMAPINFOHEADER(IFile& file, BITMAPINFOHEADER& bmih) | |
11 | -{ | |
12 | - size_t fileSize = file.Size(); | |
13 | - if (fileSize <= 54) { | |
14 | - TRACE("file size <= 54 bytes."); | |
15 | - return false; | |
16 | - } | |
17 | - BITMAPFILEHEADER bmpFileHeader; | |
18 | - size_t readBytes = 0; | |
19 | - file.Read(&bmpFileHeader, 14, readBytes); | |
20 | - assert(readBytes == 14); | |
21 | - if (bmpFileHeader.bfType != 19778) { | |
22 | - TRACE("file hedear bfType != 19778"); | |
23 | - return false; | |
24 | - } | |
25 | - | |
26 | - file.Read(&bmih, 40, readBytes); | |
27 | - if (readBytes != 40) { | |
28 | - TRACE("bmih shortage."); | |
29 | - return false; | |
30 | - } | |
31 | - | |
32 | - return true; | |
33 | -} | |
34 | - | |
35 | -bool ReadImageInfo_BMP(IFile& file, ImageInfo& info) | |
36 | -{ | |
37 | - BITMAPINFOHEADER bmih; | |
38 | - if (!Read_BITMAPINFOHEADER(file, bmih)) { | |
39 | - return false; | |
40 | - } | |
41 | - info.width = bmih.biWidth; | |
42 | - info.height = bmih.biHeight; | |
43 | - switch (bmih.biBitCount) { | |
44 | - case 1: | |
45 | - case 2: | |
46 | - case 4: | |
47 | - return false; | |
48 | - break; | |
49 | - case 8: | |
50 | - info.bitsPerSample = 8; | |
51 | - info.samplesPerPixel = 1; | |
52 | - break; | |
53 | - case 16: | |
54 | - return false; | |
55 | - case 24: | |
56 | - info.bitsPerSample = 8; | |
57 | - info.samplesPerPixel = 3; | |
58 | - break; | |
59 | - case 32: | |
60 | - info.bitsPerSample = 8; | |
61 | - info.samplesPerPixel = 4; | |
62 | - break; | |
63 | - } | |
64 | - return true; | |
65 | -} | |
66 | - | |
67 | -bool ReadImageData_BMP(IFile& file, unsigned char* dest, int lineOffset, void* palettes) | |
68 | -{ | |
69 | - file.Seek(0, FILE_BEGIN); | |
70 | - BITMAPINFOHEADER bmih; | |
71 | - if (!Read_BITMAPINFOHEADER(file, bmih)) { | |
72 | - return false; | |
73 | - } | |
74 | - size_t bmiColorsLen = 0; | |
75 | - switch (bmih.biBitCount) { | |
76 | - case 1: | |
77 | - bmiColorsLen = 2; | |
78 | - break; | |
79 | - case 4: | |
80 | - bmiColorsLen = 16; | |
81 | - break; | |
82 | - case 8: | |
83 | - bmiColorsLen = 256; | |
84 | - break; | |
85 | - case 16: | |
86 | - TRACE("16 bit BMP not supported."); | |
87 | - return false; | |
88 | - break; | |
89 | - case 32: | |
90 | - if (bmih.biCompression == BI_BITFIELDS) { | |
91 | - bmiColorsLen = 3; | |
92 | - } | |
93 | - } | |
94 | - size_t readBytes = 0; | |
95 | - if (bmiColorsLen) { | |
96 | - size_t needBytes = /*sizeof(RGBQUAD)*/4*bmiColorsLen; | |
97 | - file.Read(palettes, needBytes, readBytes); | |
98 | - if (readBytes != needBytes) { | |
99 | - TRACE("bmiColors read failed."); | |
100 | - return false; | |
101 | - } | |
102 | - } | |
103 | - const int lineBytes = ((((bmih.biWidth * bmih.biBitCount) + 31) & ~31) >> 3); | |
104 | - const int lineIdx = (bmih.biHeight < 0) ? 0 : (bmih.biHeight-1); | |
105 | - OffsetPtr(dest, lineOffset * lineIdx); | |
106 | - lineOffset *= (bmih.biHeight < 0) ? 1 : -1; | |
107 | - | |
108 | - const size_t height = abs(bmih.biHeight); | |
109 | - for (size_t y=0; y<height; ++y) { | |
110 | - file.Read(dest, lineBytes, readBytes); | |
111 | - if (readBytes != lineBytes) { | |
112 | - TRACE("read bytes != lineBytes."); | |
113 | - return false; | |
114 | - } | |
115 | - OffsetPtr(dest, lineOffset); | |
116 | - } | |
117 | - | |
118 | - return true; | |
119 | -} | |
120 | - | |
121 | -bool ReadImageInfo(IFile& file, ImageInfo& info) | |
122 | -{ | |
123 | - return ReadImageInfo_BMP(file, info); | |
124 | -} | |
125 | - | |
126 | -bool ReadImageData(IFile& file, unsigned char* dest, int lineOffset, void* palettes) | |
127 | -{ | |
128 | - return ReadImageData_BMP(file, dest, lineOffset, palettes); | |
129 | -} | |
130 | - |
@@ -1,15 +0,0 @@ | ||
1 | -#pragma once | |
2 | - | |
3 | -#include "IFile.h" | |
4 | - | |
5 | -struct ImageInfo | |
6 | -{ | |
7 | - size_t width; | |
8 | - size_t height; | |
9 | - size_t bitsPerSample; | |
10 | - size_t samplesPerPixel; | |
11 | -}; | |
12 | - | |
13 | -bool ReadImageInfo(IFile& file, ImageInfo& info); | |
14 | -bool ReadImageData(IFile& file, unsigned char* dest, int lineOffset, void* palettes); | |
15 | - |
@@ -36,8 +36,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
36 | 36 | #include <limits> |
37 | 37 | #include <iostream> |
38 | 38 | |
39 | -#include "ReadImage/ReadImage.h" | |
40 | -#include "ReadImage/File.h" | |
39 | +#include "ImageIO/ImageIO.h" | |
40 | +#include "ImageIO/File.h" | |
41 | 41 | |
42 | 42 | #include "quantize.h" |
43 | 43 | #include "dxor.h" |
@@ -49,7 +49,7 @@ size_t call_count = 0; | ||
49 | 49 | int _tmain(int argc, _TCHAR* argv[]) |
50 | 50 | { |
51 | 51 | if (argc < 1+3 || argc > 1+5) { |
52 | - puts("Usage: color_quantizer <source image.bmp> <output image.raw> <desired palette size> [dithering level] [filter size (1/3/5)]\n"); | |
52 | + puts("Usage: color_quantizer <source image.bmp> <output image.bmp> <desired palette size> [dithering level] [filter size (1/3/5)]\n"); | |
53 | 53 | return -1; |
54 | 54 | } |
55 | 55 |
@@ -83,7 +83,7 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
83 | 83 | lineBytes += 4 - (lineBytes % 4); |
84 | 84 | } |
85 | 85 | std::vector<uint8_t> buff(lineBytes * imageInfo.height); |
86 | - ReadImageData(file, &buff[0], lineBytes, 0); | |
86 | + ReadImage(file, imageInfo, &buff[0], lineBytes, 0); | |
87 | 87 | fclose(f); |
88 | 88 | uint8_t* p = &buff[0]; |
89 | 89 |
@@ -92,11 +92,20 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
92 | 92 | Image image(width, height); |
93 | 93 | Color* pLine = image.pBuff_; |
94 | 94 | uint8_t* pSrcLine = p; |
95 | + | |
96 | + double gamma = 1.0; | |
97 | + double lut[256]; | |
98 | + lut[0] = 0.0; | |
99 | + lut[255] = 1.0; | |
100 | + for (size_t i=1; i<255; ++i) { | |
101 | + lut[i] = pow(i/255.0, 1.0/gamma); | |
102 | + } | |
95 | 103 | for (size_t y=0; y<height; ++y) { |
96 | 104 | for (size_t x=0; x<width; ++x) { |
97 | - double r = pSrcLine[x*3+0] / 255.0; | |
98 | - double g = pSrcLine[x*3+1] / 255.0; | |
99 | - double b = pSrcLine[x*3+2] / 255.0; | |
105 | + const uint8_t* pPixel = pSrcLine + x * 3; | |
106 | + double r = lut[ pPixel[0] ]; | |
107 | + double g = lut[ pPixel[1] ]; | |
108 | + double b = lut[ pPixel[2] ]; | |
100 | 109 | pLine[x] = Color(r,g,b,0.0); |
101 | 110 | } |
102 | 111 | pLine += width; |
@@ -194,18 +203,19 @@ int _tmain(int argc, _TCHAR* argv[]) | ||
194 | 203 | printf("Could not open output file '%s'.\n", argv[2]); |
195 | 204 | return -1; |
196 | 205 | } |
197 | - unsigned char c[3] = {0,0,0}; | |
206 | + File file(out); | |
207 | + std::vector<uint8_t> bytes(imageInfo.width * imageInfo.height * 3); | |
208 | + uint8_t* pLine = &bytes[0]; | |
198 | 209 | for (int y=0; y<height; y++) { |
199 | 210 | for (int x=0; x<width; x++) { |
200 | 211 | const uint8_t idx = quantized_image[y][x]; |
201 | 212 | Color col = palette[idx]; |
202 | - col *= 255.0; | |
203 | - c[2] = (unsigned char)(col[0]); | |
204 | - c[1] = (unsigned char)(col[1]); | |
205 | - c[0] = (unsigned char)(col[2]); | |
206 | - fwrite(c, 3, 1, out); | |
213 | + *pLine++ = (unsigned char)(pow(col[0], gamma) * 255.0); | |
214 | + *pLine++ = (unsigned char)(pow(col[1], gamma) * 255.0); | |
215 | + *pLine++ = (unsigned char)(pow(col[2], gamma) * 255.0); | |
207 | 216 | } |
208 | 217 | } |
218 | + WriteImage(file, imageInfo, &bytes[0], imageInfo.width * 3); | |
209 | 219 | fclose(out); |
210 | 220 | } |
211 | 221 |
@@ -418,58 +418,34 @@ | ||
418 | 418 | </File> |
419 | 419 | </Filter> |
420 | 420 | <Filter |
421 | - Name="ReadImage" | |
421 | + Name="ImageIO" | |
422 | 422 | > |
423 | 423 | <File |
424 | - RelativePath="..\ReadImage\File.cpp" | |
424 | + RelativePath="..\ImageIO\File.cpp" | |
425 | 425 | > |
426 | - <FileConfiguration | |
427 | - Name="Debug|Win32" | |
428 | - > | |
429 | - <Tool | |
430 | - Name="VCCLCompilerTool" | |
431 | - UsePrecompiledHeader="0" | |
432 | - /> | |
433 | - </FileConfiguration> | |
434 | - <FileConfiguration | |
435 | - Name="Release|Win32" | |
436 | - > | |
437 | - <Tool | |
438 | - Name="VCCLCompilerTool" | |
439 | - UsePrecompiledHeader="0" | |
440 | - /> | |
441 | - </FileConfiguration> | |
442 | 426 | </File> |
443 | 427 | <File |
444 | - RelativePath="..\ReadImage\File.h" | |
428 | + RelativePath="..\ImageIO\File.h" | |
445 | 429 | > |
446 | 430 | </File> |
447 | 431 | <File |
448 | - RelativePath="..\ReadImage\IFile.h" | |
432 | + RelativePath="..\ImageIO\IFile.h" | |
449 | 433 | > |
450 | 434 | </File> |
451 | 435 | <File |
452 | - RelativePath="..\ReadImage\ReadImage.cpp" | |
436 | + RelativePath="..\ImageIO\ImageIO.cpp" | |
437 | + > | |
438 | + </File> | |
439 | + <File | |
440 | + RelativePath="..\ImageIO\ImageIO.h" | |
441 | + > | |
442 | + </File> | |
443 | + <File | |
444 | + RelativePath="..\ImageIO\ImageIO_bmp.cpp" | |
453 | 445 | > |
454 | - <FileConfiguration | |
455 | - Name="Debug|Win32" | |
456 | - > | |
457 | - <Tool | |
458 | - Name="VCCLCompilerTool" | |
459 | - UsePrecompiledHeader="0" | |
460 | - /> | |
461 | - </FileConfiguration> | |
462 | - <FileConfiguration | |
463 | - Name="Release|Win32" | |
464 | - > | |
465 | - <Tool | |
466 | - Name="VCCLCompilerTool" | |
467 | - UsePrecompiledHeader="0" | |
468 | - /> | |
469 | - </FileConfiguration> | |
470 | 446 | </File> |
471 | 447 | <File |
472 | - RelativePath="..\ReadImage\ReadImage.h" | |
448 | + RelativePath="..\ImageIO\ImageIO_bmp.h" | |
473 | 449 | > |
474 | 450 | </File> |
475 | 451 | </Filter> |
@@ -79,12 +79,18 @@ | ||
79 | 79 | <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet> |
80 | 80 | <RuntimeLibrary>MultiThreaded</RuntimeLibrary> |
81 | 81 | <AdditionalOptions>/arch:__AVX %(AdditionalOptions)</AdditionalOptions> |
82 | + <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed> | |
83 | + <ExceptionHandling>false</ExceptionHandling> | |
84 | + <FloatingPointExceptions>false</FloatingPointExceptions> | |
85 | + <RuntimeTypeInfo>false</RuntimeTypeInfo> | |
86 | + <BrowseInformation>true</BrowseInformation> | |
82 | 87 | </ClCompile> |
83 | 88 | <Link> |
84 | 89 | <SubSystem>Console</SubSystem> |
85 | 90 | <GenerateDebugInformation>true</GenerateDebugInformation> |
86 | 91 | <EnableCOMDATFolding>true</EnableCOMDATFolding> |
87 | 92 | <OptimizeReferences>true</OptimizeReferences> |
93 | + <RandomizedBaseAddress>false</RandomizedBaseAddress> | |
88 | 94 | </Link> |
89 | 95 | </ItemDefinitionGroup> |
90 | 96 | <ItemGroup> |