1
1
Fork 0
mirror of https://github.com/QB64Official/qb64.git synced 2024-09-04 11:10:15 +00:00
qb64/internal/c/parts/video/image/src.c
FellippeHeitor 5368b9d5d7 Initial attempt to restore _LOADIMAGE's ability to load in 8bit mode.
- When mode = 256, _LOADIMAGE will return an 8bit image with the loaded file's colors matching the default palette.
- Slower for bigger images, because of the call to matchcol();
2017-09-21 23:55:01 -03:00

159 lines
3.8 KiB
C

extern uint32 matchcol(int32 r,int32 g,int32 b);
#ifndef DEPENDENCY_IMAGE_CODEC
//Stub(s):
int32 func__loadimage(qbs *f,int32 bpp,int32 passed);
#else
#ifdef QB64_BACKSLASH_FILESYSTEM
#include "decode\\jpg\\src.c"
#include "decode\\png\\src.c"
#include "decode\\bmp\\src.c"
#include "decode\\other\\src.c" //PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM(PPM/PGM)
#else
#include "decode/jpg/src.c"
#include "decode/png/src.c"
#include "decode/bmp/src.c"
#include "decode/other/src.c" //PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM(PPM/PGM)
#endif
inline int32 func__red32(uint32 col){
return col>>16&0xFF;
}
inline int32 func__green32(uint32 col){
return col>>8&0xFF;
}
inline int32 func__blue32(uint32 col){
return col&0xFF;
}
int32 func__loadimage(qbs *f,int32 bpp,int32 passed){
if (new_error) return 0;
static int32 isHardware;
isHardware=0; if (bpp==33){bpp=32; isHardware=1;}
//validate bpp
if (passed){
if ((bpp!=32)&&(bpp!=256)){error(5); return 0;}
}else{
if (write_page->text){error(5); return 0;}
bpp=-1;
}
if (!f->len) return -1;//return invalid handle if null length string
//load the file
int32 fh,result = 0;
int64 lof;
fh=gfs_open(f,1,0,0);
if (fh<0) return -1;
lof=gfs_lof(fh);
static uint8* content;
content=(uint8*)malloc(lof); if (!content){gfs_close(fh); return -1;}
result=gfs_read(fh,-1,content,lof);
gfs_close(fh);
if (result<0){free(content); return -1;}
//Identify format:
static int32 format;
format=0;
//'.png'
if (lof>=8){
if ((content[0]==0x89)&&(content[1]==0x50)&&(content[2]==0x4E)&&(content[3]==0x47)&&
(content[4]==0x0D)&&(content[5]==0x0A)&&(content[6]==0x1A)&&(content[7]==0x0A))
{format=2; goto got_format;}//PNG
}//8
//'.bmp'
if (lof>=6){
if ((content[0]==0x42)&&(content[1]==0x4D)){
if ( (*((int32*)(&content[2]))) == lof ){//length of file
format=3; goto got_format;
}
}//BMP
}//6
//'.jpg' The first two bytes of every JPEG stream are the Start Of Image (SOI) marker values FFh D8h
if (lof>=2){
if ((content[0]==0xFF)&&(content[1]==0xD8)){format=1; goto got_format;}//JP[E]G
}//2
got_format:
static uint8 *pixels;
static int32 x,y;
if (format==1) pixels=image_decode_jpg(content,lof,&result,&x,&y);
if (format==2) pixels=image_decode_png(content,lof,&result,&x,&y);
if (format==3) pixels=image_decode_bmp(content,lof,&result,&x,&y);
if (!(result & 1)) {
pixels=image_decode_other(content,lof,&result,&x,&y);
}
free(content);
if (!(result&1)) return -1;
//...
static int32 i;
static int32 prevDest;
static uint16 scanX, scanY;
static uint8 red, green, blue;
if (bpp==256) {
i=func__newimage(x,y,256,1);
if (i==-1){free(pixels); return -1;}
prevDest=func__dest();
sub__dest(i);
for (scanY=0;scanY<y;scanY++){
for (scanX=0;scanX<x;scanX++){
blue=*pixels; pixels++;
green=*pixels; pixels++;
red=*pixels; pixels++;
pixels++; //skip alpha
pset(scanX,scanY,matchcol(red,green,blue));
}
}
sub__dest(prevDest);
} else {
i=func__newimage(x,y,32,1);
if (i==-1){free(pixels); return -1;}
memcpy(img[-i].offset,pixels,x*y*4);
}
free(pixels);
if (isHardware){
static int32 iHardware;
iHardware=func__copyimage(i,33,1);
sub__freeimage(i,1);
i=iHardware;
}
return i;
#ifndef NO_S_D_L
static qbs *tqbs=NULL,*nullt=NULL;
static int32 i;
if (new_error) return 0;
//validate bpp
if (passed){
if ((bpp!=32)&&(bpp!=256)){error(5); return 0;}
}else{
if (write_page->text){error(5); return 0;}
bpp=-1;
}
if (!f->len) return -1;//return invalid handle if null length string
if (!tqbs) tqbs=qbs_new(0,0);
if (!nullt){nullt=qbs_new(1,0); nullt->chr[0]=0;}
qbs_set(tqbs,qbs_add(f,nullt));
i=imgload(fixdir(tqbs),bpp);
if (!i) return -1;//failed
return -i;
return -1;
#endif //NO_S_D_L
}
#endif