int main(int argc, char** argv) { // No filename is specified on the command-line. if (argc < 2) { printf ("Please run as:\n\nDevIL_testGL image_filename\n"); return 1; } FileName = argv[1]; // Set filename equal to the first argument. // // Check if the shared lib's version matches the executable's version. // // // fixed to get the right numbers from the right library call... // if (ilGetInteger(IL_VERSION_NUM) < IL_VERSION || iluGetInteger(ILU_VERSION_NUM) < ILU_VERSION || ilutGetInteger(ILUT_VERSION_NUM) < ILUT_VERSION) { printf ("DevIL library is out of date! Please upgrade\n"); return 2; } // Needed to initialize DevIL. ilInit (); iluInit(); // GL cannot use palettes anyway, so convert early. ilEnable (IL_CONV_PAL); // Gets rid of dithering on some nVidia-based cards. ilutEnable (ILUT_OPENGL_CONV); // Generate the main image name to use. ilGenImages (1, &ImgId); // Bind this image name. ilBindImage (ImgId); // Loads the image specified by File into the ImgId image. if (!ilLoadImage(FileName)) { HandleDevILErrors (); } // Make sure the window is in the same proportions as the image. // Generate the appropriate width x height less than or equal to MAX_X x MAX_Y. // Instead of just clipping Width x Height to MAX_X x MAX_Y, we scale to // an appropriate size, so the image isn't stretched/squished. Width = ilGetInteger (IL_IMAGE_WIDTH); Height = ilGetInteger (IL_IMAGE_HEIGHT); if (Width > 0) { // Don't want a divide by 0... if (Width > MAX_X) { Width = MAX_X; Height = (ILuint)(MAX_X / (ILfloat)ilGetInteger(IL_IMAGE_WIDTH) * Height); } } if (Height > 0) { // Don't want a divide by 0... if (Height > MAX_Y) { Height = MAX_Y; Width = (ILuint)(MAX_Y / (ILfloat)ilGetInteger(IL_IMAGE_HEIGHT) * Width); } } HandleDevILErrors (); // Standard glut initializations. glutInit (&argc, argv); // Standard glut initialization. glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE); glutInitWindowPosition (100, 100); glutInitWindowSize (Width, Height); window = glutCreateWindow("Developer's Image Library (DevIL) Test"); ilutInit(); glutDisplayFunc (DisplayFunc); glutKeyboardFunc (KeyboardFunc); // Goes into our setup function. if (Setup() == IL_FALSE) return 1; // Enter the main (Free)GLUT processing loop glutMainLoop(); // Clean up any loose ends. CleanUp(); return 0; }
const int GetTextureReference(const String& filename, GLint clampmode, GLint filtermode, bool optional) { bool cached = false; TextureCacheEntry* currentCacheEntry = NULL; //check cache to see if it's loaded already std::map<String,TextureCacheEntry>::iterator it = theTextureCache.find(filename); // See if we already have it in the cache. if (it != theTextureCache.end()) { // If we found it and it's not dirty, bail out with the index. if (!it->second.dirty) { return it->second.textureIndex; } // We found it, but it's dirty. We'll reload the texture below. cached = true; currentCacheEntry = &(it->second); } GLuint texRef; #if !_ANGEL_DISABLE_DEVIL ILuint imgRef; ilGenImages(1, &imgRef); ilBindImage(imgRef); // Load it up, log any errors if we need to if (!ilLoadImage(filename.c_str())) { if (!optional) { HandleDevILErrors(filename); } else { ClearDevILErrors(); } ilDeleteImages(1, &imgRef); return -1; } // Send it to GL texRef = BindTexImageWithClampAndFilter(clampmode, filtermode); // Clear it out ilDeleteImages(1, &imgRef); // output any errors that happened during the binding/deleting HandleDevILErrors(filename); #else int result = BindPNG(filename, texRef, clampmode, filtermode); if (result != 0) { return -1; } #endif //_ANGEL_DISABLE_DEVIL // If it was cached, clear the dirty flag so we don't try and load it again. if (cached) { currentCacheEntry->dirty = false; } // If we're not cached, add a new entry. else { TextureCacheEntry newEntry; newEntry.filename = filename; newEntry.clampMode = clampmode; newEntry.filterMode = filtermode; newEntry.textureIndex = texRef; newEntry.dirty = false; theTextureCache[filename] = newEntry; } return texRef; }
bool PixelsToPositions(const String& filename, Vector2List &positions, float gridSize, const Color& pixelColor, float tolerance) { #if _ANGEL_DISABLE_DEVIL png_byte* pngData; float* rawData; png_uint_32 width, height; if (!LoadPNG(filename, pngData, width, height)) { //error was output by the load return false; } // convert png ubyte data to float array rawData = (float*)malloc(3 * width * height * sizeof(float)); int j = 0; for (int i = 0; i < (4 * width * height); i++) { if ((i+1) % 4) { rawData[j] = (float)pngData[i] / 255.0f; j++; } } free(pngData); #else ILuint imgRef; ilGenImages(1, &imgRef); ilBindImage(imgRef); // load image into DevIL if (!ilLoadImage(filename.c_str())) { HandleDevILErrors(filename); return false; } // get image datums unsigned int width = ilGetInteger(IL_IMAGE_WIDTH); unsigned int height = ilGetInteger(IL_IMAGE_HEIGHT); // convert it to RGB floats for easy comparison ilConvertImage(IL_RGB, IL_FLOAT); // flip it if we need to if (ilGetInteger(IL_IMAGE_ORIGIN) != IL_ORIGIN_LOWER_LEFT) { iluFlipImage(); } // grab the raw data ILfloat* rawData = (ILfloat*)ilGetData(); #endif //_ANGEL_DISABLE_DEVIL // check every pixel, see if it's within the tolerance of the target float w = -((float)width)*gridSize/2.f; float h = -((float)height)*gridSize/2.f; Vector2 offset(w, h); float pixR, pixG, pixB; unsigned int pixOffset = 0; for (int y=0; y < height; y++) { for (int x=0; x < width; x++) { pixOffset = (y * width * 3) + (x * 3); pixR = rawData[pixOffset]; pixG = rawData[pixOffset + 1]; pixB = rawData[pixOffset + 2]; if ( fabs(pixR - pixelColor.R) <= tolerance && fabs(pixG - pixelColor.G) <= tolerance && fabs(pixB - pixelColor.B) <= tolerance ) { positions.push_back(offset + Vector2(x * gridSize, y * gridSize)); } } } // cleanup and return #if _ANGEL_DISABLE_DEVIL free(rawData); #else ilDeleteImages(1, &imgRef); #endif return true; }
bool GetRawImageData(const String& filename, std::vector<Color> &pixels) { #if _ANGEL_DISABLE_DEVIL png_byte* pngData; float* rawData; png_uint_32 width, height; if (!LoadPNG(filename, pngData, width, height, true)) { //error was output by the load return false; } // convert png ubyte data to float array rawData = (float*)malloc(4 * width * height * sizeof(float)); int j = 0; for (int i = 0; i < (4 * width * height); i++) { rawData[j] = (float)pngData[i] / 255.0f; j++; } free(pngData); #else ILuint imgRef; ilGenImages(1, &imgRef); ilBindImage(imgRef); // load image into DevIL if (!ilLoadImage(filename.c_str())) { HandleDevILErrors(filename); return false; } // get image datums unsigned int width = ilGetInteger(IL_IMAGE_WIDTH); unsigned int height = ilGetInteger(IL_IMAGE_HEIGHT); // convert it to RGB floats for easy comparison ilConvertImage(IL_RGBA, IL_FLOAT); // flip it if we need to if (ilGetInteger(IL_IMAGE_ORIGIN) != IL_ORIGIN_LOWER_LEFT) { iluFlipImage(); } // grab the raw data ILfloat* rawData = (ILfloat*)ilGetData(); #endif //_ANGEL_DISABLE_DEVIL // convert every pixel into colors float pixR, pixG, pixB, pixA; unsigned int pixOffset = 0; for (int y=0; y < height; y++) { for (int x=0; x < width; x++) { pixOffset = (y * width * 4) + (x * 4); pixR = rawData[pixOffset]; pixG = rawData[pixOffset + 1]; pixB = rawData[pixOffset + 2]; pixA = rawData[pixOffset + 3]; pixels.push_back(Color(pixR, pixG, pixB, pixA)); } } // cleanup and return #if _ANGEL_DISABLE_DEVIL free(rawData); #else ilDeleteImages(1, &imgRef); #endif return true; }