• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

減色プログラム


Commit MetaInfo

修订版96481f8cc65d486c61adae5a17f7a16fb6cd3c40 (tree)
时间2011-05-29 17:03:27
作者berupon <berupon@gmai...>
Commiterberupon

Log Message

確認がし易いように出力ファイルフォーマットをRAWからBMPに変更。

更改概述

差异

--- /dev/null
+++ b/ImageIO/ImageIO.cpp
@@ -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+
--- /dev/null
+++ b/ImageIO/ImageIO.h
@@ -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+
--- a/ReadImage/ReadImage.cpp
+++ /dev/null
@@ -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-
--- a/ReadImage/ReadImage.h
+++ /dev/null
@@ -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-
--- a/main.cpp
+++ b/main.cpp
@@ -36,8 +36,8 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3636 #include <limits>
3737 #include <iostream>
3838
39-#include "ReadImage/ReadImage.h"
40-#include "ReadImage/File.h"
39+#include "ImageIO/ImageIO.h"
40+#include "ImageIO/File.h"
4141
4242 #include "quantize.h"
4343 #include "dxor.h"
@@ -49,7 +49,7 @@ size_t call_count = 0;
4949 int _tmain(int argc, _TCHAR* argv[])
5050 {
5151 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");
5353 return -1;
5454 }
5555
@@ -83,7 +83,7 @@ int _tmain(int argc, _TCHAR* argv[])
8383 lineBytes += 4 - (lineBytes % 4);
8484 }
8585 std::vector<uint8_t> buff(lineBytes * imageInfo.height);
86- ReadImageData(file, &buff[0], lineBytes, 0);
86+ ReadImage(file, imageInfo, &buff[0], lineBytes, 0);
8787 fclose(f);
8888 uint8_t* p = &buff[0];
8989
@@ -92,11 +92,20 @@ int _tmain(int argc, _TCHAR* argv[])
9292 Image image(width, height);
9393 Color* pLine = image.pBuff_;
9494 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+ }
95103 for (size_t y=0; y<height; ++y) {
96104 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] ];
100109 pLine[x] = Color(r,g,b,0.0);
101110 }
102111 pLine += width;
@@ -194,18 +203,19 @@ int _tmain(int argc, _TCHAR* argv[])
194203 printf("Could not open output file '%s'.\n", argv[2]);
195204 return -1;
196205 }
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];
198209 for (int y=0; y<height; y++) {
199210 for (int x=0; x<width; x++) {
200211 const uint8_t idx = quantized_image[y][x];
201212 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);
207216 }
208217 }
218+ WriteImage(file, imageInfo, &bytes[0], imageInfo.width * 3);
209219 fclose(out);
210220 }
211221
--- a/vs2008/color_quantizer.vcproj
+++ b/vs2008/color_quantizer.vcproj
@@ -418,58 +418,34 @@
418418 </File>
419419 </Filter>
420420 <Filter
421- Name="ReadImage"
421+ Name="ImageIO"
422422 >
423423 <File
424- RelativePath="..\ReadImage\File.cpp"
424+ RelativePath="..\ImageIO\File.cpp"
425425 >
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>
442426 </File>
443427 <File
444- RelativePath="..\ReadImage\File.h"
428+ RelativePath="..\ImageIO\File.h"
445429 >
446430 </File>
447431 <File
448- RelativePath="..\ReadImage\IFile.h"
432+ RelativePath="..\ImageIO\IFile.h"
449433 >
450434 </File>
451435 <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"
453445 >
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>
470446 </File>
471447 <File
472- RelativePath="..\ReadImage\ReadImage.h"
448+ RelativePath="..\ImageIO\ImageIO_bmp.h"
473449 >
474450 </File>
475451 </Filter>
--- a/vs2010/color_quantizer.vcxproj
+++ b/vs2010/color_quantizer.vcxproj
@@ -79,12 +79,18 @@
7979 <EnableEnhancedInstructionSet>StreamingSIMDExtensions2</EnableEnhancedInstructionSet>
8080 <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
8181 <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>
8287 </ClCompile>
8388 <Link>
8489 <SubSystem>Console</SubSystem>
8590 <GenerateDebugInformation>true</GenerateDebugInformation>
8691 <EnableCOMDATFolding>true</EnableCOMDATFolding>
8792 <OptimizeReferences>true</OptimizeReferences>
93+ <RandomizedBaseAddress>false</RandomizedBaseAddress>
8894 </Link>
8995 </ItemDefinitionGroup>
9096 <ItemGroup>