Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ya2d_Texture* ya2d_load_BMP_file(const char* filename, int place)
- {
- SceUID fd;
- if(!(fd = sceIoOpen(filename, PSP_O_RDONLY, 0777))) {
- goto exit_error;
- }
- BITMAPFILEHEADER bmp_fh;
- sceIoRead(fd, (void*)&bmp_fh, sizeof(BITMAPFILEHEADER));
- if(bmp_fh.bfType != YA2D_BMPSIGNATURE) {
- goto exit_close;
- }
- BITMAPINFOHEADER bmp_ih;
- sceIoRead(fd, (void*)&bmp_ih, sizeof(BITMAPINFOHEADER));
- unsigned int row_size;
- if (bmp_ih.biBitCount == 32) {
- row_size = bmp_ih.biWidth * 4;
- }else if (bmp_ih.biBitCount == 24) {
- row_size = bmp_ih.biWidth * 3;
- } else if (bmp_ih.biBitCount == 16) {
- row_size = bmp_ih.biWidth * 2;
- } else {
- goto exit_close;
- }
- if(row_size%4 != 0) {
- row_size += 4-(row_size%4);
- }
- ya2d_Texture* texture = ya2d_create_texture(bmp_ih.biWidth, bmp_ih.biHeight,
- GU_PSM_8888, place);
- sceIoLseek(fd, bmp_fh.bfOffBits, SEEK_SET);
- void *buffer = malloc(row_size);
- unsigned int* tex_ptr, color;
- int i, x, y=bmp_ih.biHeight-1;
- for(i = 0; i < bmp_ih.biHeight; ++i, y=bmp_ih.biHeight-1-i) {
- sceIoRead(fd, buffer, row_size);
- tex_ptr = (unsigned int*)(texture->data + y*texture->stride);
- for(x = 0; x < bmp_ih.biWidth; ++x) {
- if (bmp_ih.biBitCount == 32) { //ABGR8888
- color = *(unsigned int*)(buffer + x*4);
- *tex_ptr = (color&0xFF)<<24 | ((color>>8)&0xFF)<<16 |
- ((color>>16)&0xFF)<<8 | (color>>24);
- } else if (bmp_ih.biBitCount == 24) { //BGR888
- unsigned char *address = buffer + x*3;
- *tex_ptr = (*address)<<16 | (*(address+1))<<8 |
- (*(address+2)) | (0xFF<<24);
- } else if (bmp_ih.biBitCount == 16) { //BGR565
- color = *(unsigned short*)(buffer + x*2);
- unsigned char r = (color&0x1F)*((float)255/31);
- unsigned char g = ((color>>5)&0x3F)*((float)255/63);
- unsigned char b = ((color>>11)&0x1F)*((float)255/31);
- *tex_ptr = ((r<<16) | (g<<8) | b | (0xFF<<24));
- }
- tex_ptr++;
- }
- }
- free(buffer);
- sceIoClose(fd);
- return texture;
- exit_close:
- sceIoClose(fd);
- exit_error:
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement