TNB Library
TnbCeBitmapImage.h
[詳解]
1#pragma once
11#ifndef _WIN32_WCE
12 #error TnbCeBitmapImage.h is only supported on Windows CE platforms.
13#endif // _WIN32_WCE
14
15
16
17#include <initguid.h>
18#include <imaging.h>
19#include "TnbBitmapImage.h"
20#include "TnbStr.h"
21
22
23
24//TNB Library
25namespace TNB{
26
27
28
43{
44public:
45
51 {
59 };
60
68 static HBITMAP LoadBitmap(LPCTSTR lpszFileName)
69 {
70 ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
71 CBitmapHandle bmp;
72 IImagingFactory* pFactory = NULL;
73 IImage* pImage = NULL;
74 IBitmapImage* pBitmap = NULL;
75 ImageInfo info;
76 HRESULT hr = ::CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER
77 , IID_IImagingFactory, reinterpret_cast<LPVOID*>(&pFactory));
78 if ( SUCCEEDED(hr) )
79 {
80 hr = pFactory->CreateImageFromFile(CUnicode(lpszFileName), &pImage);
81 }
82 if ( SUCCEEDED(hr) )
83 {
84 pImage->GetImageInfo(&info);
85 hr = pFactory->CreateBitmapFromImage(pImage, info.Width, info.Height
86 , PixelFormat24bppRGB, InterpolationHintDefault, &pBitmap);
87 }
88 if ( pImage != NULL )
89 {
90 pImage->Release();
91 pImage = NULL;
92 }
93 if ( pFactory != NULL )
94 {
95 pFactory->Release();
96 pFactory = NULL;
97 }
98 BitmapData lockdata;
99 if ( SUCCEEDED(hr) )
100 {
101 hr = pBitmap->LockBits(NULL, ImageLockModeRead, PixelFormat32bppARGB, &lockdata);
102 }
103 if ( SUCCEEDED(hr) )
104 {
105 SIZE sz = { info.Width, info.Height };
107 const RGBQUAD* pSrc = static_cast<const RGBQUAD*>(lockdata.Scan0);
108 loop ( y, info.Height )
109 {
110 RGBQUAD* pDst = raw[y];
111 loop ( x, info.Width )
112 {
113 *pDst++ = *pSrc++;
114 }
115 }
116 pBitmap->UnlockBits(&lockdata);
117 CBitmapImage bi;
118 bi.Set(raw);
119 bmp = bi.GetBitmapHandle();
120 }
121 if ( pBitmap != NULL )
122 {
123 pBitmap->Release();
124 pBitmap = NULL;
125 }
126 return bmp.Detach();
127 }
128
139 static bool SaveBitmap(LPCTSTR lpszFileName, HBITMAP hBmp, EBitmapFormat fmt = BMP, long quality = 80)
140 {
141 ::CoInitializeEx(NULL, COINIT_MULTITHREADED);
142 CBitmapHandle bb;
143 bb.SetClone(hBmp);
144 CBitmapImage bm = bb;
145 if ( bm.IsEmpty() )
146 {
147 return false;
148 }
149 ::DeleteFile(lpszFileName);
150 IImagingFactory* pFactory = NULL;
151 IImageEncoder* pEncoder = NULL;
152 IImageSink* pImageSink = NULL;
153 HRESULT hr = ::CoCreateInstance(CLSID_ImagingFactory, NULL, CLSCTX_INPROC_SERVER
154 , IID_IImagingFactory, reinterpret_cast<LPVOID*>(&pFactory));
155 if ( SUCCEEDED(hr) )
156 {
157 ImageCodecInfo* pCodecInfo = NULL;
158 UINT count;
159 hr = pFactory->GetInstalledEncoders(&count, &pCodecInfo);
160 if ( SUCCEEDED(hr) )
161 {
162 LPCWSTR lpFmtDes = NULL;
163 switch( fmt )
164 {
165 case BMP:
166 default: lpFmtDes = L"BMP"; break;
167 case GIF: lpFmtDes = L"GIF"; break;
168 case TIFF: lpFmtDes = L"TIFF"; break;
169 case PNG: lpFmtDes = L"PNG"; break;
170 case JPEG: lpFmtDes = L"JPEG"; break;
171 }
172 CLSID encoderClassId;
173 loop ( i, count )
174 {
175 if ( STRLIB::Compare(pCodecInfo[i].FormatDescription, lpFmtDes) == 0 )
176 {
177 encoderClassId = pCodecInfo[i].Clsid;
178 break;
179 }
180 }
181 ImageFormatJPEG;
182 hr = pFactory->CreateImageEncoderToFile(&encoderClassId, CUnicode(lpszFileName), &pEncoder);
183 free(pCodecInfo);
184 }
185 }
186 if ( SUCCEEDED(hr) && fmt == JPEG )
187 {
188 EncoderParameters ep;
189 ep.Count = 1;
190 ep.Parameter[0].Guid = EncoderQuality ;
191 ep.Parameter[0].Type = 4/*EncoderParameterValueTypeLong*/;
192 ep.Parameter[0].NumberOfValues = 1;
193 ep.Parameter[0].Value = &quality; //JPEGクオリティ:0〜100
194 hr = pEncoder->SetEncoderParameters(&ep);
195 }
196 if ( SUCCEEDED(hr) )
197 {
198 hr = pEncoder->GetEncodeSink(&pImageSink);
199 }
200 if ( SUCCEEDED(hr) )
201 {
202 ImageInfo imageInfo;
203 imageInfo.Width = bm.GetSize().cx;
204 imageInfo.Height = bm.GetSize().cy;
205 imageInfo.RawDataFormat = ImageFormatMemoryBMP;
206 imageInfo.Flags |= SinkFlagsTopDown | SinkFlagsFullWidth;
207 imageInfo.Xdpi = 96;
208 imageInfo.Ydpi = 96;
209 imageInfo.PixelFormat = PixelFormat32bppARGB;
210 imageInfo.Flags |= SinkFlagsHasAlpha;
211 hr = pImageSink->BeginSink(&imageInfo, NULL);
212 }
213 if ( SUCCEEDED(hr) )
214 {
215 ColorPalette pal = { 0 };
216 hr = pImageSink->SetPalette(&pal);
217 }
218 if ( SUCCEEDED(hr) )
219 {
220 BitmapData bmpData;
221 bmpData.Height = bm.GetSize().cy;
222 bmpData.Width = bm.GetSize().cx;
224 {
225 // 上下反転
226 size_t h = bm.GetSize().cy;
227 size_t w = bm.GetSize().cx;
228 CWorkMemT<RGBQUAD> buf(w);
229 size_t j = h - 1;
230 loop ( i, h / 2 )
231 {
232 MemCopy(buf.Ref(), raw[i], w);
233 MemCopy(raw[i], raw[j], w);
234 MemCopy(raw[j], buf.Ref(), w);
235 j--;
236 }
237 }
238 bmpData.Scan0 =raw.Refer();
239 bmpData.PixelFormat = PixelFormat32bppARGB;
240 UINT bitsPerLine = bm.GetSize().cx * 4 * 8;
241 UINT bitAlignment = sizeof(LONG) * 8;
242 UINT bitStride = bitAlignment * (bitsPerLine / bitAlignment);
243 if ( (bitsPerLine % bitAlignment) != 0 )
244 {
245 bitStride += bitAlignment;
246 }
247 bmpData.Stride = bitStride / 8;
248 RECT rect = { 0, 0, bmpData.Width, bmpData.Height };
249 hr = pImageSink->PushPixelData(&rect, &bmpData, TRUE);
250 pImageSink->EndSink(S_OK);
251 pImageSink->Release();
252 }
253 if ( pEncoder != NULL )
254 {
255 pEncoder->TerminateEncoder();
256 pEncoder->Release();
257 pEncoder = NULL;
258 }
259 if ( pFactory != NULL )
260 {
261 pFactory->Release();
262 pFactory = NULL;
263 }
264 bm.Detach();
265 return true;
266 }
267};
268
269
270
271};//TNB
272
273
ビットマップイメージ管理関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
文字列管理関係のヘッダ
HBITMAP型ハンドルハンドル
void SetClone(HBITMAP hBmp)
[設定] HBITMAPセット.
HBITMAP Detach(void)
[取得] デタッチ.
ビットマップ生データ管理クラス
RGBQUAD * Refer(void)
[取得] データ参照.
ビットマップイメージ管理クラス
CBitmapHandle Detach(void)
[設定] デタッチ.
bool IsEmpty(void) const
[確認] Empty状態確認.
const SIZE & GetSize(void) const
[取得] イメージサイズ取得.
bool Set(int cx, int cy, COLORREF color=CLR_INVALID)
[設定] イメージ設定.
CBitmapHandle GetBitmapHandle(void)
[取得] ビットマップハンドル取得
CRawData _deprecated CreateRawData(void) const
[作成] 生データ作成.
ビットマップ管理(CE専用)
static bool SaveBitmap(LPCTSTR lpszFileName, HBITMAP hBmp, EBitmapFormat fmt=BMP, long quality=80)
[作成] 画像ファイル作成.
EBitmapFormat
Saveフォーマット値
static HBITMAP LoadBitmap(LPCTSTR lpszFileName)
[作成] 画像ファイル読込み.
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
Definition: TnbStrLib.h:135
TNB::CStrT< WCHAR > CUnicode
UNICODE文字列クラス
Definition: TnbStr.h:1771
TNB Library
Definition: TnbDoxyTitle.txt:2
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー
Definition: TnbDef.h:376