Exemplo n.º 1
0
/**
 * Assumed that the right image is bound to IL
 */
int perform_operation(const char * operation, int verbose)
{
	/* Where to store the first parsing results? */
	char function[long_strlen], params[long_strlen], solid_params[long_strlen];
	/* Get the function name string and parameters string */
	parse_function(operation, function, params);
	/* Get rid of any whitespaces from the parameters string */
	remove_whitespaces(params, solid_params);
	if (verbose)
		printf("Calling %s(%s)\n", function, solid_params);
	/* What function was wanted? -1 means that we don't know */
	int function_index = -1;
	int i;
	for (i = 0; i < ILU_FUN_COUNT; i++)
		if (strncmp(function, ilu_functions[i].Name, short_strlen) == 0)
		{/* Yeah, this function was wanted. Let's have its index from the ilu_functions array */
			function_index = i;
			break;	/* nothing to do here any more */
		}
	if (function_index == -1)
	{/* Seems we haven't found anything... */
		fprintf(stderr, "Error: You have specified an invalid function name '%s' (have you called %s).\nRun '%s --help' command to get some help\n", function, operation, program_name);
		return 1;
	}
	/* We are going to try something and we want to know how it ended */
	ILboolean return_value;
	switch (ilu_functions[function_index].Parameter_type)
	{/* First semi-automatic processing according to type of parameters */
		case PARAM_VOID:
			{
				ILboolean (* function)() = ilu_functions[function_index].Callback;
				return_value = function();
				break;
			}/* endcase PARAM_VOID */
		case PARAM_ILUINT:
			{
				/* first assign and determine the type of the Callback */
				ILboolean (* function)(ILuint) = ilu_functions[function_index].Callback;
				/* then declare the parameter variables */
				ILuint param_value;
				/* fill them */
				int success = sscanf(solid_params, "%u", & param_value);
				if (success != 1)
				{/* see how it ended */
					fprintf(stderr, "Error interpreting '%s' as unsigned integer (when calling %s)\n", solid_params, operation);
					break;
				}
				/* execute the command and store the result */
				return_value = function(param_value);
				break;
			}/* endcase PARAM_ILUINT */
		case PARAM_ILFLOAT:
			{
				ILboolean (* function)(ILfloat) = ilu_functions[function_index].Callback;
				double param_value;
				int success = sscanf(solid_params, "%lf", & param_value);
				if (success != 1)
				{
					fprintf(stderr, "Error interpreting '%s' as float (when calling %s)\n", solid_params, operation);
					break;
				}
				return_value = function((ILfloat)param_value);
				break;
			}/* endcase PARAM_ILFLOAT */
		case PARAM_OTHERS:
			switch (function_index)
			{/* next, the manual processing according to names */
				case ILU_SHARPEN:
					{
						ILboolean (* function)(ILfloat, ILuint) = ilu_functions[function_index].Callback;
						double factor; 
						ILuint iter;
						int success = sscanf(solid_params, "%lf,%u", & factor, & iter);
						if (success != 2)
						{
							fprintf(stderr, "Error interpreting '%s' as floating-point number and unsigned integer separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function((ILfloat)factor, iter);
						break;
					}/* endcase ILU_SHARPEN */
				case ILU_CROP:
					{
						ILboolean (* function)(ILuint, ILuint, ILuint, ILuint, ILuint, ILuint ) = ilu_functions[function_index].Callback;
						ILuint xoff, yoff, zoff, width, height, depth;
						int success = sscanf(solid_params, "%u,%u,%u,%u,%u,%u", & xoff, & yoff, & zoff, & width, & height, & depth);
						if (success != 6)
						{
							fprintf(stderr, "Error interpreting '%s' as 6 unsigned integers separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function(xoff, yoff, zoff, width, height, depth);
						break;
					}/* endcase ILU_CROP */
				case ILU_ENLARGECANVAS:
				case ILU_SCALE:
					{
						ILboolean (* function)(ILuint, ILuint, ILuint) = ilu_functions[function_index].Callback;
						ILuint width, height, depth;
						int success = sscanf(solid_params, "%u,%u,%u", & width, & height, & depth);
						if (success != 3)
						{
							fprintf(stderr, "Error interpreting '%s' as 3 unsigned integers separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function(width, height, depth);
						break;
					}/* endcase ILU_ENLARGECANVAS + ILU_SCALE */
				case ILU_ENLARGEIMAGE:
				case ILU_SCALECOLOURS:
					{
						ILboolean (* function)(ILfloat, ILfloat, ILfloat) = ilu_functions[function_index].Callback;
						double first, second, third;
						int success = sscanf(solid_params, "%lf,%lf,%lf", & first, & second, & third);
						if (success != 3)
						{
							fprintf(stderr, "Error interpreting '%s' as 3 floating-point numbers separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function((ILfloat)first, (ILfloat)second, (ILfloat)third);
						break;
					}/* endcase ILU_ENLARGEIMAGE + ILU_SCALECOLOURS */
				case ILU_ROTATE3D:
				case ILU_SATURATE4F:
					{
						ILboolean (* function)(ILfloat, ILfloat, ILfloat, ILfloat) = ilu_functions[function_index].Callback;
						double first, second, third, fourth;
						int success = sscanf(solid_params, "%lf,%lf,%lf,%lf", & first, & second, & third, & fourth);
						if (success != 4)
						{
							fprintf(stderr, "Error interpreting '%s' as 4 floating-point numbers separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function((ILfloat)first, (ILfloat)second, (ILfloat)third, (ILfloat)fourth);
						break;
					}/* endcase ILU_ROTATE3D + ILU_SATURATE4F */
				case ILU_REPLACECOLOUR:
					{
						ILboolean (* function)(ILubyte, ILubyte, ILubyte, ILfloat ) = ilu_functions[function_index].Callback;
						ILuint red, green, blue;
						double tolerance;
						int success = sscanf(solid_params, "%u,%u,%u,%lf", & red, & green, & blue, & tolerance);
						if (success != 4)
						{
							fprintf(stderr, "Error interpreting '%s' as 3 8-bit unsigned integers and one floating-point number separated by comma (when calling %s)\n", solid_params, operation);
							break;
						}
						return_value = function((ILubyte)red, (ILubyte)green, (ILubyte)blue, (ILfloat)tolerance);
						break;
					}/* endcase ILU_ROTATE3D + ILU_SATURATE4F * */
				/* iluConvolution(ILint *matrix, ILint scale, ILint bias); Any idea about this? */ 
			}/* endswitch(function_index) */
			break;

	}/* endswitch (ilu_functions[function_index].Parameter_type) */
	/* It didn't end good for some reason... */
	if (return_value == IL_FALSE)
	{
		int error= ilGetError();
		fprintf(stderr, "Something got wrong when calling %s(%s): %s\n", function, solid_params, iluErrorString(error) );
		return error;
	}
	return 0;
}
Exemplo n.º 2
0
image_t *readimage(char *filename)
{
	int w,h,bands,bpc;
	image_t *ni=NULL;
	void *handle=NULL;
	ILuint imageid;
	/* library functions that we will look up dynamically */
	ILint (*dlilGetInteger)(ILenum);
	void (*dlilInit)(void);
	void (*dlilShutDown)(void);
	void (*dlilBindImage)(ILuint imageid);
	ILboolean (*dlilGenImages)(ILsizei,ILuint *imageid);
	ILboolean (*dlilLoadImage)(char *filename);
	void (*dlilDeleteImages)(ILsizei,const ILuint *);
	void (*dlilCopyPixels)(
			ILuint Xoff,
			ILuint Yoff,
			ILuint Zoff,
			ILuint Width,
			ILuint Height,
			ILuint Depth,
			ILenum Format,
			ILenum Type,
			void *Data);

	dlerror(); /* clear any old errors in dynamic library loading */
	/* get the library handle */
	handle=dlopen("libIL.so.1",RTLD_LAZY);
	if (handle==NULL)
	{
		fprintf(stderr,"Error opening library `libIL.so.1'\n");
		fprintf(stderr,"%s\n",dlerror());
	}

	/* get all the function handles */
	
	/******* ilGetInteger ***********/
	dlerror();
	dlilGetInteger=dlsym(handle,"ilGetInteger");
	if (dlilGetInteger==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilInit ***********/
	dlerror();
	dlilInit=dlsym(handle,"ilInit");
	if (dlilInit==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilShutDown ***********/
	dlerror();
	dlilShutDown=dlsym(handle,"ilShutDown");
	if (dlilShutDown==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilGenImages ***********/
	dlerror();
	dlilGenImages=dlsym(handle,"ilGenImages");
	if (dlilGenImages==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilBindImage ***********/
	dlerror();
	dlilBindImage=dlsym(handle,"ilBindImage");
	if (dlilBindImage==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilLoadImage ***********/
	dlerror();
	dlilLoadImage=dlsym(handle,"ilLoadImage");
	if (dlilLoadImage==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilDeleteImages ***********/
	dlerror();
	dlilDeleteImages=dlsym(handle,"ilDeleteImages");
	if (dlilDeleteImages==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	/******* ilCopyPixels ***********/
	dlerror();
	dlilCopyPixels=dlsym(handle,"ilCopyPixels");
	if (dlilCopyPixels==NULL)
	{
		fprintf(stderr,"Error getting symbols.\n");
		fprintf(stderr,"%s\n",dlerror());
		dlclose(handle);
		return NULL;
	}

	if (dlilGetInteger(IL_VERSION_NUM)<IL_VERSION)
	{
		fprintf(stderr,"DevIL library versions differ!\n");
		dlclose(handle);
		return NULL;
	}

	/* initialize the devIL library */
	dlilInit();

  /* generate an image name */
  dlilGenImages(1,&imageid);

  /* bind to this image as current image to do stuff */
  dlilBindImage(imageid);

  /* load an image */
  if (!dlilLoadImage(filename))
  {
		fprintf(stderr,"Error loading image `%s'\n",filename);
		dlilDeleteImages(1,&imageid);
		dlilShutDown();
		dlclose(handle);
		return NULL;
  }

  /* print out info about image */
  w=dlilGetInteger(IL_IMAGE_WIDTH);
  h=dlilGetInteger(IL_IMAGE_HEIGHT);
  bands=dlilGetInteger(IL_IMAGE_BPP);
  bpc=dlilGetInteger(IL_IMAGE_BPC);
	bands/=bpc;
  fprintf(stderr,"Image is %u by %u with %d channels at %u bpc.\n",
			w,h,bands,bpc);

	/* set up the image */
	ni=(image_t*)malloc(sizeof(image_t));
	ni->w=w;
	ni->h=h;
	ni->d=1;
	ni->bands=bands;
	ni->data=(float*)malloc(w*h*bands*sizeof(float));
	unsigned char *tmpdata=(unsigned char *)malloc(w*h*bands*bpc*sizeof(unsigned char));

	unsigned int format=IL_RGB;
	unsigned int type=IL_UNSIGNED_BYTE;
	switch(bands) {
		case 3:
			format=IL_RGB;
			break;
		case 4:
			format=IL_RGBA;
			break;
		default:
			format=IL_LUMINANCE;
			break;
	}
	switch(bpc) {
		case 1:
			type=IL_UNSIGNED_BYTE;
			break;
		case 2:
			type=IL_UNSIGNED_SHORT;
			break;
	}

	dlilCopyPixels(0,0,0,w,h,1,format,type,tmpdata);

	/* convert tmp data to floats */
	unsigned int val=0;
	unsigned int maxval=(1<<bpc*8)-1;
	int i;
	for (i=0;i<w*h*bands;i++) {
		if (bpc==1) val=(unsigned int)tmpdata[i];
		else if (bpc==2) val=(unsigned int)((short*)tmpdata)[i];
		else if (bpc==4) val=((unsigned int*)tmpdata)[i];
		ni->data[i]=(float)val/(float)maxval;
	}

  	/* delete the image */
  	dlilDeleteImages(1,&imageid);

	/* shutdown DevIL and free all memory */
	dlilShutDown();

  	dlclose(handle);

	free(tmpdata);

	return ni;
}