forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Solved] (Blit image to VRAM) sceGuCopyImage in Kernel mode?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sat Nov 15, 2008 8:14 pm    Post subject: [Solved] (Blit image to VRAM) sceGuCopyImage in Kernel mode? Reply with quote

Is it possible?

I'm under the impression that sceGuCopyImage will handle the source pixel format and convert it for the current display mode.

I just want to copy an 8888 image to VRAM in-game using sceGuCopyImage.

If it doesn't then I assume I'll have to write code to convert the source image into the current pixel format and write it to VRAM manually? Any one have some sample code to do this?

I blitting stuff to VRAM now, but for blitting an image the pixel format becomes a problem depending on what mode the game is using, so I though the GU functions would handle that.


Last edited by Torch on Sun Nov 16, 2008 1:23 am; edited 1 time in total
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Nov 16, 2008 1:20 am    Post subject: Reply with quote

I threw together something to blit 8888 RGBA images to VRAM, with automatic conversion to target pixel format with alpha blending. Its based off someones's screenshot plugin.

Code:
//Globals
int pmode, pwidth, pheight, bufferwidth, pixelformat;
unsigned int* vram32;
unsigned short* vram16;

//Somewhere in your code
...
   sceDisplayGetMode(&pmode, &pwidth, &pheight);
   sceDisplayGetFrameBuf((void*)&vram32, &bufferwidth, &pixelformat, PSP_DISPLAY_SETBUF_IMMEDIATE);
   vram16 = (unsigned short*) vram32;
   
   drawimage(smiley, smiley_w, smiley_h, 20, 20);
...

void drawimage(unsigned char * image, unsigned int srcw, unsigned int srch, unsigned int destx, unsigned int desty)
{
   #define sbpp 4 //Source bytes-per-pixel (RGBA=4)
   unsigned int x, y, pos = 0;
   
   sceDisplayWaitVblankStart();

   for(y = desty; y < desty+srch; y++){
      for(x = destx; x < destx+srcw; x++){
         unsigned int color, offset = x + y*bufferwidth;
         unsigned char r = 0, g = 0, b = 0;
         
         switch (pixelformat) {
            case 0:   // 16-bit RGB 5:6:5
               color = vram16[offset];
               r = (color & 0x1f) << 3;
               g = ((color >> 5) & 0x3f) << 2;
               b = ((color >> 11) & 0x1f) << 3;
               r = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*r + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp]) >> 3;
               g = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*g + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 1]) >> 2;
               b = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*b + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 2]) >> 3;
               vram16[offset] = (b << 11)|(g << 5)|r;
               break;
            case 1:// 16-bit RGBA 5:5:5:1
               color = vram16[offset];
               r = (color & 0x1f) << 3;
               g = ((color >> 5) & 0x1f) << 3;
               b = ((color >> 10) & 0x1f) << 3;
               r = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*r + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp]) >> 3;
               g = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*g + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 1]) >> 3;
               b = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*b + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 2]) >> 3;
               vram16[offset] = (b << 10)|(g << 5)|r;
               break;
            case 2:// 16-bit RGBA 4:4:4:4
               color = vram16[offset];
               r = (color & 0xf) << 4;
               g = ((color >> 4) & 0xf) << 4;
               b = ((color >> 8) & 0xf) << 4;
               r = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*r + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp]) >> 4;
               g = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*g + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 1]) >> 4;
               b = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*b + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 2]) >> 4;
               vram16[offset] = (b << 8)|(g << 4)|r;
               break;
            case 3:// 32-bit RGBA 8:8:8:8
               color = vram32[offset];
               r = color & 0xff;
               g = (color >> 8) & 0xff;
               b = (color >> 16) & 0xff;
               r = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*r + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp]);
               g = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*g + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 1]);
               b = (unsigned char)(((255.0f - image[pos*sbpp + 3])/255.0f)*b + (image[pos*sbpp + 3]/255.0f)*image[pos*sbpp + 2]);
               vram32[offset] = (b << 16)|(g << 8)|r;
               break;
         }
         pos++;
      }
   }
   
   sceKernelDcacheWritebackAll();
}
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Sun Nov 16, 2008 4:33 am    Post subject: Reply with quote

Cleaned it up a bit. Made it a nice callable function with source params.
Back to top
View user's profile Send private message
.::Albandu51::.



Joined: 04 Oct 2007
Posts: 12

PostPosted: Mon Nov 24, 2008 5:36 am    Post subject: Reply with quote

hi Torch !

Sorry for my language but i'm French.

Your fonction for load a image ,what type of image is loading ? Bmp , Png ?

It's BMP file , no ? I don't sure :)


Thank's
Back to top
View user's profile Send private message
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Mon Nov 24, 2008 12:42 pm    Post subject: Reply with quote

The char * image[] should contain raw 8888 RGBA data. I'm not loading any image in my app, I've directly saved the RGBA data in the source code as an array.

You can export an image as raw data using GIMP (by choosing RAW in the Save As options).

If you have images in PNG, BMP etc, then you can use already available code to load it. Then just pass the array of the image data after loading.
Back to top
View user's profile Send private message
.::Albandu51::.



Joined: 04 Oct 2007
Posts: 12

PostPosted: Mon Nov 24, 2008 4:24 pm    Post subject: Reply with quote

Ok thank's !
Back to top
View user's profile Send private message
kralyk



Joined: 06 Apr 2008
Posts: 114
Location: Czech Republic, central EU

PostPosted: Mon Nov 24, 2008 11:09 pm    Post subject: Reply with quote

Torch wrote:
Its based off someones's screenshot plugin.


What? Where? When? How? ;-)
I'd just love to see a source code of a screenshot plugin, because of my future work, do you have it or can you tell me where I can find it?
_________________
...sorry for my english...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group