All pastes #578398 Raw Edit

Someone

public c v1 · immutable
·published 2007-06-20 08:35 UTC
rendered paste body
#include <GL/glew.h>#include <IL/il.h>#include <IL/ilu.h>#include <stdlib.h>#include <assert.h>#include "image.h"Image *load_image(char *filename){    Image  *img;    ILuint image_name;    ILenum error;    char   *error_str;    ILint cur_format;    ilGenImages(1, &image_name);    ilBindImage(image_name);    // Force OpenGL-compatible row order.    ilEnable(IL_ORIGIN_SET);    ilOriginFunc(IL_ORIGIN_LOWER_LEFT);    if (ilLoadImage(filename)) {        cur_format = ilGetInteger(IL_IMAGE_FORMAT);        if ( cur_format == IL_COLOR_INDEX ) {            printf("DEBUG: load_image: Image format for \"%s\" is COLOR_INDEX, converting to RGBA\n", filename);            if ( !ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE) ) {                fprintf(stderr, "load_image: Failed to convert color indexed image \"%s\" to RGBA.\n", filename);                ilDeleteImages(1, &image_name);                return NULL;            }        }        img = (Image *) malloc(sizeof(Image));        img->width  = ilGetInteger(IL_IMAGE_WIDTH);        img->height = ilGetInteger(IL_IMAGE_HEIGHT);        img->data   = (unsigned char *) malloc(img->width * img->height * 4);        if( !ilCopyPixels(0, 0, 0, img->width, img->height, 1, IL_RGBA, IL_UNSIGNED_BYTE, img->data) ) {            error = ilGetError();            error_str = iluErrorString(error);            fprintf(stderr, "load_image: Failed to read image data for \"%s\". (Last error given by DevIL was \"%s (code %d)\".)\n", filename, error_str, error);            free( (void *) img->data );            free(img);            ilDeleteImages(1, &image_name);            return NULL;        }    } else {        fprintf(stderr, "load_image: Failed to load image \"%s\".\n", filename);        ilDeleteImages(1, &image_name);        return NULL;    }    ilDeleteImages(1, &image_name);    return img;}void draw_image(Image *img, int x, int y){    if ( img != NULL ) {        glWindowPos2iARB(x, y);        glDrawPixels(img->width, img->height, GL_RGBA, GL_UNSIGNED_BYTE, img->data);    } else {#ifdef DEBUG        fprintf(stderr, "DEBUG: draw_image: Unable to draw image, img = NULL.\n");#endif    }}void delete_image(Image *img){    if ( img != NULL ) {        if ( img->data != NULL ) {            free(img->data);        } else {#ifdef DEBUG                fprintf(stderr, "DEBUG: delete_image: Warning, img->data is NULL.\n");#endif        }        free(img);    } else {#ifdef DEBUG            fprintf(stderr, "DEBUG draw_image: Warning, img is NULL.\n");#endif    }}