bool ImageUtils::flipImageVertically(Image* pImage) { unsigned int scanlines = pImage->getHeight(); // only need to do half of them, as we're swapping top and bottom unsigned int numRows = scanlines / 2; unsigned int width = pImage->getWidth(); bool done = false; if (pImage->getImageType() == (Image::IMAGE_CHANNELS_3 | Image::IMAGE_FORMAT_FLOAT)) { ImageColour3f* pImageColour3f = static_cast<ImageColour3f*>(pImage); flipImageVertically(pImageColour3f, scanlines, numRows, width, width * sizeof(Colour3f)); done = true; } else if (pImage->getImageType() == (Image::IMAGE_CHANNELS_3 | Image::IMAGE_FORMAT_HALF)) { ImageColour3h* pImageColour3h = static_cast<ImageColour3h*>(pImage); flipImageVertically(pImageColour3h, scanlines, numRows, width, width * sizeof(Colour3h)); done = true; } else if (pImage->getImageType() == (Image::IMAGE_CHANNELS_1 | Image::IMAGE_FORMAT_FLOAT)) { Image1f* pImage1f = static_cast<Image1f*>(pImage); flipImageVertically(pImage1f, scanlines, numRows, width, width * sizeof(float)); done = true; } else if (pImage->getImageType() == (Image::IMAGE_CHANNELS_1 | Image::IMAGE_FORMAT_HALF)) { Image1h* pImage1h = static_cast<Image1h*>(pImage); flipImageVertically(pImage1h, scanlines, numRows, width, width * sizeof(half)); done = true; } return done; }
void makeScreenshot(HWND window, const char* fileName) { if(! fileName){ DateTime curDateTime; char dateTimeText[MAX_PATH]; sprintf_s(dateTimeText, MAX_PATH, "%s.gif", curDateTime.longText); fileName = dateTimeText; } gif::GIF* g = gif::newGIF(0); RECT rect; GetClientRect(window, &rect); int w = rect.right - rect.left, h = rect.bottom - rect.top; unsigned char* rgb = new unsigned char[w * h * 3]; glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, rgb); flipImageVertically(rgb, w, h); gif::addFrame(g, w, h, rgb, 0); delete[] rgb; gif::write(g, fileName); gif::dispose(g); }
CTgaImageFile::CTgaImageFile(const std::string filePath) { // Check file`s existance. if(!(m_fp = fopen(filePath.c_str(), "rb"))) { CLogger::failedLoadWarning(filePath, "File not found or broken"); return; } fread(&m_tgaIdLength, sizeof(uint8_t), 1, m_fp); fread(&m_tgaColorMapType, sizeof(uint8_t), 1, m_fp); fread(&m_tgaImageType, sizeof(uint8_t), 1, m_fp); fread(&m_tgaColorMapFirstEntry, sizeof(int16_t), 1, m_fp); fread(&m_tgaColorMapLength, sizeof(int16_t), 1, m_fp); fread(&m_tgaColorMapEntrySize, sizeof(uint8_t), 1, m_fp); fread(&m_tgaOriginX, sizeof(int16_t), 1, m_fp); fread(&m_tgaOriginY, sizeof(int16_t), 1, m_fp); fread(&m_tgaWidth, sizeof(int16_t), 1, m_fp); fread(&m_tgaHeight, sizeof(int16_t), 1, m_fp); fread(&m_tgaBPP, sizeof(uint8_t), 1, m_fp); fread(&m_tgaDescriptor, sizeof(uint8_t), 1, m_fp); m_components = m_tgaBPP / 8; m_bytes = m_tgaWidth * m_tgaHeight * m_components; // Check if the image type is supported by this loader. if(m_tgaImageType != TGA_RAW_BYTE_DATA and m_tgaImageType != TGA_RLE_COMPRESSED_DATA) { CLogger::failedLoadWarning(filePath, "Unsupported file type"); fclose(m_fp); return; } // Check if the BPP is supported by this loader and set the proper GL type. if(m_tgaBPP == 24) { m_colorType = GL_RGB; } else { if(m_tgaBPP == 32) { m_colorType = GL_RGBA; } else { if(m_tgaImageType == TGA_RAW_BYTE_DATA) { CLogger::failedLoadWarning(filePath, "Unsupported raw format"); fclose(m_fp); return; } else // RLE { CLogger::failedLoadWarning(filePath, "Unsupported compressed format"); fclose(m_fp); return; } } } // Get image data. getImageData(); // The file can be closed now as the data is in the memory. fclose(m_fp); // Flip correction. if(!(m_tgaDescriptor & TGA_FLIPPED_VERTICALLY)) { flipImageVertically(); } m_imgBaseWidth = m_tgaWidth; m_imgBaseHeight = m_tgaHeight; m_imgHasAlpha = (m_tgaBPP == 32) ? true : false; if(m_imgHasAlpha) { m_imgFormat = GL_RGBA; } else { m_imgFormat = GL_RGB; } m_imgType = GL_UNSIGNED_BYTE; m_imgLevelAmount = 1; m_imgValid = true; m_imgBlockSize = (m_tgaBPP == 32) ? 4 : 3; m_imgFilePath = filePath; }