/* SImage::convertAlphaMap * Converts the image to an alpha map, generating alpha values from * either pixel brightness or existing alpha, depending on the value * of [alpha_source] *******************************************************************/ bool SImage::convertAlphaMap(int alpha_source, Palette8bit* pal) { // Get RGBA data MemChunk rgba; getRGBAData(rgba, pal); // Recreate image create(width, height, ALPHAMAP); // Generate alpha mask unsigned c = 0; for (int a = 0; a < width * height; a++) { // Determine alpha for this pixel uint8_t alpha = 0; if (alpha_source == BRIGHTNESS) // Pixel brightness alpha = double(rgba[c])*0.3 + double(rgba[c+1])*0.59 + double(rgba[c+2])*0.11; else // Existing alpha alpha = rgba[c+3]; // Set pixel data[a] = alpha; // Next RGBA pixel c += 4; } // Announce change announce("image_changed"); return true; }
/* SImage::convertRGBA * Converts the image to 32bpp (RGBA). Returns false if the image was * already 32bpp, true otherwise. *******************************************************************/ bool SImage::convertRGBA(Palette8bit* pal) { // If it's already 32bpp do nothing if (type == RGBA) return false; // Get 32bit data MemChunk rgba_data; getRGBAData(rgba_data, pal); // Clear current data clearData(true); // Copy it data = new uint8_t[width * height * 4]; memcpy(data, rgba_data.getData(), width * height * 4); // Set new type & update variables type = RGBA; has_palette = false; // Announce change announce("image_changed"); // Done return true; }
/* SImage::convertPaletted * Converts the image to paletted + mask. [pal_target] is the new * palette to convert to (the image's palette will also be set to * this). [pal_current] will be used as the image's current palette * if it doesn't already have one *******************************************************************/ bool SImage::convertPaletted(Palette8bit* pal_target, Palette8bit* pal_current) { // Check image/parameters are valid if (!isValid() || !pal_target) return false; // Get image data as RGBA MemChunk rgba_data; getRGBAData(rgba_data, pal_current); // Create mask from alpha info (if converting from RGBA) if (type == RGBA || type == ALPHAMAP) { // Clear current mask if (mask) delete[] mask; // Init mask mask = new uint8_t[width * height]; // Get values from alpha channel int c = 0; for (int a = 3; a < width * height * 4; a += 4) mask[c++] = rgba_data[a]; } // Load given palette palette.copyPalette(pal_target); // Clear current image data (but not mask) clearData(false); // Do conversion data = new uint8_t[width * height]; unsigned i = 0; rgba_t col; for (int a = 0; a < width*height; a++) { col.r = rgba_data[i++]; col.g = rgba_data[i++]; col.b = rgba_data[i++]; data[a] = palette.nearestColour(col); i++; // Skip alpha } // Update variables type = PALMASK; has_palette = true; // Announce change announce("image_changed"); // Success return true; }
void GameSprite::Image::createGLTexture(GLuint whatid) { ASSERT(!isGLLoaded); uint8_t* rgba = getRGBAData(); if(!rgba) { return; } isGLLoaded = true; g_gui.gfx.loaded_textures += 1; glBindTexture(GL_TEXTURE_2D, whatid); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Linear Filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 0x812F); // GL_CLAMP_TO_EDGE glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 0x812F); // GL_CLAMP_TO_EDGE glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SPRITE_PIXELS, SPRITE_PIXELS, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgba); delete[] rgba; #undef SPRITE_SIZE }