2 * Acess2 GUI (AxWin) Version 3
3 * - By John Hodge (thePowersGang)
13 #include "include/image.h"
16 extern tImage *Image_SIF_Parse(void *Buffer, size_t Size);
19 int UnBase64(uint8_t *Dest, char *Src, int BufSize);
23 * \brief Open an image from a URI
25 tImage *Image_Load(const char *URI)
34 _SysDebug("Image_Load: Unable parse as URI '%s'", URI);
38 if( strcmp(uri->Proto, "file") == 0 )
42 fp = fopen(uri->Path, "rb");
44 _SysDebug("Image_Load: Unable to open '%s'", uri->Path);
49 fseek(fp, 0, SEEK_END);
51 fseek(fp, 0, SEEK_SET);
52 buf = malloc( filesize );
54 _SysDebug("Image_Load: malloc() failed!");
60 tmp = fread(buf, 1, filesize, fp);
62 _SysDebug("Image_Load: fread() failed - %i != filesize (%i)", tmp, filesize);
66 else if( strcmp(uri->Proto, "base64") == 0 )
68 // 4 bytes of base64 = 3 bytes of binary (base 256)
69 filesize = strlen( uri->Path ) * 3 / 4;
70 buf = malloc(filesize);
72 _SysDebug("Image_Load: malloc() failed!");
77 filesize = UnBase64((uint8_t*)buf, uri->Path, filesize);
81 _SysDebug("Image_Load: Unknow protocol '%s'", uri->Proto);
86 img = Image_SIF_Parse(buf, filesize);
90 _SysDebug("Image_Load: Unable to parse SIF from '%s'\n", URI);
98 * \brief Decode a Base64 value
100 int UnBase64(uint8_t *Dest, char *Src, int BufSize)
104 char *start_src = Src;
106 for( i = 0; i+2 < BufSize; i += 3 )
109 for( j = 0; j < 4; j++, Src ++ ) {
110 if('A' <= *Src && *Src <= 'Z')
111 val |= (*Src - 'A') << ((3-j)*6);
112 else if('a' <= *Src && *Src <= 'z')
113 val |= (*Src - 'a' + 26) << ((3-j)*6);
114 else if('0' <= *Src && *Src <= '9')
115 val |= (*Src - '0' + 52) << ((3-j)*6);
117 val |= 62 << ((3-j)*6);
119 val |= 63 << ((3-j)*6);
123 j --; // Ignore invalid characters
125 Dest[i ] = (val >> 16) & 0xFF;
126 Dest[i+1] = (val >> 8) & 0xFF;
127 Dest[i+2] = val & 0xFF;
133 Dest[i] = (val >> 16) & 0xFF;
135 Dest[i+1] = (val >> 8) & 0xFF;
137 return Src - start_src;