SpriteSheet::SpriteSheet(wchar_t* filename, Graphics* gfx) { this->gfx = gfx; bmp = NULL; HRESULT hr; //create a wic factory IWICImagingFactory *wicFactory = NULL; hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&wicFactory); //create a decoder IWICBitmapDecoder *wicDecoder = NULL; hr = wicFactory->CreateDecoderFromFilename( filename, // THE FILE NAME NULL, // the preferred vendor GENERIC_READ, // we're reading the file, not writing WICDecodeMetadataCacheOnLoad, &wicDecoder); // read a frame from the image IWICBitmapFrameDecode* wicFrame = NULL; hr = wicDecoder->GetFrame(0, &wicFrame); // create a converter IWICFormatConverter *wicConverter = NULL; hr = wicFactory->CreateFormatConverter(&wicConverter); // setup the converter hr = wicConverter->Initialize( wicFrame, // frame GUID_WICPixelFormat32bppPBGRA, // pixel format WICBitmapDitherTypeNone, // irrelevant NULL, // no palette needed, irrlevant 0.0, // alpha transparency % irrelevant WICBitmapPaletteTypeCustom // irrelevant ); // use the converter to create an D2D1Bitmap /// ID2D1Bitmap* bmp; // this will be a member variable hr = gfx->GetRenderTarget()->CreateBitmapFromWicBitmap( wicConverter, // converter NULL, // D2D1_BITMAP_PROPERIES &bmp // destiatnion D2D1 bitmap ); if (wicFactory) wicFactory->Release(); if (wicDecoder) wicDecoder->Release(); if (wicConverter) wicConverter->Release(); if (wicFrame) wicFrame->Release(); }
void GL::Image::load(const unsigned char *buf, size_t bufSize) { if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK) { // bad! return; } IStream *stream = SHCreateMemStream((const BYTE*)buf, (UINT)bufSize); if (stream != NULL) { IWICImagingFactory *pFactory; if (CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pFactory) == S_OK) { IWICBitmapDecoder *pDecoder; if (pFactory->CreateDecoderFromStream(stream, &CLSID_WICPngDecoder, WICDecodeMetadataCacheOnDemand, &pDecoder) == S_OK) { IWICBitmapFrameDecode *frame; if (pDecoder->GetFrame(0, &frame) == S_OK) { UINT w, h; if (frame->GetSize(&w, &h) == S_OK) { width_ = w; height_ = h; } IWICFormatConverter *formatConverter; if (pFactory->CreateFormatConverter(&formatConverter) == S_OK) { if (formatConverter->Initialize(frame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.0, WICBitmapPaletteTypeCustom) == S_OK) { unsigned char *pixels = new unsigned char[w * h * 4]; if (formatConverter->CopyPixels(0, w * 4, w * h * 4, pixels) == S_OK) { loadTextureData_(pixels); } delete[] pixels; } formatConverter->Release(); } } pDecoder->Release(); } pFactory->Release(); } stream->Release(); } CoUninitialize(); }
static void BuildSkinIcons(TEnumData *lParam) { IWICImagingFactory *factory = (bIsVistaPlus) ? ARGB_GetWorker() : NULL; TSlotIPC *pct = lParam->ipch->NewIconsBegin; TShellExt *Self = lParam->Self; while (pct != NULL) { if (pct->cbSize != sizeof(TSlotIPC) || pct->fType != REQUEST_NEWICONS) break; TSlotProtoIcons *p = (TSlotProtoIcons*)(PBYTE(pct) + sizeof(TSlotIPC)); Self->ProtoIcons = (TSlotProtoIcons*)realloc(Self->ProtoIcons, (Self->ProtoIconsCount + 1) * sizeof(TSlotProtoIcons)); TSlotProtoIcons *d = &Self->ProtoIcons[Self->ProtoIconsCount]; memmove(d, p, sizeof(TSlotProtoIcons)); // if using Vista (or later), clone all the icons into bitmaps and keep these around, // if using anything older, just use the default code, the bitmaps (and/or icons) will be freed // with the shell object. for (int j = 0; j < 10; j++) { if (bIsVistaPlus) { d->hBitmaps[j] = ARGB_BitmapFromIcon(factory, Self->hMemDC, p->hIcons[j]); d->hIcons[j] = NULL; } else { d->hBitmaps[j] = NULL; d->hIcons[j] = CopyIcon(p->hIcons[j]); } } Self->ProtoIconsCount++; pct = pct->Next; } if (factory) factory->Release(); }
//@https://msdn.microsoft.com/de-de/library/windows/desktop/dd756686(v=vs.85).aspx HRESULT BitmapDecoder::LoadBitmapFromFile(ID2D1RenderTarget * pRenderTarget, PCWSTR uri, ID2D1Bitmap ** ppBitmap) { IWICImagingFactory* pFactory; HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&pFactory ); //IWICStream *pStream = NULL; //? //IWICBitmapScaler *pScaler = NULL; //? IWICBitmapDecoder *pDecoder = NULL; if (SUCCEEDED(hr)) { hr = pFactory->CreateDecoderFromFilename(uri, NULL, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder); } IWICBitmapFrameDecode *pSource = NULL; if (SUCCEEDED(hr)) { hr = pDecoder->GetFrame(0, &pSource); } IWICFormatConverter *pConverter = NULL; if (SUCCEEDED(hr)) { hr = pFactory->CreateFormatConverter(&pConverter); } if (SUCCEEDED(hr)) { hr = pConverter->Initialize( pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.f, WICBitmapPaletteTypeMedianCut ); } if (SUCCEEDED(hr)) { pRenderTarget->CreateBitmapFromWicBitmap(pConverter, NULL, ppBitmap); } if (pDecoder != nullptr) { pDecoder->Release();//SafeRelease(&pDecoder); } if (pSource != nullptr) { pSource->Release(); } //SafeRelease(&pSource); // pStream->Release();//SafeRelease(&pStream); if(pConverter!= nullptr) { pConverter->Release(); } //SafeRelease(&pConverter); // pScaler->Release();//SafeRelease(&pScaler); if (pFactory != nullptr) { pFactory->Release(); } return hr; }
HRESULT CMainWindow::DecodeImageFromThumbCache( IShellItem *pShellItem, ID2D1Bitmap **ppBitmap ) { const UINT THUMB_SIZE = 256; // Read the bitmap from the thumbnail cache IThumbnailCache *pThumbCache; HRESULT hr = CoCreateInstance( CLSID_LocalThumbnailCache, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pThumbCache) ); if (SUCCEEDED(hr)) { ISharedBitmap *pBitmap; hr = pThumbCache->GetThumbnail( pShellItem, THUMB_SIZE, WTS_SCALETOREQUESTEDSIZE, &pBitmap, NULL, NULL ); if (SUCCEEDED(hr)) { HBITMAP hBitmap; hr = pBitmap->GetSharedBitmap( &hBitmap ); if (SUCCEEDED(hr)) { // Create a WIC bitmap from the shared bitmap IWICImagingFactory *pFactory; hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory) ); if (SUCCEEDED(hr)) { IWICBitmap *pWICBitmap; hr = pFactory->CreateBitmapFromHBITMAP( hBitmap, NULL, WICBitmapIgnoreAlpha, &pWICBitmap ); if (SUCCEEDED(hr)) { // Create a D2D bitmap from the WIC bitmap hr = m_pRenderTarget->CreateBitmapFromWicBitmap( pWICBitmap, NULL, ppBitmap ); pWICBitmap->Release(); } pFactory->Release(); } } pBitmap->Release(); } pThumbCache->Release(); } return hr; }
HRESULT YCbCrPixelFormatConverter::ConvertRgbToYCbCr( /* [in] */ const WICRect *prc, /* [in] */ UINT cbStride, /* [in] */ UINT cbPixelsSize, /* [out] */ BYTE *pbPixels) { HRESULT result = S_OK; //Sanity check WICPixelFormatGUID srcPixelFormat; bitmapSource->GetPixelFormat(&srcPixelFormat); if (srcPixelFormat != GUID_WICPixelFormat32bppBGRA) { result = E_UNEXPECTED; return result; } IWICImagingFactory *codecFactory = NULL; IWICFormatConverter *formatConverter = NULL; if (SUCCEEDED(result)) { result = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*) &codecFactory); } if (SUCCEEDED(result)) { result = codecFactory->CreateFormatConverter(&formatConverter); } if (SUCCEEDED(result)) { //We will convert to 24RGB first, since it is easier to convert to YCbCr from there result = formatConverter->Initialize(bitmapSource, GUID_WICPixelFormat24bppBGR, WICBitmapDitherTypeSolid, NULL, 1.0, WICBitmapPaletteTypeFixedWebPalette); } if (SUCCEEDED(result)) { result = formatConverter->CopyPixels(prc, cbStride, cbPixelsSize, pbPixels); } //Since the two formats have same number of bytes, we will do an inplace conversion UINT width, height; if (prc == NULL) { if (SUCCEEDED(result)) { result = bitmapSource->GetSize(&width, &height); } } else { width = prc->Width; height = prc->Height; } if (SUCCEEDED(result)) { //Loop on the data and do the conversion BYTE *curPos = NULL; curPos = pbPixels; for (int i = 0 ; i < height; i++) { for (int j = 0; j < width; j++) { BYTE R, G ,B; BYTE Y, Cb, Cr; B = *curPos; G = *(curPos+1); R = *(curPos+2); //Do the maths Y = Clamp(0, 255, (0.257*R) + (0.504*G) + (0.098*B) + 16); Cb = Clamp(0, 255, (-0.148*R) - (0.291*G) + (0.439*B) + 128); Cr = Clamp(0, 255, (0.439*R) - (0.368*G) - (0.071*B) + 128); *curPos = Cr; *(curPos+1) = Cb; *(curPos+2) = Y; //Advance to next pixel curPos += 3; } curPos += (cbStride - (width * 3)); //Fast forward remaining part of the stride } } if (formatConverter) { formatConverter->Release(); } if (codecFactory) { codecFactory->Release(); } return result; }
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd) { xxd.SetScreenResolution(ScreenWidth, ScreenHeight); WNDCLASSEX pencere; ZeroMemory(&pencere, sizeof(WNDCLASSEX)); pencere.cbClsExtra = NULL; pencere.cbSize = sizeof(WNDCLASSEX); pencere.cbWndExtra = NULL; pencere.hCursor = LoadCursor(NULL, IDC_ARROW); pencere.hIcon = NULL; pencere.hIconSm = NULL; pencere.hInstance = hInst; pencere.lpfnWndProc = (WNDPROC)WinProc; pencere.lpszClassName = "Pencere"; pencere.lpszMenuName = NULL; pencere.style = CS_HREDRAW | CS_VREDRAW; RegisterClassEx(&pencere); ID2D1Factory* directx = NULL; D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,&directx); HWND hWnd = NULL; hWnd = CreateWindowEx(NULL, "Pencere", "Direct2D",WS_POPUP | WS_EX_TOPMOST, 0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInst, NULL); ShowWindow(hWnd, nShowCmd); /*D2D1Rect*/ RECT rect; GetClientRect(hWnd, &rect); ID2D1HwndRenderTarget* RenderTarget = NULL; directx->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd,D2D1::SizeU(rect.right - rect.left, rect.bottom - rect.top)), &RenderTarget); /*D2D1Rect*/ /* D2D1Brush */ /*ID2D1SolidColorBrush* Brush = NULL; RenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &Brush);*/ /* D2D1Brush */ MSG msg; ZeroMemory(&msg, sizeof(MSG)); /*D2D1_ELLIPSE ellipse = D2D1::Ellipse( D2D1::Point2F(100.f, 100.f), 50.f, 50.f );*/ /*POINT pts; pts.x = (ScreenWidth/2) - 25; float gravity = 1.0f; float y = 20; int alo = 0; pts.y = (ScreenHeight/2) - 80;*/ ID2D1Bitmap* Bitmap = NULL; IWICImagingFactory* ImagingFactory = NULL; CoInitializeEx(NULL, COINIT_MULTITHREADED); CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&ImagingFactory); /*lb.LoadBitmapFromFile(RenderTarget, ImagingFactory, L"Oyuncu/Dokular/Aylak/ön.png", &Bitmap); D2D1_SIZE_F size = Bitmap->GetSize(); D2D1_POINT_2F upperLeftCorner;*/ btn.InitFactory(ImagingFactory); btn.InitTarget(RenderTarget); btn.setPos(100, 100); btn.getImages(); btn.InitText(L"Buton Bura Oglim"); Bush trn; trn.InitGadgets(RenderTarget,ImagingFactory); trn.InitImage(); trn.CalcThat(); trn.InitThat(); Player plyr; plyr.InitFactories(ImagingFactory,RenderTarget); plyr.InitIdleImages(); plyr.InitMoveImages(); DWORD baslangic_noktasi; float deltaTime, oldTime = 0; while (TRUE){ baslangic_noktasi = GetTickCount(); deltaTime = baslangic_noktasi - oldTime; oldTime = baslangic_noktasi; if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } //KEY if (TUS(VK_SPACE)) { //alo = 1; } if (TUS(VK_LEFT)) { tp.x += 2; //pts.x -= 2; } if (TUS(VK_RIGHT)) { tp.x -= 2; //pts.x += 2; } if (TUS(VK_UP)) { tp.y += 2; //pts.y -= 2; } if (TUS(VK_DOWN)) { tp.y -= 2; //pts.y += 2; } plyr.HandleKeysForIdle(); plyr.HandleKeysForMove(deltaTime); /*if (alo == 1) { y -= gravity; pts.y -= y; if (y == -19) { alo = 0; y = 20.f; } }*/ /*if (clps.x != 0 && clps.y!= 0) { text.setPos(clps.x, clps.y); }*/ /*upperLeftCorner = D2D1::Point2F(pts.x, pts.y);*/ //text.setFont(L"Comic Sans MS"); //LOOP RenderTarget->BeginDraw(); RenderTarget->Clear(D2D1::ColorF(0.25f,0.80f,0.22f,1.f)); /*Terrain*/ trn.drawBush(); /*Terrain*/ //RenderTarget->DrawRectangle(D2D1::RectF(100,100,200,300), Brush); //RenderTarget->DrawEllipse(ellipse, Brush, 2.f); RenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(0 + tp.x,0 + tp.y)); /*RenderTarget->DrawBitmap( Bitmap, D2D1::RectF( upperLeftCorner.x, upperLeftCorner.y, upperLeftCorner.x + 50, upperLeftCorner.y + 160), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR );*/ //text.drawatext(50,L"Merhaba",RenderTarget,Brush); plyr.Render(); btn.RenderIt(cups, clk, tp); RenderTarget->EndDraw(); /*if (TUS(VK_ESCAPE)) { break; PostMessage(hWnd, WM_DESTROY, 0, 0); }*/ // } directx->Release(); RenderTarget->Release(); /*Brush->Release();*/ Bitmap->Release(); ImagingFactory->Release(); xxd.RestoreScreenResolution(); return 0; }
void LoadJPG(wchar_t *pwszJpg, HWND hwnd) { IWICImagingFactory *pFactory = NULL; HRESULT hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory)); if (SUCCEEDED(hr)) { IWICBitmapDecoder *pDecoder = NULL; hr = pFactory->CreateDecoderFromFilename(pwszJpg, NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &pDecoder); if (SUCCEEDED(hr)) { IWICBitmapFrameDecode *pIDecoderFrame = NULL; hr = pDecoder->GetFrame(0, &pIDecoderFrame); if (SUCCEEDED(hr)) { IWICFormatConverter *pFC = NULL; hr = pFactory->CreateFormatConverter(&pFC); hr = pFC->Initialize(pIDecoderFrame, GUID_WICPixelFormat24bppBGR, WICBitmapDitherTypeNone, NULL, 0.0, WICBitmapPaletteTypeCustom); const int nBytesPixel = 3; // GUID_WICPixelFormat24bppBGR 每像素3字节(24bits) BITMAPINFO bmpInfo; ZeroMemory(&bmpInfo, sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); hr = pIDecoderFrame->GetSize((UINT*)&bmpInfo.bmiHeader.biWidth, (UINT*)&bmpInfo.bmiHeader.biHeight); bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biBitCount = 8 * nBytesPixel; bmpInfo.bmiHeader.biCompression = BI_RGB; BYTE *pBuf = NULL; HDC hdc = GetDC(hwnd); bmpInfo.bmiHeader.biHeight *= -1; // BMP 方向调整 HBITMAP hBmp = CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, (void**)&pBuf, NULL, 0); bmpInfo.bmiHeader.biHeight *= -1; // 计算扫描线 unsigned int cbStride = nBytesPixel * bmpInfo.bmiHeader.biWidth; unsigned int total = cbStride*bmpInfo.bmiHeader.biHeight; hr = pFC->CopyPixels(NULL, cbStride, total, pBuf); // HSL色彩空间变换 DirectX::XMVECTORF32 colorTransofrom = { 1.0f, 1.0f, 1.0f, 1.0f }; if (SUCCEEDED(hr)) { for (int i = 0; i < bmpInfo.bmiHeader.biHeight; i++) { for (int j = 0; j < bmpInfo.bmiHeader.biWidth; j++) { BYTE *pColor = pBuf + cbStride*i + j * nBytesPixel; DirectX::XMVECTOR colorM = DirectX::PackedVector::XMLoadUByteN4((DirectX::PackedVector::XMUBYTEN4*)pColor); colorM = DirectX::XMColorRGBToHSL(colorM); colorM = DirectX::XMColorModulate(colorM, colorTransofrom); colorM = DirectX::XMColorHSLToRGB(colorM); DirectX::PackedVector::XMStoreUByteN4((DirectX::PackedVector::XMUBYTEN4*)pColor, colorM); SetPixel(hdc, j, i, RGB(pColor[2], pColor[1], pColor[0])); } } } ReleaseDC(hwnd, hdc); if (SUCCEEDED(hr)) { MakeMemDC(hBmp, hwnd); InvalidateRect(hwnd, NULL, TRUE); } hr = pFC->Release(); pIDecoderFrame->Release(); } pDecoder->Release(); } pFactory->Release(); } }