wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Easylogo TGA->header converter |
| 3 | ** ============================== |
| 4 | ** (C) 2000 by Paolo Scaffardi (arsenio@tin.it) |
| 5 | ** AIRVENT SAM s.p.a - RIMINI(ITALY) |
| 6 | ** |
| 7 | ** This is still under construction! |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 13 | |
| 14 | #pragma pack(1) |
| 15 | |
| 16 | /*#define ENABLE_ASCII_BANNERS */ |
| 17 | |
| 18 | typedef struct { |
| 19 | unsigned char id; |
| 20 | unsigned char ColorMapType; |
| 21 | unsigned char ImageTypeCode; |
| 22 | unsigned short ColorMapOrigin; |
| 23 | unsigned short ColorMapLenght; |
| 24 | unsigned char ColorMapEntrySize; |
| 25 | unsigned short ImageXOrigin; |
| 26 | unsigned short ImageYOrigin; |
| 27 | unsigned short ImageWidth; |
| 28 | unsigned short ImageHeight; |
| 29 | unsigned char ImagePixelSize; |
| 30 | unsigned char ImageDescriptorByte; |
| 31 | } tga_header_t; |
| 32 | |
| 33 | typedef struct { |
| 34 | unsigned char r,g,b ; |
| 35 | } rgb_t ; |
| 36 | |
| 37 | typedef struct { |
| 38 | unsigned char b,g,r ; |
| 39 | } bgr_t ; |
| 40 | |
| 41 | typedef struct { |
| 42 | unsigned char Cb,y1,Cr,y2; |
| 43 | } yuyv_t ; |
| 44 | |
| 45 | typedef struct { |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 46 | void *data, |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 47 | *palette ; |
| 48 | int width, |
| 49 | height, |
| 50 | pixels, |
| 51 | bpp, |
| 52 | pixel_size, |
| 53 | size, |
| 54 | palette_size, |
| 55 | yuyv; |
| 56 | } image_t ; |
| 57 | |
| 58 | void StringUpperCase (char *str) |
| 59 | { |
| 60 | int count = strlen(str); |
| 61 | char c ; |
| 62 | |
| 63 | while(count--) |
| 64 | { |
| 65 | c=*str; |
| 66 | if ((c >= 'a')&&(c<='z')) |
| 67 | *str = 'A' + (c-'a'); |
| 68 | str++ ; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void StringLowerCase (char *str) |
| 73 | { |
| 74 | int count = strlen(str); |
| 75 | char c ; |
| 76 | |
| 77 | while(count--) |
| 78 | { |
| 79 | c=*str; |
| 80 | if ((c >= 'A')&&(c<='Z')) |
| 81 | *str = 'a' + (c-'A'); |
| 82 | str++ ; |
| 83 | } |
| 84 | } |
| 85 | void pixel_rgb_to_yuyv (rgb_t *rgb_pixel, yuyv_t *yuyv_pixel) |
| 86 | { |
| 87 | unsigned int pR, pG, pB ; |
| 88 | |
| 89 | /* Transform (0-255) components to (0-100) */ |
| 90 | pR = rgb_pixel->r * 100 / 255 ; |
| 91 | pG = rgb_pixel->g * 100 / 255 ; |
| 92 | pB = rgb_pixel->b * 100 / 255 ; |
| 93 | |
| 94 | /* Calculate YUV values (0-255) from RGB beetween 0-100 */ |
| 95 | yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16 ; |
| 96 | yuyv_pixel->Cb = pB - (pR/4) - (pG*3/4) + 128 ; |
| 97 | yuyv_pixel->Cr = pR - (pG*3/4) - (pB/4) + 128 ; |
| 98 | |
| 99 | return ; |
| 100 | } |
| 101 | |
| 102 | void printlogo_rgb (rgb_t *data, int w, int h) |
| 103 | { |
| 104 | int x,y; |
| 105 | for (y=0; y<h; y++) |
| 106 | { |
| 107 | for (x=0; x<w; x++, data++) |
| 108 | if ((data->r < 30)/*&&(data->g == 0)&&(data->b == 0)*/) |
| 109 | printf(" "); |
| 110 | else |
| 111 | printf("X"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 112 | printf("\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | void printlogo_yuyv (unsigned short *data, int w, int h) |
| 117 | { |
| 118 | int x,y; |
| 119 | for (y=0; y<h; y++) |
| 120 | { |
| 121 | for (x=0; x<w; x++, data++) |
| 122 | if (*data == 0x1080) /* Because of inverted on i386! */ |
| 123 | printf(" "); |
| 124 | else |
| 125 | printf("X"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 126 | printf("\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | int image_load_tga (image_t *image, char *filename) |
| 131 | { |
| 132 | FILE *file ; |
| 133 | tga_header_t header ; |
| 134 | int i; |
| 135 | unsigned char app ; |
| 136 | rgb_t *p ; |
| 137 | |
| 138 | if( ( file = fopen( filename, "rb" ) ) == NULL ) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 139 | return -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 140 | |
| 141 | fread(&header, sizeof(header), 1, file); |
| 142 | |
| 143 | image->width = header.ImageWidth ; |
| 144 | image->height = header.ImageHeight ; |
| 145 | |
| 146 | switch (header.ImageTypeCode){ |
| 147 | case 2: /* Uncompressed RGB */ |
| 148 | image->yuyv = 0 ; |
| 149 | image->palette_size = 0 ; |
| 150 | image->palette = NULL ; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 151 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 152 | |
| 153 | default: |
| 154 | printf("Format not supported!\n"); |
| 155 | return -1 ; |
| 156 | } |
| 157 | |
| 158 | image->bpp = header.ImagePixelSize ; |
| 159 | image->pixel_size = ((image->bpp-1) / 8) + 1 ; |
| 160 | image->pixels = image->width * image->height; |
| 161 | image->size = image->pixels * image->pixel_size ; |
| 162 | image->data = malloc(image->size) ; |
| 163 | |
| 164 | if (image->bpp != 24) |
| 165 | { |
| 166 | printf("Bpp not supported: %d!\n", image->bpp); |
| 167 | return -1 ; |
| 168 | } |
| 169 | |
| 170 | fread(image->data, image->size, 1, file); |
| 171 | |
| 172 | /* Swapping R and B values */ |
| 173 | |
| 174 | p = image->data ; |
| 175 | for(i=0; i < image->pixels; i++, p++) |
| 176 | { |
| 177 | app = p->r ; |
| 178 | p->r = p->b ; |
| 179 | p->b = app ; |
| 180 | } |
| 181 | |
| 182 | /* Swapping image */ |
| 183 | |
| 184 | if(!(header.ImageDescriptorByte & 0x20)) |
| 185 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 186 | unsigned char *temp = malloc(image->size); |
| 187 | int linesize = image->pixel_size * image->width ; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 188 | void *dest = image->data, |
| 189 | *source = temp + image->size - linesize ; |
| 190 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 191 | printf("S"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 192 | if (temp == NULL) |
| 193 | { |
| 194 | printf("Cannot alloc temp buffer!\n"); |
| 195 | return -1; |
| 196 | } |
| 197 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 198 | memcpy(temp, image->data, image->size); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 199 | for(i = 0; i<image->height; i++, dest+=linesize, source-=linesize) |
| 200 | memcpy(dest, source, linesize); |
| 201 | |
| 202 | free( temp ); |
| 203 | } |
| 204 | |
| 205 | #ifdef ENABLE_ASCII_BANNERS |
| 206 | printlogo_rgb (image->data,image->width, image->height); |
| 207 | #endif |
| 208 | |
| 209 | fclose (file); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int image_free (image_t *image) |
| 214 | { |
| 215 | if(image->data != NULL) |
| 216 | free(image->data); |
| 217 | |
| 218 | if(image->palette != NULL) |
| 219 | free(image->palette); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | int image_rgb_to_yuyv (image_t *rgb_image, image_t *yuyv_image) |
| 225 | { |
| 226 | rgb_t *rgb_ptr = (rgb_t *) rgb_image->data ; |
| 227 | yuyv_t yuyv ; |
| 228 | unsigned short *dest ; |
| 229 | int count = 0 ; |
| 230 | |
| 231 | yuyv_image->pixel_size = 2 ; |
| 232 | yuyv_image->bpp = 16 ; |
| 233 | yuyv_image->yuyv = 1 ; |
| 234 | yuyv_image->width = rgb_image->width ; |
| 235 | yuyv_image->height = rgb_image->height ; |
| 236 | yuyv_image->pixels = yuyv_image->width * yuyv_image->height ; |
| 237 | yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size ; |
| 238 | dest = (unsigned short *) (yuyv_image->data = malloc(yuyv_image->size)) ; |
| 239 | yuyv_image->palette = 0 ; |
| 240 | yuyv_image->palette_size= 0 ; |
| 241 | |
| 242 | while((count++) < rgb_image->pixels) |
| 243 | { |
| 244 | pixel_rgb_to_yuyv (rgb_ptr++, &yuyv); |
| 245 | |
| 246 | if ((count & 1)==0) /* Was == 0 */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 247 | memcpy (dest, ((void *)&yuyv) + 2, sizeof(short)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 248 | else |
| 249 | memcpy (dest, (void *)&yuyv, sizeof(short)); |
| 250 | |
| 251 | dest ++ ; |
| 252 | } |
| 253 | |
| 254 | #ifdef ENABLE_ASCII_BANNERS |
| 255 | printlogo_yuyv (yuyv_image->data, yuyv_image->width, yuyv_image->height); |
| 256 | #endif |
| 257 | return 0 ; |
| 258 | } |
| 259 | |
| 260 | int image_save_header (image_t *image, char *filename, char *varname) |
| 261 | { |
| 262 | FILE *file = fopen (filename, "w"); |
| 263 | char app[256], str[256]="", def_name[64] ; |
| 264 | int count = image->size, col=0; |
| 265 | unsigned char *dataptr = image->data ; |
| 266 | if (file==NULL) |
| 267 | return -1 ; |
| 268 | |
wdenk | cc1e256 | 2003-03-06 13:39:27 +0000 | [diff] [blame] | 269 | /* Author information */ |
Marian Balakowicz | 0b76bfb | 2006-09-01 19:44:05 +0200 | [diff] [blame] | 270 | fprintf(file, "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 271 | fprintf(file, " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n", varname); |
| 272 | fprintf(file, " * Where:\t'screen'\tis the pointer to the frame buffer\n"); |
| 273 | fprintf(file, " *\t\t'width'\tis the screen width\n"); |
| 274 | fprintf(file, " *\t\t'x'\t\tis the horizontal position\n"); |
| 275 | fprintf(file, " *\t\t'y'\t\tis the vertical position\n */\n\n"); |
| 276 | |
| 277 | /* Headers */ |
| 278 | fprintf(file, "#include <video_easylogo.h>\n\n"); |
| 279 | /* Macros */ |
| 280 | strcpy(def_name, varname); |
| 281 | StringUpperCase (def_name); |
| 282 | fprintf(file, "#define DEF_%s_WIDTH\t\t%d\n", def_name, image->width); |
| 283 | fprintf(file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name, image->height); |
| 284 | fprintf(file, "#define DEF_%s_PIXELS\t\t%d\n", def_name, image->pixels); |
| 285 | fprintf(file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp); |
| 286 | fprintf(file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name, image->pixel_size); |
| 287 | fprintf(file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name, image->size); |
| 288 | /* Declaration */ |
| 289 | fprintf(file, "unsigned char DEF_%s_DATA[DEF_%s_SIZE] = {\n", def_name, def_name); |
| 290 | |
| 291 | /* Data */ |
| 292 | while(count) |
| 293 | switch (col){ |
| 294 | case 0: |
| 295 | sprintf(str, " 0x%02x", *dataptr++); |
| 296 | col++; |
| 297 | count-- ; |
| 298 | break; |
| 299 | |
| 300 | case 16: |
| 301 | fprintf(file, "%s", str); |
| 302 | if (count > 0) |
| 303 | fprintf(file,","); |
| 304 | fprintf(file, "\n"); |
| 305 | |
| 306 | col = 0 ; |
| 307 | break; |
| 308 | |
| 309 | default: |
| 310 | strcpy(app, str); |
| 311 | sprintf(str, "%s, 0x%02x", app, *dataptr++); |
| 312 | col++ ; |
| 313 | count-- ; |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | if (col) |
| 318 | fprintf(file, "%s\n", str); |
| 319 | |
| 320 | /* End of declaration */ |
| 321 | fprintf(file, "};\n\n"); |
| 322 | /* Variable */ |
| 323 | fprintf(file, "fastimage_t %s = {\n", varname); |
| 324 | fprintf(file, " DEF_%s_DATA,\n", def_name); |
| 325 | fprintf(file, " DEF_%s_WIDTH,\n", def_name); |
| 326 | fprintf(file, " DEF_%s_HEIGHT,\n", def_name); |
| 327 | fprintf(file, " DEF_%s_BPP,\n", def_name); |
| 328 | fprintf(file, " DEF_%s_PIXEL_SIZE,\n", def_name); |
| 329 | fprintf(file, " DEF_%s_SIZE\n};\n", def_name); |
| 330 | |
| 331 | fclose (file); |
| 332 | |
| 333 | return 0 ; |
| 334 | } |
| 335 | |
| 336 | #define DEF_FILELEN 256 |
| 337 | |
| 338 | int main (int argc, char *argv[]) |
| 339 | { |
| 340 | char |
| 341 | inputfile[DEF_FILELEN], |
| 342 | outputfile[DEF_FILELEN], |
| 343 | varname[DEF_FILELEN]; |
| 344 | |
| 345 | image_t rgb_logo, yuyv_logo ; |
| 346 | |
| 347 | switch (argc){ |
| 348 | case 2: |
| 349 | case 3: |
| 350 | case 4: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 351 | strcpy (inputfile, argv[1]); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 352 | |
| 353 | if (argc > 2) |
| 354 | strcpy (varname, argv[2]); |
| 355 | else |
| 356 | { |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 357 | char *dot = strchr(inputfile, '.'); |
| 358 | int pos = dot - inputfile; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 359 | |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 360 | if (dot) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 361 | { |
| 362 | strncpy (varname, inputfile, pos); |
| 363 | varname[pos] = 0 ; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (argc > 3) |
| 368 | strcpy (outputfile, argv[3]); |
| 369 | else |
| 370 | { |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 371 | char *dot = strchr (varname, '.'); |
| 372 | int pos = dot - varname; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 373 | |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 374 | if (dot) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 375 | { |
| 376 | char app[DEF_FILELEN] ; |
| 377 | |
| 378 | strncpy(app, varname, pos); |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame^] | 379 | app[pos] = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 380 | sprintf(outputfile, "%s.h", app); |
| 381 | } |
| 382 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 383 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 384 | |
| 385 | default: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 386 | printf("EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 387 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 388 | printf("Syntax: easylogo inputfile [outputvar {outputfile}] \n"); |
| 389 | printf("\n"); |
| 390 | printf("Where: 'inputfile' is the TGA image to load\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 391 | printf(" 'outputvar' is the variable name to create\n"); |
| 392 | printf(" 'outputfile' is the output header file (default is 'inputfile.h')\n"); |
| 393 | |
| 394 | return -1 ; |
| 395 | } |
| 396 | |
| 397 | printf("Doing '%s' (%s) from '%s'...", |
| 398 | outputfile, varname, inputfile); |
| 399 | |
| 400 | /* Import TGA logo */ |
| 401 | |
| 402 | printf("L"); |
| 403 | if (image_load_tga (&rgb_logo, inputfile)<0) |
| 404 | { |
| 405 | printf("input file not found!\n"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 406 | exit(1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Convert it to YUYV format */ |
| 410 | |
| 411 | printf("C"); |
| 412 | image_rgb_to_yuyv (&rgb_logo, &yuyv_logo) ; |
| 413 | |
| 414 | /* Save it into a header format */ |
| 415 | |
| 416 | printf("S"); |
| 417 | image_save_header (&yuyv_logo, outputfile, varname) ; |
| 418 | |
| 419 | /* Free original image and copy */ |
| 420 | |
| 421 | image_free (&rgb_logo); |
| 422 | image_free (&yuyv_logo); |
| 423 | |
| 424 | printf("\n"); |
| 425 | |
| 426 | return 0 ; |
| 427 | } |