bool CSprite::_CreateTexture(const CBitmap& _bitmap) { HRESULT hr; CGraphicsManager *pGraphicsManager = CGraphicsManager::GetInstance(); IDirect3DDevice9 *pD3Device = pGraphicsManager->GetDevice(); int iWidth = _bitmap.GetWidth(); int iHeight = _bitmap.GetHeight(); IDirect3DTexture9 *pSystemTexture = NULL; hr = pD3Device->CreateTexture((UINT)iWidth, (UINT)iHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pSystemTexture, NULL); if(FAILED(hr)) { LogError("Failed to create system texture"); return false; } D3DLOCKED_RECT lockedRect; memset(&lockedRect, 0, sizeof(lockedRect)); hr = pSystemTexture->LockRect(0, &lockedRect, NULL, 0); if(FAILED(hr)) { LogErrorHr("Failed to lock system texture rect", hr); return false; } DWORD *pTexLocation = (DWORD *)lockedRect.pBits; for(int iRowIndex = 0; iRowIndex < iHeight; iRowIndex++) { memcpy(pTexLocation, _bitmap.GetRow(iRowIndex), sizeof(DWORD) * iWidth); for(int iColumnIndex = 0; iColumnIndex < iWidth; iColumnIndex++) { DWORD dwPixel = pTexLocation[iColumnIndex]; if(dwPixel != g_dwBackgroundColor) { dwPixel |= 0xff000000l; pTexLocation[iColumnIndex] = dwPixel; } } BYTE *pTextureByteLocation = (BYTE *)pTexLocation; pTextureByteLocation += lockedRect.Pitch; pTexLocation = (DWORD *)pTextureByteLocation; } hr = pSystemTexture->UnlockRect(0); if(FAILED(hr)) { LogErrorHr("Failed to unlock system texture rect", hr); return false; } hr = pD3Device->CreateTexture(iWidth, iHeight, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pTexture, NULL); if(FAILED(hr)) { LogErrorHr("Failed to create video texture", hr); return false; } hr = pD3Device->UpdateTexture(pSystemTexture, m_pTexture); if(FAILED(hr)) { LogErrorHr("Failed to update video texture", hr); return false; } pSystemTexture->Release(); return true; }
bool parseCodeImage(const char* pszPath) { D3DXIMAGE_INFO info; HRESULT re = D3DXGetImageInfoFromFileA(pszPath, &info); if(re != S_OK) return false; IDirect3DTexture9 *pText = NULL; //D3DXCreateTextureFromFileA(g_pDevice, pszPath, &pText); //用没有 EX 的函数, 创建的纹理长宽会变成 2的 n次方 re = D3DXCreateTextureFromFileExA(g_pDevice, pszPath, info.Width, info.Height, info.MipLevels, 0/*D3DUSAGE_RENDERTARGET*/, info.Format, D3DPOOL_SYSTEMMEM, D3DX_FILTER_TRIANGLE,D3DX_FILTER_TRIANGLE,D3DCOLOR_ARGB(255,0,0,0), NULL, NULL, &pText); FILE* pFile = fopen("D:/hehe.txt", "w"); D3DLOCKED_RECT rc; pText->LockRect(0, &rc, NULL, 0); BYTE bR = 0, bG = 0, bB = 0; DWORD* pSrc = (DWORD*)rc.pBits; for (int i = 0; i < info.Height ; i++) { for (int j = 0; j < info.Width; j++) { bR = (*pSrc) >> 16; bG = (*pSrc) << 16 >> 24; bB = (*pSrc) & 0x000000ff; if (bR >= 205 && bG >= 205 && bB >= 205) { fprintf(pFile, "1"); } else { fprintf(pFile, "8"); } BYTE t = max( max(bR, bG), bB); *pSrc = t | t << 8 | t << 16; ++pSrc; } fprintf(pFile, "\r\n"); } pText->UnlockRect(0); // 保存灰度图 re = D3DXSaveTextureToFileA("D:/hehe.jpg", D3DXIFF_BMP, pText, NULL); pText->Release(); fclose(pFile); }
bool CreateFontTexture ( IDirect3DDevice9* pDev, DWORD usage, D3DPOOL pool, UINT ch, Font* pFont ) { HDC hdc = GetDC(NULL); HFONT hFont = ::CreateFont(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, _T("MS UI Gothic")); HFONT hOldFont = (HFONT)::SelectObject(hdc, hFont); HRESULT hr; int gm_w, gm_h; int fnt_x, fnt_y; BYTE *bmp_p = NULL; int bmp_w, bmp_h; //bmp_p { TEXTMETRIC tm; GLYPHMETRICS gm; MAT2 mat2 = { {0,1}, {0,0}, {0,0}, {0,1} }; GetTextMetrics(hdc, &tm); DWORD bufsize = GetGlyphOutline( hdc, ch, GGO_GRAY4_BITMAP, &gm, 0, NULL, &mat2); if (bufsize == GDI_ERROR) { bufsize = GetLastError(); goto fin; } bmp_p = (BYTE*)malloc(bufsize); DWORD r = GetGlyphOutline( hdc, ch, GGO_GRAY4_BITMAP, &gm, bufsize, bmp_p, &mat2); pFont->tm_max_w = tm.tmMaxCharWidth; pFont->tm_ave_w = tm.tmAveCharWidth; gm_w = gm.gmCellIncX; gm_h = tm.tmHeight; bmp_w = ((gm.gmBlackBoxX + 3) / 4) * 4; //4-align bmp_h = gm.gmBlackBoxY; fnt_x = gm.gmptGlyphOrigin.x; fnt_y = tm.tmAscent - gm.gmptGlyphOrigin.y; } IDirect3DTexture9* pTex; hr = pDev->CreateTexture(gm_w, gm_h, 1, usage, D3DFMT_A8R8G8B8, pool, &pTex, NULL); { D3DLOCKED_RECT rect; pTex->LockRect(0, &rect, NULL, D3DLOCK_DISCARD); FillMemory(rect.pBits, rect.Pitch * gm_h, 0); for (int y=0; y<bmp_h; ++y) { BYTE* p = ((BYTE*)rect.pBits) + rect.Pitch * (fnt_y + y) + fnt_x * 4; for (int x=0; x<bmp_w; ++x) { DWORD trans = ((255 * bmp_p[x+y*bmp_w]) /16)&0xFF; DWORD color = 0x00FFFFFF | (trans << 24); memcpy(p, &color, 4); p += 4; } } pTex->UnlockRect(0); } pFont->pTex = pTex; pFont->gm_w = gm_w; pFont->gm_h = gm_h; pFont->fnt_x = fnt_x; pFont->fnt_y = fnt_y; fin: if (bmp_p) { free(bmp_p); } SelectObject(hdc, hOldFont); return true; }
void DeprecatedTextureHostSystemMemD3D9::UpdateImpl(const SurfaceDescriptor& aImage, nsIntRegion *aRegion, nsIntPoint *aOffset) { MOZ_ASSERT(aImage.type() == SurfaceDescriptor::TSurfaceDescriptorD3D9); MOZ_ASSERT(mCompositor, "Must have compositor to update."); if (!mCompositor->device()) { return; } IDirect3DTexture9* texture = reinterpret_cast<IDirect3DTexture9*>(aImage.get_SurfaceDescriptorD3D9().texture()); if (!texture) { Reset(); return; } D3DSURFACE_DESC desc; texture->GetLevelDesc(0, &desc); HRESULT hr = texture->GetLevelDesc(0, &desc); if (FAILED(hr)) { Reset(); return; } mSize.width = desc.Width; mSize.height = desc.Height; _D3DFORMAT format = desc.Format; uint32_t bpp = 0; switch (format) { case D3DFMT_X8R8G8B8: mFormat = SurfaceFormat::B8G8R8X8; bpp = 4; break; case D3DFMT_A8R8G8B8: mFormat = SurfaceFormat::B8G8R8A8; bpp = 4; break; case D3DFMT_A8: mFormat = SurfaceFormat::A8; bpp = 1; break; default: NS_ERROR("Bad image format"); } int32_t maxSize = mCompositor->GetMaxTextureSize(); if (mSize.width <= maxSize && mSize.height <= maxSize) { mIsTiled = false; mTexture = TextureToTexture(gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(), texture, mSize, format); if (!mTexture) { NS_WARNING("Could not upload texture"); Reset(); return; } } else { mIsTiled = true; uint32_t tileCount = GetRequiredTilesD3D9(mSize.width, maxSize) * GetRequiredTilesD3D9(mSize.height, maxSize); mTileTextures.resize(tileCount); for (uint32_t i = 0; i < tileCount; i++) { IntRect tileRect = GetTileRect(i); RECT d3dTileRect; d3dTileRect.left = tileRect.x; d3dTileRect.top = tileRect.y; d3dTileRect.right = tileRect.XMost(); d3dTileRect.bottom = tileRect.YMost(); D3DLOCKED_RECT lockedRect; texture->LockRect(0, &lockedRect, &d3dTileRect, 0); mTileTextures[i] = DataToTexture(gfxWindowsPlatform::GetPlatform()->GetD3D9DeviceManager(), reinterpret_cast<unsigned char*>(lockedRect.pBits), lockedRect.Pitch, tileRect.Size(), format, bpp); texture->UnlockRect(0); if (!mTileTextures[i]) { NS_WARNING("Could not upload texture"); Reset(); return; } } } }
void RenderState::init(IDirect3DDevice9* dev) { _dev = dev; D3DDEVICE_CREATION_PARAMETERS params; HRESULT hr = dev->GetCreationParameters(¶ms); if (FAILED(hr)) { MM_LOG_INFO(format("Failed to obtain device creation parameters")); } else { _focusWindow = params.hFocusWindow; } // create "selected" texture WCHAR* dataPath = NULL; if (Interop::OK()) { IDirect3DTexture9 * tex; int width = 256; int height = 256; HRESULT hr = dev->CreateTexture(width, height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &tex, 0); if (FAILED(hr)) { MM_LOG_INFO("Failed to create 'selection' texture"); } else { _selectionTexture = tex; D3DLOCKED_RECT rect; hr = tex->LockRect(0, &rect, 0, D3DLOCK_DISCARD); if (FAILED(hr)) { MM_LOG_INFO("Failed to lock 'selection' texture"); } else { unsigned char* dest = static_cast<unsigned char*>(rect.pBits); // fill it with a lovely shade of green Uint32 numEls = width * height; for (Uint32 i = 0; i < numEls; ++i) { Uint32* d = (Uint32*)(dest + (i*sizeof(Uint32))); *d = 0xFF00FF00; } //MM_LOG_INFO("filled selection texture"); tex->UnlockRect(0); } } } // Set key bindings. Input also assumes that CONTROL modifier is required for these as well. // TODO: should push this out to conf file eventually so that they can be customized without rebuild // If you change these, be sure to change LocStrings/ProfileText in MMLaunch! _punctKeyMap[DIK_BACKSLASH] = [&]() { this->loadMods(); }; _punctKeyMap[DIK_RBRACKET] = [&]() { this->toggleShowModMesh(); }; _punctKeyMap[DIK_SEMICOLON] = [&]() { this->clearTextureLists(); }; _punctKeyMap[DIK_COMMA] = [&]() { this->selectNextTexture(); }; _punctKeyMap[DIK_PERIOD] = [&]() { this->selectPrevTexture(); }; _punctKeyMap[DIK_SLASH] = [&]() { this->requestSnap(); }; _punctKeyMap[DIK_MINUS] = [&]() { this->loadEverything(); }; // If you change these, be sure to change LocStrings/ProfileText in MMLaunch! _fKeyMap[DIK_F1] = [&]() { this->loadMods(); }; _fKeyMap[DIK_F2] = [&]() { this->toggleShowModMesh(); }; _fKeyMap[DIK_F6] = [&]() { this->clearTextureLists(); }; _fKeyMap[DIK_F3] = [&]() { this->selectNextTexture(); }; _fKeyMap[DIK_F4] = [&]() { this->selectPrevTexture(); }; _fKeyMap[DIK_F7] = [&]() { this->requestSnap(); }; _fKeyMap[DIK_F10] = [&]() { this->loadEverything(); }; _pCurrentKeyMap = &_fKeyMap; if (Interop::OK()) { if (Interop::Conf().LoadModsOnStart) { loadEverything(); toggleShowModMesh(); } else { loadManagedAssembly(); } } _initted = true; }
IDirect3DTexture9 * Texture_DX9::build_from_Image(const Image &image) { Video_DX9 &vdx = dynamic_cast<Video_DX9 &>(get_Video()); IDirect3DTexture9 * ppTexture; D3DFORMAT format; switch(image.color_space()) { case Image::Luminance: format = D3DFMT_L8; break; case Image::Luminance_Alpha: format = D3DFMT_A8B8G8R8; break; case Image::RGB: format = D3DFMT_A8B8G8R8; break; case Image::RGBA: format = D3DFMT_A8B8G8R8; break; default: format = D3DFMT_UNKNOWN; abort(); } set_sampler_states(); if(FAILED(Video_DX9::D3DXCreateTexture()(vdx.get_d3d_device(), UINT(image.width()), UINT(image.height()), D3DX_DEFAULT, 0, format, D3DPOOL_MANAGED, &ppTexture))) throw Texture_Init_Failure(); D3DLOCKED_RECT rect; if(FAILED(ppTexture->LockRect(0, &rect, 0, 0))) { ppTexture->Release(); throw Texture_Init_Failure(); } if(image.color_space() == Image::Luminance) { memcpy(rect.pBits, image.get_data(), image.width() * image.height()); } else if(image.color_space() == Image::Luminance_Alpha) { Uint8 * dest = reinterpret_cast<Uint8 *>(rect.pBits); const Uint8 * src = image.get_data(); for(Uint8 * const dest_end = dest + image.width() * image.height() * 4; dest != dest_end; dest += 4, src += 2) { dest[0] = src[0]; dest[1] = src[0]; dest[2] = src[0]; dest[3] = src[1]; } } else if(image.color_space() == Image::RGB) { Uint8 * dest = reinterpret_cast<Uint8 *>(rect.pBits); const Uint8 * src = image.get_data(); for(Uint8 * const dest_end = dest + image.width() * image.height() * 4; dest != dest_end; dest += 4, src += 3) { dest[0] = src[2]; dest[1] = src[1]; dest[2] = src[0]; dest[3] = 0xFF; } } else /*if(image.color_space() == Image::RGBA)*/ { Uint8 * dest = reinterpret_cast<Uint8 *>(rect.pBits); const Uint8 * src = image.get_data(); for(Uint8 * const dest_end = dest + image.width() * image.height() * 4; dest != dest_end; dest += 4, src += 4) { dest[0] = src[2]; dest[1] = src[1]; dest[2] = src[0]; dest[3] = src[3]; } } if(FAILED(ppTexture->UnlockRect(0))) { ppTexture->Release(); throw Texture_Init_Failure(); } if(FAILED(Video_DX9::D3DXFilterTexture()(ppTexture, 0, D3DX_DEFAULT, D3DX_DEFAULT))) { ppTexture->Release(); throw Texture_Init_Failure(); } return ppTexture; }
HRESULT D3DBufImgOps_EnableLookupOp(D3DContext *d3dc, jboolean nonPremult, jboolean shortData, jint numBands, jint bandLength, jint offset, void *tableValues) { HRESULT res; IDirect3DDevice9 *pd3dDevice; D3DResource *pLutTexRes; IDirect3DTexture9 *pLutTex; int bytesPerElem = (shortData ? 2 : 1); jfloat foffsets[4]; void *bands[4]; int i; jint flags = 0; J2dTraceLn4(J2D_TRACE_INFO, "D3DBufImgOps_EnableLookupOp: short=%d num=%d len=%d off=%d", shortData, numBands, bandLength, offset); RETURN_STATUS_IF_NULL(d3dc, E_FAIL); d3dc->UpdateState(STATE_CHANGE); // choose the appropriate shader, depending on the source image // and the number of bands involved if (numBands != 4) { flags |= LOOKUP_USE_SRC_ALPHA; } if (nonPremult) { flags |= LOOKUP_NON_PREMULT; } // locate/enable the shader program for the given flags res = d3dc->EnableLookupProgram(flags); RETURN_STATUS_IF_FAILED(res); // update the "uniform" offset value for (i = 0; i < 4; i++) { foffsets[i] = offset / 255.0f; } pd3dDevice = d3dc->Get3DDevice(); pd3dDevice->SetPixelShaderConstantF(0, foffsets, 1); res = d3dc->GetResourceManager()->GetLookupOpLutTexture(&pLutTexRes); RETURN_STATUS_IF_FAILED(res); pLutTex = pLutTexRes->GetTexture(); // update the lookup table with the user-provided values if (numBands == 1) { // replicate the single band for R/G/B; alpha band is unused for (i = 0; i < 3; i++) { bands[i] = tableValues; } bands[3] = NULL; } else if (numBands == 3) { // user supplied band for each of R/G/B; alpha band is unused for (i = 0; i < 3; i++) { bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); } bands[3] = NULL; } else if (numBands == 4) { // user supplied band for each of R/G/B/A for (i = 0; i < 4; i++) { bands[i] = PtrAddBytes(tableValues, i*bandLength*bytesPerElem); } } // upload the bands one row at a time into our lookup table texture D3DLOCKED_RECT lockedRect; res = pLutTex->LockRect(0, &lockedRect, NULL, D3DLOCK_NOSYSLOCK); RETURN_STATUS_IF_FAILED(res); jushort *pBase = (jushort*)lockedRect.pBits; for (i = 0; i < 4; i++) { jushort *pDst; if (bands[i] == NULL) { continue; } pDst = pBase + (i * 256); if (shortData) { memcpy(pDst, bands[i], bandLength*sizeof(jushort)); } else { int j; jubyte *pSrc = (jubyte *)bands[i]; for (j = 0; j < bandLength; j++) { pDst[j] = (jushort)(pSrc[j] << 8); } } } pLutTex->UnlockRect(0); // bind the lookup table to texture unit 1 and enable texturing res = d3dc->SetTexture(pLutTex, 1); pd3dDevice->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); pd3dDevice->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); pd3dDevice->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); pd3dDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); return res; }
HRESULT Loader::LoadElevations( float **ppImageData, int* nSize, const char * szFilename, bool swapVertical /*= true*/) { float* pImageData = NULL; CParaFile cFile; cFile.OpenAssetFile(szFilename, true, ParaTerrain::Settings::GetInstance()->GetMediaPath()); if(cFile.isEof()) return E_FAIL; int elevWidth=0; int elevHeight=0; if(strcmp(szFilename+strlen(szFilename)-4, ".raw") == 0) { /// Load from raw elevation elevWidth = (int)sqrt((float)(((int)cFile.getSize()/4))); elevHeight = elevWidth; /// just use the file buffer as the image buffer pImageData = (float*)cFile.getBuffer(); cFile.GiveupBufferOwnership(); } else { #ifdef USE_DIRECTX_RENDERER D3DSURFACE_DESC desc; IDirect3DTexture9* pTexture = NULL; HRESULT hr; /// Create a D3DFMT_X8R8G8B8 texture -- use D3DPOOL_SCRATCH to // ensure that this will succeeded independent of the device hr = D3DXCreateTextureFromFileInMemoryEx( CGlobals::GetRenderDevice(), cFile.getBuffer(), (int)cFile.getSize(), 0, 0, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_SCRATCH, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0, NULL, NULL, &pTexture ); if( FAILED(hr) ) return hr; pTexture->GetLevelDesc( 0, &desc ); elevWidth = desc.Width; elevHeight = desc.Height; // create an array of floats to store the values of the bmp pImageData = new float[elevWidth * elevHeight]; if( pImageData == NULL ) { SAFE_RELEASE( pTexture ); return E_OUTOFMEMORY; } //ZeroMemory( pImageData, elevWidth * elevHeight * sizeof(FLOAT) ); D3DLOCKED_RECT lockedRect; hr = pTexture->LockRect( 0, &lockedRect, NULL, D3DLOCK_READONLY ); float fMin=1.0f,fMax=-1.0f; if( SUCCEEDED(hr) ) { DWORD* pBuffer = (DWORD*) lockedRect.pBits; for( DWORD iY=0; iY<desc.Height; iY++ ) { pBuffer = (DWORD*)((BYTE*)lockedRect.pBits + lockedRect.Pitch * iY); for( DWORD iX=0; iX<desc.Width; iX++ ) { LinearColor color((DWORD)(*pBuffer)); float fValue = (color.r+color.g+color.b)/3.0f-0.5f; if (fValue<fMin) fMin = fValue; if (fValue>fMax) fMax = fValue; if (swapVertical) { // Invert Y, so it appears the same as the bmp pImageData[ (desc.Height-1-iY)*elevWidth + iX] = fValue; } else { pImageData[ iY*elevWidth + iX] = fValue; } pBuffer++; } } pTexture->UnlockRect( 0 ); /// Normalize all values between 0.0f and 1.0f /*float fHeight = (fMax-fMin); for( DWORD iY=0; iY<desc.Height; iY++ ) { for( DWORD iX=0; iX<desc.Width; iX++ ) { pImageData[ iY*elevWidth + iX] = (pImageData[ iY*elevWidth + iX]-fMin)/fHeight; } }*/ } SAFE_RELEASE( pTexture ); #else return E_FAIL; #endif } // TODO: re-enable this when you are ready. const bool FORCE_256_mesh = false; if(elevWidth == 129 && FORCE_256_mesh) { // make it 257. int elevWidthDest = 257; float * pImageDataDest = new float[elevWidthDest * elevWidthDest]; for (int i=0; i<elevWidthDest; i++) { for (int j=0; j<elevWidthDest; j++) { int rI = i%2; int ltX = i/2; int rJ = j%2; int ltY = j/2; if(rI == 0) { if(rJ == 0) { pImageDataDest[i*elevWidthDest+j] = pImageData[ltX*elevWidth+ltY]; } else { pImageDataDest[i*elevWidthDest+j] = (pImageData[ltX*elevWidth+ltY+1] + pImageData[ltX*elevWidth+ltY]) * 0.5f; } } else { if(rJ == 0) { pImageDataDest[i*elevWidthDest+j] = (pImageData[(ltX+1)*elevWidth+ltY] + pImageData[ltX*elevWidth+ltY])*0.5f; } else { pImageDataDest[i*elevWidthDest+j] = (pImageData[(ltX+1)*elevWidth+ltY]+pImageData[ltX*elevWidth+ltY]+pImageData[ltX*elevWidth+ltY+1]+pImageData[(ltX+1)*elevWidth+ltY+1])*0.25f; } } } } SAFE_DELETE(pImageData); pImageData = pImageDataDest; elevWidth = elevWidthDest; } *nSize = elevWidth; *ppImageData = pImageData; return S_OK; }