1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// ImageFactory.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
#include <gdiplus.h>
#include <FreeImage.h>
using namespace std;
void bmp2PNG8(const char* bmp_fileName, const char* save_fileName);
void bmp2GIF8(const char* bmp_fileName, const char* save_fileName);
int main()
{
//初始化 FreeImage
FreeImage_Initialise();
//测试函数
bmp2PNG8("test.bmp","test1.png");
bmp2GIF8("test.bmp", "test1.gif");
//释放 FreeImage
FreeImage_DeInitialise();
return 0;
}
void bmp2PNG8(const char* bmp_fileName, const char* save_fileName)
{
FIBITMAP* hDIB24bpp = FreeImage_Load(FIF_BMP, bmp_fileName, 0);
if (hDIB24bpp) {
FIBITMAP* res = FreeImage_Rescale(hDIB24bpp, 60, 40);
// color-quantize 24bpp (results in a 8bpp bitmap to set transparency)
FIBITMAP* hDIB8bpp = FreeImage_ColorQuantize(res, FIQ_WUQUANT);
// 读取调色板并进行修改
RGBQUAD* Palette = FreeImage_GetPalette(hDIB8bpp);
BYTE Transparency[256];
for (unsigned i = 0; i < 256; i++) {
Transparency[i] = 0xFF;
//判断调色板中RGB 白色时, 修改为透明色
if (Palette[i].rgbGreen == 0xff && Palette[i].rgbBlue == 0xff && Palette[i].rgbRed == 0xff) {
Transparency[i] = 0x00;
}
}
//设置调色板
FreeImage_SetTransparencyTable(hDIB8bpp, Transparency, 256);
//保存图像
FreeImage_Save(FIF_PNG, hDIB8bpp, save_fileName, 0);
FreeImage_Unload(hDIB24bpp);
FreeImage_Unload(hDIB8bpp);
FreeImage_Unload(res);
}
else
{
cout << "读取错误" << endl;
}
}
void bmp2GIF8(const char* bmp_fileName, const char* save_fileName)
{
FIBITMAP* hDIB24bpp = FreeImage_Load(FIF_BMP, bmp_fileName, 0);
if (hDIB24bpp) {
FIBITMAP* res = FreeImage_Rescale(hDIB24bpp, 1600, 800);
// color-quantize 24bpp (results in a 8bpp bitmap to set transparency)
FIBITMAP* hDIB8bpp = FreeImage_ColorQuantize(res, FIQ_WUQUANT);
// 读取调色板并进行修改
RGBQUAD* Palette = FreeImage_GetPalette(hDIB8bpp);
BYTE Transparency[256];
for (unsigned i = 0; i < 256; i++) {
Transparency[i] = 0xFF;
//判断调色板中RGB 白色时, 修改为透明色
if (Palette[i].rgbGreen == 0xff && Palette[i].rgbBlue == 0xff && Palette[i].rgbRed == 0xff) {
Transparency[i] = 0x00;
}
}
//设置调色板
FreeImage_SetTransparencyTable(hDIB8bpp, Transparency, 256);
//保存图像
FreeImage_Save(FIF_GIF, hDIB8bpp, save_fileName, 0);
FreeImage_Unload(hDIB24bpp);
FreeImage_Unload(hDIB8bpp);
FreeImage_Unload(res);
}
else
{
cout << "读取错误" << endl;
}
}
|