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);
63 else if( strcmp(uri->Proto, "base64") == 0 )
65 // 4 bytes of base64 = 3 bytes of binary (base 256)
66 filesize = strlen( uri->Path ) * 3 / 4;
67 buf = malloc(filesize);
69 _SysDebug("Image_Load: malloc() failed!");
74 filesize = UnBase64((uint8_t*)buf, uri->Path, filesize);
78 _SysDebug("Image_Load: Unknow protocol '%s'", uri->Proto);
83 img = Image_SIF_Parse(buf, filesize);
87 _SysDebug("Image_Load: Unable to parse SIF from '%s'\n", URI);
95 * \brief Decode a Base64 value
97 int UnBase64(uint8_t *Dest, char *Src, int BufSize)
101 char *start_src = Src;
103 for( i = 0; i+2 < BufSize; i += 3 )
106 for( j = 0; j < 4; j++, Src ++ ) {
107 if('A' <= *Src && *Src <= 'Z')
108 val |= (*Src - 'A') << ((3-j)*6);
109 else if('a' <= *Src && *Src <= 'z')
110 val |= (*Src - 'a' + 26) << ((3-j)*6);
111 else if('0' <= *Src && *Src <= '9')
112 val |= (*Src - '0' + 52) << ((3-j)*6);
114 val |= 62 << ((3-j)*6);
116 val |= 63 << ((3-j)*6);
120 j --; // Ignore invalid characters
122 Dest[i ] = (val >> 16) & 0xFF;
123 Dest[i+1] = (val >> 8) & 0xFF;
124 Dest[i+2] = val & 0xFF;
130 Dest[i] = (val >> 16) & 0xFF;
132 Dest[i+1] = (val >> 8) & 0xFF;
134 return Src - start_src;