void WindowRendererSupportGLWin32::setPixelFormat(const DeviceContext* deviceContext, const PixelFormat& pixelFormat) const { HDC hdc = static_cast<const DeviceContextWin32*>(deviceContext)->getHDC(); ushort colourBits = pixelFormat.getRedBits() + pixelFormat.getGreenBits() + pixelFormat.getBlueBits(); ushort alphaBits = pixelFormat.getAlphaBits(); ushort depthBits = 24; ushort stencilBits = 8; ushort samples = 4; PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = (BYTE)colourBits; pfd.cAlphaBits = (BYTE)alphaBits; pfd.cDepthBits = (BYTE)depthBits; pfd.cStencilBits = (BYTE)stencilBits; int format = 0; if(mHasMultisample) { vector<int> attribList; attribList.push_back(WGL_DRAW_TO_WINDOW_ARB); attribList.push_back(GL_TRUE); attribList.push_back(WGL_SUPPORT_OPENGL_ARB); attribList.push_back(GL_TRUE); attribList.push_back(WGL_DOUBLE_BUFFER_ARB); attribList.push_back(GL_TRUE); attribList.push_back(WGL_SAMPLE_BUFFERS_ARB); attribList.push_back(GL_TRUE); attribList.push_back(WGL_ACCELERATION_ARB); attribList.push_back(WGL_FULL_ACCELERATION_ARB); attribList.push_back(WGL_COLOR_BITS_ARB); attribList.push_back(colourBits); attribList.push_back(WGL_ALPHA_BITS_ARB); attribList.push_back(alphaBits); attribList.push_back(WGL_DEPTH_BITS_ARB); attribList.push_back(depthBits); attribList.push_back(WGL_STENCIL_BITS_ARB); attribList.push_back(stencilBits); attribList.push_back(WGL_SAMPLES_ARB); attribList.push_back(samples); if(mHasHardwareGamma) { attribList.push_back(WGL_FRAMEBUFFER_SRGB_CAPABLE_EXT); attribList.push_back(GL_TRUE); } attribList.push_back(0); UINT nformats; wglChoosePixelFormatARB(hdc, &(attribList[0]), nullptr, 1, &format, &nformats); } else { format = ChoosePixelFormat(hdc, &pfd); } SetPixelFormat(hdc, format, &pfd); }
bool Image::read(Stream stream) { BytesArray mem; mem = stream.process(); if (!stream.ok()) return false; FIMEMORY* fimem = FreeImage_OpenMemory(mem.raw(), mem.size()); if (!fimem) return false; FREE_IMAGE_FORMAT fiformat = FreeImage_GetFileTypeFromMemory(fimem); FIBITMAP* fibit = FreeImage_LoadFromMemory(fiformat, fimem); FreeImage_CloseMemory(fimem); if (!fibit) return false; //FIBITMAP* fitmp = fibit; //fibit = FreeImage_ConvertTo32Bits(fitmp); //FreeImage_Unload(fitmp); uint64 red_mask = FreeImage_GetRedMask(fibit); uint64 green_mask = FreeImage_GetGreenMask(fibit); uint64 blue_mask = FreeImage_GetBlueMask(fibit); uint64 alpha_mask = 0; PixelFormat tmpformat; tmpformat.setDepthBits(0); tmpformat.setStencilBits(0); tmpformat.setColorBitsFromMasks(&red_mask, &green_mask, &blue_mask, &alpha_mask); if (FreeImage_GetBPP(fibit) != tmpformat.bitsPerPixel()) { alpha_mask = ~(uint32)(red_mask | green_mask | blue_mask); tmpformat.setColorBitsFromMasks(&red_mask, &green_mask, &blue_mask, &alpha_mask); } format = TransferFormat(tmpformat.getRedBits(), tmpformat.getGreenBits(), tmpformat.getBlueBits(), tmpformat.getAlphaBits()); MIRROR_ASSERT(FreeImage_GetBPP(fibit) == format.bitsPerPixel(), "Conversion from FreeImage bitmap to Mirror bitmap failed!\nBits per pixel conversion from " "FreeImage color masks probably returned bad bits-per-channel values respectivly for red, green and blue channel, as" " stored inside PixelFormat structure - see PixelFormat::setColorBitsFromMasks for calculations!"); my_size = { FreeImage_GetWidth(fibit), FreeImage_GetHeight(fibit) }; uint32 req_bits = my_size.width()*my_size.height()*format.bitsPerPixel(); pixels.resize(PixelFormat::bitsToFullBytes(req_bits)); const uint32 pitch = FreeImage_GetPitch(fibit); const uint32 lineSize = FreeImage_GetLine(fibit); uint32 pixidx = 0; BYTE* line = FreeImage_GetBits(fibit); for (uint32 y = 0; y < my_size.height(); ++y, line += pitch) for (BYTE* pixel = line; pixel < line + lineSize; pixel += format.bytesPerPixel()) { pixels[pixidx++] = pixel[2]; //red pixels[pixidx++] = pixel[1]; //green pixels[pixidx++] = pixel[0]; //blue if (alpha_mask) pixels[pixidx++] = pixel[3]; //alpha } ////transfer pixels from FreeImage to our buffer as raw bits, should be in RGBA order thanks to our new color masks //FreeImage_ConvertToRawBits(pixels.raw(), fibit, format.bitsPerPixel()*my_size.width(), format.bitsPerPixel(), 0, 0, 0, TRUE); FreeImage_Unload(fibit); return true; }