Scale a image

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
ne0h
Posts: 386
Joined: Thu Feb 21, 2008 2:15 am

Scale a image

Post by ne0h »

I've try to modify the graphic.c lib for scale a image to a custom value but the PSP crash when the image is load!
There is a simple library or a function for scale a image?

EDIT:
I've found yet a library ( Easy_Accelerated_Image_lib_0.2 ) that can scale a image!

I've another problem, i've try to modify the Easy_Accelerated_Image_lib_0.2
for load a image included in the Eboot with Bin2o!
But the compiler give me too many errors!!!!
I've do this work with graphic.c but if i copy and try to adapt the code with the graphic2d.c i've got many errors

Code: Select all

Image* loadImage(const unsigned char* data, int size)
{
	memory_png file;
	png_structp pngPtr;
	png_infop infoPtr;
	unsigned int sigRead = 0;
	png_uint_32 width, height;
	int bitDepth, colorType, interlaceType, x, y;
	u32* line;
	Image* image = (Image*) malloc(sizeof(Image));
    if (!image) return NULL;

    file.file = (char*)data;
    file.pos = 0;
    file.size = size;

	pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

	if (pngPtr == NULL) 
	{
		free(image);
		return NULL;
	}

	png_set_error_fn(pngPtr, (png_voidp) NULL, (png_error_ptr) NULL, NULL);
	png_set_read_fn(pngPtr, (png_voidp)&file, user_read_fn);
	infoPtr = png_create_info_struct(pngPtr);
	if (infoPtr == NULL) 
	{
		free(image);
		png_destroy_read_struct(&pngPtr, png_infopp_NULL, png_infopp_NULL);
		return NULL;
	}

	png_init_io(pngPtr, (FILE*)&file);
	png_set_sig_bytes(pngPtr, sigRead);
	png_read_info(pngPtr, infoPtr);
	png_get_IHDR(pngPtr, infoPtr, &width, &height, &bitDepth, &colorType, &interlaceType, int_p_NULL, int_p_NULL);

	// can't allow images with width or height greater than 512 or it will crash
	// the system.
	//
	if (width > 512 || height > 512) 
	{
		free(image);
		png_destroy_read_struct(&pngPtr, png_infopp_NULL, png_infopp_NULL);
		return NULL;
	}

	// attempt to allocate memory for new image
	//
	img = (Image*)malloc(sizeof(Image));
	if( img == NULL )
		return NULL;

	img->swizzled = 0;
	
	img->width = width;
	img->height = height;
	img->origWidth = width;
	img->origHeight = height;
	img->texWidth = nextPowOf2(width);
	img->texHeight = nextPowOf2(height);
	png_set_strip_16(pngPtr);
	png_set_packing(pngPtr);

	if (colorType == PNG_COLOR_TYPE_PALETTE) 
		png_set_palette_to_rgb(pngPtr);

	if &#40;colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8&#41; 
		png_set_gray_1_2_4_to_8&#40;pngPtr&#41;;

	if &#40;png_get_valid&#40;pngPtr, infoPtr, PNG_INFO_tRNS&#41;&#41; 
		png_set_tRNS_to_alpha&#40;pngPtr&#41;;

	png_set_filler&#40;pngPtr, 0xff, PNG_FILLER_AFTER&#41;;

	//img->data = malloc&#40;width * height * sizeof&#40;u32&#41;&#41;;
	img->data = &#40;unsigned char*&#41;malloc&#40; img->texWidth * img->height * sizeof&#40;u32&#41;&#41;;
	
	if &#40;!img->data&#41; 
	&#123;
		png_destroy_read_struct&#40;&pngPtr, png_infopp_NULL, png_infopp_NULL&#41;;
		free&#40;img&#41;;
		return NULL;
	&#125;

	line = &#40;u32*&#41; malloc&#40;width * 4&#41;;

	if &#40;!line&#41; 
	&#123;
		free&#40;img&#41;;
		free&#40;img->data&#41;;
		png_destroy_read_struct&#40;&pngPtr, png_infopp_NULL, png_infopp_NULL&#41;;
		return NULL;
	&#125;

	u32* data = &#40;u32*&#41;img->data;

	for &#40;y = 0; y < height; y++&#41; 
	&#123;
		png_read_row&#40;pngPtr, &#40;u8*&#41; line, png_bytep_NULL&#41;;

		for &#40;x = 0; x < width; x++&#41; 
		&#123;
			u32 color = line&#91;x&#93;;
			data&#91;x + y * img->texWidth&#93; =  color;
		&#125;
	&#125;

	// Png's are 32-bit color? so...
	//
	img->colorMode = 32 / 8; // i'm not sure about this

	free&#40;line&#41;;
	png_read_end&#40;pngPtr, infoPtr&#41;;
	png_destroy_read_struct&#40;&pngPtr, &infoPtr, png_infopp_NULL&#41;;

	if&#40; &#40;img->height % 8&#41; == 0 &#41;
		swizzleImage&#40;img&#41;;
	createImageVerts&#40;img&#41;;
	return img;

&#125;
And this is the line in graphics2d.h

Code: Select all


Image* loadImage&#40;const unsigned char* data, int size&#41;

I've read this code a lot of times! But I haven't find the errors

Code: Select all

main.c&#58; In function 'loadImage'&#58;
main.c&#58;7&#58; error&#58; expected declaration specifiers before 'asm'

main.c&#58;7&#58; error&#58; storage class specified for parameter '__lib_ent_top'

main.c&#58;7&#58; error&#58; storage class specified for parameter '__lib_ent_bottom'
main.c&#58;7&#58; error&#58; storage class specified for parameter '__lib_stub_top'
main.c&#58;7&#58; error&#58; storage class specified for parameter '__lib_stub_bottom'

main.c&#58;7&#58; error&#58; parameter 'module_info' is initialized
main.c&#58;7&#58; error&#58; section attribute not allowed for 'module_info'
main.c&#58;7&#58; error&#58; alignment may not be specified for 'module_info'
main.c&#58;20&#58; error&#58; storage class specified for parameter 'sfondo_start'
main.c&#58;23&#58; error&#58; expected '=', ',', ';', 'asm' or '__attribute__' before '&#123;' token
main.c&#58;29&#58; error&#58; expected '=', ',', ';', 'asm' or '__attribute__' before '&#123;' token

main.c&#58;41&#58; error&#58; expected '=', ',', ';', 'asm' or '__attribute__' before '&#123;' token
main.c&#58;53&#58; error&#58; expected '=', ',', ';', 'asm' or '__attribute__' before '&#123;' token
main.c&#58;68&#58; error&#58; old-style parameter declarations in prototyped function definition

main.c&#58;68&#58; error&#58; expected '&#123;' at end of input

make.exe&#58; *** &#91;main.o&#93; Error 1
And here is the main function

Code: Select all

int main&#40;&#41;
&#123;
	pspDebugScreenInit&#40;&#41;;
	SetupCallbacks&#40;&#41;;
	initGraphics&#40;&#41;;
	Image* backgroundImg;
	backgroundImg = loadImage&#40;sfondo_start, 0&#41;;
	startDrawing&#40;&#41;;
    drawImage&#40;backgroundImg,0,0,0.0f&#41;;
	endDrawing&#40;&#41;;
    flipScreen&#40;&#41;;
	endGraphics&#40;&#41;;
	freeImage&#40;backgroundImg&#41;;
	
	sceKernelSleepThread&#40;&#41;;
	return 0;
&#125;
Makefile:

Code: Select all

TARGET = main
OBJS = main.o graphics2d.o sfondo.o

CFLAGS = -O2 -G0 -Wall -g
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;
LIBDIR =

LIBS = -lmad -lpspaudiolib -lpspaudio -lpsppower -lpspgum -lpspgu -lpng -lz -lm
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = File Select


include $&#40;PSPSDK&#41;/lib/build.mak

sfondo.o&#58; sfondo.png
	bin2o -i sfondo.png sfondo.o sfondo
Post Reply