Usermode/AxWin3 - Clean up unneeded (and silly) log message
[tpg/acess2.git] / Usermode / Applications / imageview_src / main.c
1 /*
2  * Acess2 Image Viewer
3  * By John Hodge (thePowersGang)
4  */
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <acess/sys.h>
9 #include <uri.h>
10
11 // === PROTOTYPES ===
12  int    main(int argc, char *argv[]);
13 void    *SIF_Load(tURIFile *FP, int *Width, int *Height, uint32_t Magic);
14
15 // === CONSTANTS ===
16 const struct {
17         const char      *Name;
18         uint32_t        Mask;
19         uint32_t        Ident;
20         void    *(*Load)(tURIFile *FP, int *Width, int *Height, uint32_t Magic);
21 }       caFileTypes[] = {
22         {"SIF", 0xFFFF, 0x51F0, SIF_Load}
23         };
24 #define NUM_FILE_TYPES  (sizeof(caFileTypes)/sizeof(caFileTypes[0]))
25
26 // === CODE ===
27 int main(int argc, char *argv[])
28 {
29         char    *imageURI;
30         tURI    *uri;
31         tURIFile        *fp;
32         uint32_t        idDWord = 0;
33          int    i;
34         uint32_t        *buffer;
35          int    w, h;
36          int    termW, termH;
37          int    tmp;
38         
39         // --- Parse Arguments
40         if( argc < 2 ) {
41                 fprintf(stderr, "Usage: %s <image>\n", argv[0]);
42                 return 0;
43         }
44         imageURI = argv[1];
45         
46         // --- Open File
47         uri = URI_Parse(imageURI);
48         printf("uri = {Proto:'%s',Host:'%s',Port:%i('%s'),Path:'%s'}\n",
49                 uri->Proto, uri->Host, uri->PortNum, uri->PortStr, uri->Path);
50         fp = URI_Open(URI_MODE_READ, uri);
51         printf("fp = %p\n", fp);
52         if(!fp) {
53                 fprintf(stderr, "ERROR: Unable to open '%s'\nFile unable to open\n", imageURI);
54                 return -1;
55         }
56         free(uri);
57         
58         
59         // --- Determine file type
60         i = URI_Read(fp, 4, &idDWord);
61         printf("i = %i\n", i);
62         printf("idDWord = 0x%08lx\n", idDWord);
63         for( i = 0; i < NUM_FILE_TYPES; i++ )
64         {
65                 if( (idDWord & caFileTypes[i].Mask) == caFileTypes[i].Ident )
66                         break;
67         }
68         if( i == NUM_FILE_TYPES ) {
69                 fprintf(stderr, "ERROR: Unable to open '%s'\nUnknown file type\n", imageURI);
70                 return -2;
71         }
72         
73         // --- Load
74         buffer = caFileTypes[i].Load(fp, &w, &h, idDWord);
75         if( !buffer ) {
76                 fprintf(stderr, "ERROR: Unable to open '%s'\nParsing failed\n", imageURI);
77                 return -3;
78         }
79         
80         printf("w=%i,h=%i\n", w, h);
81         
82         // --- Display
83         termW = ioctl(1, 5, NULL);      termW *= 8;     ioctl(1, 5, &termW);
84         termH = ioctl(1, 6, NULL);      termH *= 16;    ioctl(1, 6, &termH);
85         //printf("termW = %i, termH = %i\n", termW, termH);
86         tmp = 1;        ioctl(1, 4, &tmp);
87         
88         seek(1, 0, SEEK_SET);
89         for( i = 0; i < (h < termH ? h : termH); i++ )
90         {
91                 write(1, (w < termW ? w : termW) * 4, buffer + i*w);
92                 if(w < termW)
93                         seek(1, (termW-w)*4, SEEK_CUR);
94         }
95         
96         for( ;; );
97         
98         return 0;
99 }
100
101 // === Simple Image Format Loader ===
102 void *SIF_Load(tURIFile *FP, int *Width, int *Height, uint32_t Magic)
103 {
104         uint16_t        flags = Magic >> 16;
105         uint16_t        w, h;
106          int    comp = flags & 3;
107          int    ofs, i;
108         uint32_t        *ret;
109         
110         // First 4 bytes were read by main()
111         URI_Read(FP, 2, &w);
112         URI_Read(FP, 2, &h);
113         
114         ret = malloc(w * h * 4);
115         
116         switch(comp)
117         {
118         // Uncompressed 32-bpp data
119         case 0:
120                 URI_Read( FP, w * h * 4, ret );
121                 *Width = w;
122                 *Height = h;
123                 return ret;
124         
125         // 1.7.32 RLE
126         // (1 Flag, 7-bit size, 32-bit value)
127         case 1:
128                 ofs = 0;
129                 while( ofs < w*h )
130                 {
131                         uint8_t len;
132                         uint32_t        val;
133                         URI_Read(FP, 1, &len);
134                         if(len & 0x80) {
135                                 len &= 0x7F;
136                                 URI_Read(FP, len, ret+ofs);
137                                 ofs += len;
138                         }
139                         else {
140                                 URI_Read(FP, 4, &val);
141                                 while(len--)    ret[ofs++] = val;
142                         }
143                 }
144                 return ret;
145         
146         case 3:
147                 // Alpha
148                 ofs = 0;
149                 while( ofs < w*h )
150                 {
151                         uint8_t len, val;
152                         URI_Read(FP, 1, &len);
153                         if(len & 0x80) {
154                                 len &= 0x7F;
155                                 while(len--) {
156                                         URI_Read(FP, 1, &val);
157                                         ret[ofs++] = (uint32_t)val << 24;
158                                 }
159                         }
160                         else {
161                                 URI_Read(FP, 1, &val);
162                                 while(len--)
163                                         ret[ofs++] = (uint32_t)val << 24;
164                         }
165                 }
166                 // Red, Green, Blue
167                 for( i = 0; i < 4; i++ )
168                 {
169                         ofs = 0;
170                         while( ofs < w*h )
171                         {
172                                 uint8_t len, val;
173                                 URI_Read(FP, 1, &len);
174                                 if(len & 0x80) {
175                                         len &= 0x7F;
176                                         while(len--) {
177                                                 URI_Read(FP, 1, &val);
178                                                 ret[ofs++] |= (uint32_t)val << (24-i*8);
179                                         }
180                                 }
181                                 else {
182                                         URI_Read(FP, 1, &val);
183                                         while(len--)
184                                                 ret[ofs++] |= (uint32_t)val << (24-i*8);
185                                 }
186                         }
187                 }
188                 return ret;
189         
190         default:
191                 fprintf(stderr, "Warning: Unknown compression scheme %i for SIF\n", comp);
192                 return NULL;
193         }
194         
195 }

UCC git Repository :: git.ucc.asn.au