Advertisement
xerpi

ya2d_load_BMP_file FULL

Oct 8th, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1.  
  2. ya2d_Texture* ya2d_load_BMP_file(const char* filename, int place)
  3. {
  4.     SceUID fd;
  5.     if(!(fd = sceIoOpen(filename, PSP_O_RDONLY, 0777))) {
  6.         goto exit_error;
  7.     }
  8.    
  9.     BITMAPFILEHEADER bmp_fh;
  10.     sceIoRead(fd, (void*)&bmp_fh, sizeof(BITMAPFILEHEADER));   
  11.     if(bmp_fh.bfType != YA2D_BMPSIGNATURE) {
  12.         goto exit_close;
  13.     }
  14.    
  15.     BITMAPINFOHEADER bmp_ih;
  16.     sceIoRead(fd, (void*)&bmp_ih, sizeof(BITMAPINFOHEADER));
  17.    
  18.     unsigned int row_size;
  19.     if (bmp_ih.biBitCount == 32) {
  20.         row_size = bmp_ih.biWidth * 4;
  21.     }else if (bmp_ih.biBitCount == 24) {
  22.         row_size = bmp_ih.biWidth * 3;
  23.     } else if (bmp_ih.biBitCount == 16) {
  24.         row_size = bmp_ih.biWidth * 2;
  25.     } else {
  26.         goto exit_close;
  27.     }
  28.    
  29.     if(row_size%4 != 0) {
  30.         row_size += 4-(row_size%4);
  31.     }
  32.    
  33.     ya2d_Texture* texture = ya2d_create_texture(bmp_ih.biWidth, bmp_ih.biHeight,
  34.                                                 GU_PSM_8888, place);
  35.    
  36.     sceIoLseek(fd, bmp_fh.bfOffBits, SEEK_SET);
  37.    
  38.     void *buffer = malloc(row_size);
  39.     unsigned int* tex_ptr, color;
  40.     int i, x, y=bmp_ih.biHeight-1;
  41.     for(i = 0; i < bmp_ih.biHeight; ++i, y=bmp_ih.biHeight-1-i) {
  42.         sceIoRead(fd, buffer, row_size);
  43.         tex_ptr = (unsigned int*)(texture->data + y*texture->stride);
  44.         for(x = 0; x < bmp_ih.biWidth; ++x) {
  45.             if (bmp_ih.biBitCount == 32) {                  //ABGR8888
  46.                 color = *(unsigned int*)(buffer + x*4);
  47.                 *tex_ptr = (color&0xFF)<<24 | ((color>>8)&0xFF)<<16 |
  48.                            ((color>>16)&0xFF)<<8 | (color>>24);
  49.             } else if (bmp_ih.biBitCount == 24) {             //BGR888
  50.                 unsigned char *address = buffer + x*3;
  51.                 *tex_ptr =  (*address)<<16 | (*(address+1))<<8 |
  52.                             (*(address+2)) | (0xFF<<24);
  53.             } else if (bmp_ih.biBitCount == 16) {           //BGR565
  54.                 color = *(unsigned short*)(buffer + x*2);
  55.                 unsigned char r = (color&0x1F)*((float)255/31);
  56.                 unsigned char g = ((color>>5)&0x3F)*((float)255/63);
  57.                 unsigned char b = ((color>>11)&0x1F)*((float)255/31);
  58.                 *tex_ptr = ((r<<16) | (g<<8) | b | (0xFF<<24));
  59.             }
  60.             tex_ptr++;
  61.         }
  62.     }
  63.    
  64.     free(buffer);
  65.     sceIoClose(fd);
  66.     return texture;
  67.    
  68. exit_close:
  69.     sceIoClose(fd);
  70. exit_error:
  71.     return NULL;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement