www.pudn.com > faces.rar > traits.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. 
***/ 
 
#include  
#include  
#include  
#include  
#include "frbase.h" 
#include "fr.h" 
#include "recoalgo.h" 
 
/* prototypes for utility functions */ 
void frutilFreeTraitArray(FRrecognitionParameters *gParms, FRimageTrait* traits, unsigned int nrTraits); 
 
 
/* calculate traits */ 
int frTraitsCalc(FRrecognitionParameters* gParms, FRimage* rawImage, FRimageTrait** traits) { 
	unsigned int i; 
	int err = FR_OK; 
	FRrecoAlgo* algo; 
	FRimage image; 
 
	/* TODO: call face finder and resize found face properly */ 
	/* for now, just resize */ 
	if (frImgResize(&image, rawImage, gParms->width, gParms->height)) return FR_ERR_IL; 
 
 
	*traits = (FRimageTrait*) malloc(sizeof(FRimageTrait)*gParms->nrAlgorithms); 
	if (traits == NULL) return FR_ERR_NOMEM; 
 
	for (i=0; err == FR_OK && i < gParms->nrAlgorithms; i++) { 
		algo = frGetRecoAlgoById(gParms->algorithms[i].algorithmType); 
		err = algo->computeTrait(gParms, gParms->algorithms[i].data, &image, &(*traits)->data); 
	} 
 
	if (err != FR_OK) { 
		frutilFreeTraitArray(gParms, *traits, i-1); 
		*traits = NULL; 
	} 
	return err; 
} 
 
/* traits serialize */ 
size_t frTraitsGetSize(FRrecognitionParameters* gParms, FRimageTrait* traits) { 
	unsigned int i; 
	size_t sz=0; 
	FRrecoAlgo* algo; 
 
	for (i=0; i < gParms->nrAlgorithms; i++) { 
		algo = frGetRecoAlgoById(gParms->algorithms[i].algorithmType); 
		sz = sz + algo->getTraitSize(gParms, gParms->algorithms[i].data, traits[i].data); 
	} 
	return sz; 
} 
 
void frTraitsFree(FRrecognitionParameters* gParms, FRimageTrait** traits) { 
	frutilFreeTraitArray(gParms, *traits, gParms->nrAlgorithms); 
	*traits = NULL; 
} 
 
int frTraitsSerialize(FRrecognitionParameters* gParms, BYTE** mem, size_t maxsz, FRimageTrait** traits, BYTE direction) { 
	BYTE* begin = *mem; 
	unsigned int i; 
	int err = FR_OK; 
	FRrecoAlgo* algo; 
 
	/* sanity check size */ 
	if (direction == FR_OUT && maxsz < frTraitsGetSize(gParms, *traits)) 
		return FR_ERR_ALLOCMORE; 
 
	/* if we are reading from serialized form, allocate memory for our structure array */ 
	if (direction == FR_IN) { 
		*traits = (FRimageTrait*) malloc(sizeof(FRimageTrait)*gParms->nrAlgorithms); 
		if (*traits == NULL) return FR_ERR_NOMEM; 
	} 
 
	/* write/read data */ 
	for (i=0; err == FR_OK && i < gParms->nrAlgorithms; i++) { 
		algo = frGetRecoAlgoById(gParms->algorithms[i].algorithmType); 
		err = algo->serializeTrait(gParms, gParms->algorithms[i].data, &(*traits)[i].data, mem, maxsz-(*mem-begin),direction); 
	} 
 
	if (err != FR_OK && direction == FR_IN) 
		frutilFreeTraitArray(gParms, *traits, i-1); 
 
	return err; 
} 
 
int frTraitsSaveFile(FRrecognitionParameters* gParms, FILE* fileHandle, FRimageTrait* traits) { 
	BYTE* buffer, * temp; 
	size_t sz; 
	int err; 
 
	/* copy data to buffer */ 
	sz = frTraitsGetSize(gParms, traits); 
	temp = buffer = (BYTE*) malloc(sz); 
	if (buffer == NULL) return FR_ERR_NOMEM; 
	err = frTraitsSerialize(gParms, &temp, sz, &traits, FR_OUT); 
 
	/* write size and data */ 
	if (err == FR_OK && !fwrite(&sz, sizeof(sz),1, fileHandle)) err = FR_ERR_FILE; 
	if (err == FR_OK && !fwrite(buffer,sz, 1, fileHandle)) err = FR_ERR_FILE; 
 
	free(buffer); 
	return err; 
} 
 
int frTraitsLoadFile(FRrecognitionParameters* gParms, FILE* fileHandle, FRimageTrait** traits) { 
	BYTE* buffer, * temp; 
	size_t sz; 
	int err = FR_OK; 
 
	/* read size */ 
	if (!fread(&sz, sizeof(sz), 1, fileHandle)) return FR_ERR_FILE; 
 
	/* allocate buffer */ 
	temp = buffer = (BYTE*) malloc(sz); 
	if (buffer == NULL) return FR_ERR_NOMEM; 
 
	/* read serialized data */ 
	if (!fread(buffer, sz, 1, fileHandle)) err = FR_ERR_FILE; 
 
	/* deserialize */ 
	if (err == FR_OK) err = frTraitsSerialize(gParms, &temp, sz, traits, FR_IN); 
	free(buffer); 
	return err; 
} 
 
void frutilFreeTraitArray(FRrecognitionParameters *gParms, FRimageTrait* traits, unsigned int nrTraits) { 
	FRrecoAlgo* algo; 
	unsigned int i; 
 
	for (i=0; i < nrTraits; i++) { 
		algo = frGetRecoAlgoById(gParms->algorithms[i].algorithmType); 
		algo->freeTrait(gParms, gParms->algorithms[i].data, &traits[i].data); 
	} 
	free(traits); 
}