www.pudn.com > faces.rar > image.c


/*** 
 **     libface - Library of face recognition and supporting algorithms 
        Copyright (c) 2003 Stefan Farthofer 
 
	This file is part of libface, which is 
 
        free software; you can redistribute it and/or modify 
        it under the terms of the GNU General Public License as published by 
        the Free Software Foundation; either version 2 of the License, or 
        (at your option) any later version. 
 
        This program is distributed in the hope that it will be useful, 
        but WITHOUT ANY WARRANTY; without even the implied warranty of 
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
        GNU General Public License for more details. 
 
        You should have received a copy of the GNU General Public License 
        along with this program; if not, write to the Free Software 
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 
	For further information seek us at http://sourceforge.net/projects/openbio/ 
**	or write an email to dimitri.pissarenko@gmx.net or farthofer@chello.at. 
***/ 
/* implementation of image handling functions */ 
 
#include  
#include  
#include  
#include  
#include "frbase.h" 
#include "fr.h" 
 
/* maybe put this in another header if we accumulate enough util functions */ 
int frutilEvalILErrors(void); 
 
int frImgLoadFile(char* filename, FRimage* image) { 
	ILuint img; 
	int err; 
 
	/* load image with IL */ 
	ilGenImages(1, &img); 
	ilBindImage(img); 
	ilLoadImage(filename); 
	ilConvertImage(IL_LUMINANCE, IL_FLOAT); 
 
	/* when loaded ok copy bound image to image struct */ 
	if ((err = frutilEvalILErrors()) == FR_OK) { 
		err = frImgLoadBound(image); 
	} 
 
	ilDeleteImages(1, &img); 
	return err; 
} 
 
int frImgSaveFile(char* filename, FRimage* image) { 
	ILuint img; 
	int err; 
 
	ilGenImages(1, &img); 
	ilBindImage(img); 
	ilTexImage(image->width,image->height,1,1,IL_LUMINANCE, IL_FLOAT, image->imgdata); 
	ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE); 
	ilSaveImage(filename); 
	ilDeleteImages(1, &img); 
	if ((err = frutilEvalILErrors()) != FR_OK) 
		return err; 
	return FR_OK; 
} 
 
int frImgLoadMem(void* mem, size_t sz, FRimage* image, ILenum type) { 
	ILuint img; 
	int err; 
 
	/* load image with IL */ 
	ilGenImages(1, &img); 
	ilBindImage(img); 
	ilLoadL(type,mem,(ILuint)sz); 
	ilConvertImage(IL_LUMINANCE, IL_FLOAT); 
 
	/* when loaded ok copy bound image to image struct */ 
	if ((err = frutilEvalILErrors()) == FR_OK) { 
		err = frImgLoadBound(image); 
	} 
 
	ilDeleteImages(1, &img); 
	return err; 
} 
 
/* used to load real RAW images with 1 Byte per Pixel, don't confuse with 
   IL_RAW since this format does expect a header with the width/height of 
   the image to be loaded. 
 */ 
int frImgLoadRaw(void* mem, int width, int height, FRimage* image) { 
	ILuint img; 
	int err = FR_OK; 
 
	ilGenImages(1, &img); 
	ilBindImage(img); 
	ilTexImage(width, height, 1, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, mem); 
	ilConvertImage(IL_LUMINANCE, IL_FLOAT); 
 
	/* check if there were errors, then free IL image, set struct elements and return */ 
	err = frutilEvalILErrors(); 
 
	ilDeleteImages(1, &img); 
	image->width = width; 
	image->height = height; 
	return FR_OK; 
} 
 
int frImgGetILImg(ILuint* imgnr, FRimage* image) { 
	int err; 
 
	ilGenImages(1, imgnr); 
	ilBindImage(*imgnr); 
	ilTexImage(image->width,image->height,1,1,IL_LUMINANCE, IL_FLOAT, image->imgdata); 
	ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE); 
 
	/* if anything went wrong set imgnr to 0 and return with error */ 
	if ((err = frutilEvalILErrors()) != FR_OK) { 
		ilDeleteImages(1, imgnr); 
		*imgnr = 0; 
		return err; 
	} 
	return FR_OK; 
} 
 
int frImgGetILImgFloat(ILuint* imgnr, FRimage* image) { 
	int err; 
 
	ilGenImages(1, imgnr); 
	ilBindImage(*imgnr); 
	ilTexImage(image->width,image->height,1,1,IL_LUMINANCE, IL_FLOAT, image->imgdata); 
 
	/* if anything went wrong set imgnr to 0 and return with error */ 
	if ((err = frutilEvalILErrors()) != FR_OK) { 
		ilDeleteImages(1, imgnr); 
		*imgnr = 0; 
		return err; 
	} 
	return FR_OK; 
} 
 
void frImgFree(FRimage* image) { 
	if (image->imgdata != NULL) { 
		free(image->imgdata); 
	} 
} 
 
/* allocates an image struct and loads the currently bound IL image */ 
int frImgLoadBound(FRimage* image) { 
	size_t sz; 
 
	image->width = ilGetInteger(IL_IMAGE_WIDTH); 
	image->height = ilGetInteger(IL_IMAGE_HEIGHT); 
	sz = sizeof(float) * image->width * image->height; 
	image->imgdata = (float*) malloc(sz); 
	if (image->imgdata == NULL) return FR_ERR_NOMEM; 
	memcpy((void*)image->imgdata, ilGetData(), sz); 
	return FR_OK; 
} 
 
int frImgResize(FRimage* dst, FRimage* src, unsigned int w, unsigned int h) { 
	ILuint img; 
	int err; 
 
	if ((err = frImgGetILImgFloat(&img, src))) return err; 
	iluScale(w,h,1); 
	if (frutilEvalILErrors()) return FR_ERR_IL; 
	return frImgLoadBound(dst); 
} 
 
int frutilEvalILErrors(void) { 
	ILenum err; 
	int nr = 0; 
 
	/* for now we don't do any logging, this should change in the future */ 
	while ((err = ilGetError()) != IL_NO_ERROR) { 
		/* printf("%s %s", prefix, iluErrorString(err)); */ 
		nr++; 
	} 
	/* just say IL error for now */ 
	if (nr > 0) 
		return FR_ERR_IL; 
	else 
		return FR_OK; 
}