sdl-gpu icon indicating copy to clipboard operation
sdl-gpu copied to clipboard

GPU_SaveImage() exportes a broken png file of a GPU_Image that is loaded from a png

Open dhiahassen opened this issue 7 years ago • 7 comments

GPU_SaveImage() exports a broken png file , i loaded a png file , stored it in a GPU_Image* , and it is rendred fine , but when saving itthe resultant png is broken , when using SDL_SDL_SaveBMP() the resultant file bmp is valide, , the issue is the stb extention you are using , i edited the function implimentation replacing stb calls with SDL2_IMAGE calls and the resultant png is now valide , wanned you to know that SDL2_IMAGE doesnt use stb , it usees : tdefl_write_image_to_png_file_in_memory_ex() function from miniz.h , and it is working fine and never broken , and by the way , in my case GPU_SaveImage() fails this way only with some images loaded from png files ( i can attach the png that failed after i loaded it and saved it again )

dhiahassen avatar Sep 03 '17 00:09 dhiahassen

Can you send me the png that isn't working right? It's very probable that stb-image-write just can't handle it.

I do recommend using an external loader like SDL_image if you need better png support (e.g. stb-image-write is very slow). You should be able to use GPU_CopyImageFromSurface() with a surface loaded from SDL_image, then use GPU_CopySurfaceFromImage() to get it ready to save.

grimfang4 avatar Sep 03 '17 15:09 grimfang4

Buddy i am afraid the issue is more SDL_gpu related , i was wrong , it is true that SDL_SaveBMP() gives a valide bmp file , but the issue is related to sdl_gpu and png files , actually not because of stb library , i will give you and example :

GPU_Init(screen_w,screen_h,GPU_DEFAULT_INIT_FLAGS); SDL_Surface* img_srf1 = IMG_Load("img.png"); GPU_Image* gpu_img = GPU_CopyImageFromSurface(img_srf1); SDL_Surface* img_srf2 = GPU_CopySurfaceFromImage(gpu_img) IMG_SavePNG(img_srf1,"img2.png");

at the end line IMG_SavePNG(img_srf1,"img2.png"); when i put img_srf1 , the resultant png is valide , but when i put img_srf2 that is created by sdl_gpu by converting , the resultant png becomes broken , ill attach the orginal png for you i hope the server wont edit the meta-data ... img

it is a simple blue filt image , the result is this : img2

so the problem is with sdl_gpu converting from surface to image or from image to surface or both

dhiahassen avatar Sep 03 '17 16:09 dhiahassen

Okay, that's good! I'll see if I can sit down with it this week and figure out the problem.

grimfang4 avatar Sep 03 '17 19:09 grimfang4

Ok now i fixed the problem , it is in CopyImageFromSurface() , when i avoided that function this way :

SDL_Surface* srf1 = GPU_LoadSurface("img.png") GPU_Image* img1 = GPU_CreateImage(srf1->w,srf1->h,GPU_FORMAT_RGBA); GPU_UpdateImage(img1,nullptr,srf1,nullptr) GPU_SaveImage(img1,"img2.png",FORMAT_PNG) when i replace CopyImageFromSurface() with that code the resultant image is valide , but when i put GPU_FORMAT_RGB instead of GPU_FORMAT_RGBA it is broken again !! so the problem is with this portion of code inside CopyImageFromSurface() :

// See what the best image format is. if(surface->format->Amask == 0) { if(has_colorkey(surface)) format = GPU_FORMAT_RGBA; else format = GPU_FORMAT_RGB; } else format = GPU_FORMAT_RGBA;

when i comment that portion and leave only format = GPU_FORMAT_RGBA; it works fine with whatever image :)

dhiahassen avatar Sep 03 '17 19:09 dhiahassen

Cool, that narrows it down. That check is clearly not the most robust thing. ;)

grimfang4 avatar Sep 03 '17 19:09 grimfang4

Lets change this :

static_inline GPU_bool has_colorkey(SDL_Surface* surface) { return (SDL_GetColorKey(surface, NULL) == 0); }

to this :

static inline GPU_boll has_colorkey(SDL_Surface* surface) { return surface->format->format == SDL_PIXELFORMAT_RGBA4444 }

according to this : https://forums.libsdl.org/viewtopic.php?p=48063 & https://wiki.libsdl.org/SDL_PixelFormatEnum

but actually not only RGBA4444 but also any format with the A field

dhiahassen avatar Sep 03 '17 21:09 dhiahassen

Can you see if this is resolved in 837c2d57355f0553cd492e28d7fbbb37039eec72?

grimfang4 avatar Dec 22 '17 17:12 grimfang4