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) |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 6 | ** (C) 2007-2008 Mike Frysinger <vapier@gentoo.org> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 7 | ** |
| 8 | ** This is still under construction! |
| 9 | */ |
| 10 | |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 11 | #include <errno.h> |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 12 | #include <getopt.h> |
| 13 | #include <stdbool.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Mike Frysinger | fc78ad1 | 2007-12-18 03:23:25 -0500 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 17 | #include <unistd.h> |
| 18 | #include <sys/stat.h> |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 19 | |
| 20 | #pragma pack(1) |
| 21 | |
| 22 | /*#define ENABLE_ASCII_BANNERS */ |
| 23 | |
| 24 | typedef struct { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 25 | unsigned char id; |
| 26 | unsigned char ColorMapType; |
| 27 | unsigned char ImageTypeCode; |
| 28 | unsigned short ColorMapOrigin; |
| 29 | unsigned short ColorMapLenght; |
| 30 | unsigned char ColorMapEntrySize; |
| 31 | unsigned short ImageXOrigin; |
| 32 | unsigned short ImageYOrigin; |
| 33 | unsigned short ImageWidth; |
| 34 | unsigned short ImageHeight; |
| 35 | unsigned char ImagePixelSize; |
| 36 | unsigned char ImageDescriptorByte; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 37 | } tga_header_t; |
| 38 | |
| 39 | typedef struct { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 40 | unsigned char r, g, b; |
| 41 | } rgb_t; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 42 | |
| 43 | typedef struct { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 44 | unsigned char b, g, r; |
| 45 | } bgr_t; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 46 | |
| 47 | typedef struct { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 48 | unsigned char Cb, y1, Cr, y2; |
| 49 | } yuyv_t; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 50 | |
| 51 | typedef struct { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 52 | void *data, *palette; |
| 53 | int width, height, pixels, bpp, pixel_size, size, palette_size, yuyv; |
| 54 | } image_t; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 55 | |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 56 | void *xmalloc (size_t size) |
| 57 | { |
| 58 | void *ret = malloc (size); |
| 59 | if (!ret) { |
| 60 | fprintf (stderr, "\nerror: malloc(%zu) failed: %s", |
| 61 | size, strerror(errno)); |
| 62 | exit (1); |
| 63 | } |
| 64 | return ret; |
| 65 | } |
| 66 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 67 | void StringUpperCase (char *str) |
| 68 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 69 | int count = strlen (str); |
| 70 | char c; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 71 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 72 | while (count--) { |
| 73 | c = *str; |
| 74 | if ((c >= 'a') && (c <= 'z')) |
| 75 | *str = 'A' + (c - 'a'); |
| 76 | str++; |
| 77 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void StringLowerCase (char *str) |
| 81 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 82 | int count = strlen (str); |
| 83 | char c; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 84 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 85 | while (count--) { |
| 86 | c = *str; |
| 87 | if ((c >= 'A') && (c <= 'Z')) |
| 88 | *str = 'a' + (c - 'A'); |
| 89 | str++; |
| 90 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 91 | } |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 92 | void pixel_rgb_to_yuyv (rgb_t * rgb_pixel, yuyv_t * yuyv_pixel) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 93 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 94 | unsigned int pR, pG, pB; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 95 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 96 | /* Transform (0-255) components to (0-100) */ |
| 97 | pR = rgb_pixel->r * 100 / 255; |
| 98 | pG = rgb_pixel->g * 100 / 255; |
| 99 | pB = rgb_pixel->b * 100 / 255; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 100 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 101 | /* Calculate YUV values (0-255) from RGB beetween 0-100 */ |
| 102 | yuyv_pixel->y1 = yuyv_pixel->y2 = 209 * (pR + pG + pB) / 300 + 16; |
| 103 | yuyv_pixel->Cb = pB - (pR / 4) - (pG * 3 / 4) + 128; |
| 104 | yuyv_pixel->Cr = pR - (pG * 3 / 4) - (pB / 4) + 128; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 105 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 106 | return; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 109 | void printlogo_rgb (rgb_t * data, int w, int h) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 110 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 111 | int x, y; |
| 112 | |
| 113 | for (y = 0; y < h; y++) { |
| 114 | for (x = 0; x < w; x++, data++) |
| 115 | if ((data->r < |
| 116 | 30) /*&&(data->g == 0)&&(data->b == 0) */ ) |
| 117 | printf (" "); |
| 118 | else |
| 119 | printf ("X"); |
| 120 | printf ("\n"); |
| 121 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void printlogo_yuyv (unsigned short *data, int w, int h) |
| 125 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 126 | int x, y; |
| 127 | |
| 128 | for (y = 0; y < h; y++) { |
| 129 | for (x = 0; x < w; x++, data++) |
| 130 | if (*data == 0x1080) /* Because of inverted on i386! */ |
| 131 | printf (" "); |
| 132 | else |
| 133 | printf ("X"); |
| 134 | printf ("\n"); |
| 135 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Mike Frysinger | 3fa2b52 | 2007-12-18 04:29:55 -0500 | [diff] [blame] | 138 | static inline unsigned short le16_to_cpu (unsigned short val) |
| 139 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 140 | union { |
| 141 | unsigned char pval[2]; |
| 142 | unsigned short val; |
| 143 | } swapped; |
| 144 | |
| 145 | swapped.val = val; |
| 146 | return (swapped.pval[1] << 8) + swapped.pval[0]; |
Mike Frysinger | 3fa2b52 | 2007-12-18 04:29:55 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 149 | int image_load_tga (image_t * image, char *filename) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 150 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 151 | FILE *file; |
| 152 | tga_header_t header; |
| 153 | int i; |
| 154 | unsigned char app; |
| 155 | rgb_t *p; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 156 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 157 | if ((file = fopen (filename, "rb")) == NULL) |
| 158 | return -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 159 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 160 | fread (&header, sizeof (header), 1, file); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 161 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 162 | /* byte swap: tga is little endian, host is ??? */ |
| 163 | header.ColorMapOrigin = le16_to_cpu (header.ColorMapOrigin); |
| 164 | header.ColorMapLenght = le16_to_cpu (header.ColorMapLenght); |
| 165 | header.ImageXOrigin = le16_to_cpu (header.ImageXOrigin); |
| 166 | header.ImageYOrigin = le16_to_cpu (header.ImageYOrigin); |
| 167 | header.ImageWidth = le16_to_cpu (header.ImageWidth); |
| 168 | header.ImageHeight = le16_to_cpu (header.ImageHeight); |
Mike Frysinger | 3fa2b52 | 2007-12-18 04:29:55 -0500 | [diff] [blame] | 169 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 170 | image->width = header.ImageWidth; |
| 171 | image->height = header.ImageHeight; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 172 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 173 | switch (header.ImageTypeCode) { |
| 174 | case 2: /* Uncompressed RGB */ |
| 175 | image->yuyv = 0; |
| 176 | image->palette_size = 0; |
| 177 | image->palette = NULL; |
| 178 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 179 | |
| 180 | default: |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 181 | printf ("Format not supported!\n"); |
| 182 | return -1; |
| 183 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 184 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 185 | image->bpp = header.ImagePixelSize; |
| 186 | image->pixel_size = ((image->bpp - 1) / 8) + 1; |
| 187 | image->pixels = image->width * image->height; |
| 188 | image->size = image->pixels * image->pixel_size; |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 189 | image->data = xmalloc (image->size); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 190 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 191 | if (image->bpp != 24) { |
| 192 | printf ("Bpp not supported: %d!\n", image->bpp); |
| 193 | return -1; |
| 194 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 195 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 196 | fread (image->data, image->size, 1, file); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 197 | |
| 198 | /* Swapping R and B values */ |
| 199 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 200 | p = image->data; |
| 201 | for (i = 0; i < image->pixels; i++, p++) { |
| 202 | app = p->r; |
| 203 | p->r = p->b; |
| 204 | p->b = app; |
| 205 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 206 | |
| 207 | /* Swapping image */ |
| 208 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 209 | if (!(header.ImageDescriptorByte & 0x20)) { |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 210 | unsigned char *temp = xmalloc (image->size); |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 211 | int linesize = image->pixel_size * image->width; |
| 212 | void *dest = image->data, |
| 213 | *source = temp + image->size - linesize; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 214 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 215 | printf ("S"); |
| 216 | if (temp == NULL) { |
| 217 | printf ("Cannot alloc temp buffer!\n"); |
| 218 | return -1; |
| 219 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 220 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 221 | memcpy (temp, image->data, image->size); |
| 222 | for (i = 0; i < image->height; |
| 223 | i++, dest += linesize, source -= linesize) |
| 224 | memcpy (dest, source, linesize); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 225 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 226 | free (temp); |
| 227 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 228 | #ifdef ENABLE_ASCII_BANNERS |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 229 | printlogo_rgb (image->data, image->width, image->height); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 230 | #endif |
| 231 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 232 | fclose (file); |
| 233 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 236 | void image_free (image_t * image) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 237 | { |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 238 | free (image->data); |
| 239 | free (image->palette); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 242 | int image_rgb_to_yuyv (image_t * rgb_image, image_t * yuyv_image) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 243 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 244 | rgb_t *rgb_ptr = (rgb_t *) rgb_image->data; |
| 245 | yuyv_t yuyv; |
| 246 | unsigned short *dest; |
| 247 | int count = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 248 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 249 | yuyv_image->pixel_size = 2; |
| 250 | yuyv_image->bpp = 16; |
| 251 | yuyv_image->yuyv = 1; |
| 252 | yuyv_image->width = rgb_image->width; |
| 253 | yuyv_image->height = rgb_image->height; |
| 254 | yuyv_image->pixels = yuyv_image->width * yuyv_image->height; |
| 255 | yuyv_image->size = yuyv_image->pixels * yuyv_image->pixel_size; |
| 256 | dest = (unsigned short *) (yuyv_image->data = |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 257 | xmalloc (yuyv_image->size)); |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 258 | yuyv_image->palette = 0; |
| 259 | yuyv_image->palette_size = 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 260 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 261 | while ((count++) < rgb_image->pixels) { |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 262 | pixel_rgb_to_yuyv (rgb_ptr++, &yuyv); |
| 263 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 264 | if ((count & 1) == 0) /* Was == 0 */ |
| 265 | memcpy (dest, ((void *) &yuyv) + 2, sizeof (short)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 266 | else |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 267 | memcpy (dest, (void *) &yuyv, sizeof (short)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 268 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 269 | dest++; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #ifdef ENABLE_ASCII_BANNERS |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 273 | printlogo_yuyv (yuyv_image->data, yuyv_image->width, |
| 274 | yuyv_image->height); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 275 | #endif |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 276 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 279 | int use_gzip = 0; |
| 280 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 281 | int image_save_header (image_t * image, char *filename, char *varname) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 282 | { |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 283 | FILE *file = fopen (filename, "w"); |
| 284 | char app[256], str[256] = "", def_name[64]; |
| 285 | int count = image->size, col = 0; |
| 286 | unsigned char *dataptr = image->data; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 287 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 288 | if (file == NULL) |
| 289 | return -1; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 290 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 291 | /* Author information */ |
| 292 | fprintf (file, |
| 293 | "/*\n * Generated by EasyLogo, (C) 2000 by Paolo Scaffardi\n *\n"); |
| 294 | fprintf (file, |
| 295 | " * To use this, include it and call: easylogo_plot(screen,&%s, width,x,y)\n *\n", |
| 296 | varname); |
| 297 | fprintf (file, |
| 298 | " * Where:\t'screen'\tis the pointer to the frame buffer\n"); |
| 299 | fprintf (file, " *\t\t'width'\tis the screen width\n"); |
| 300 | fprintf (file, " *\t\t'x'\t\tis the horizontal position\n"); |
| 301 | fprintf (file, " *\t\t'y'\t\tis the vertical position\n */\n\n"); |
| 302 | |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 303 | /* gzip compress */ |
| 304 | if (use_gzip & 0x1) { |
| 305 | const char *errstr = NULL; |
| 306 | unsigned char *compressed; |
| 307 | struct stat st; |
| 308 | FILE *gz; |
| 309 | char *gzfilename = xmalloc(strlen (filename) + 20); |
| 310 | char *gzcmd = xmalloc(strlen (filename) + 20); |
| 311 | |
| 312 | sprintf (gzfilename, "%s.gz", filename); |
| 313 | sprintf (gzcmd, "gzip > %s", gzfilename); |
| 314 | gz = popen (gzcmd, "w"); |
| 315 | if (!gz) { |
| 316 | errstr = "\nerror: popen() failed"; |
| 317 | goto done; |
| 318 | } |
| 319 | if (fwrite (image->data, image->size, 1, gz) != 1) { |
| 320 | errstr = "\nerror: writing data to gzip failed"; |
| 321 | goto done; |
| 322 | } |
| 323 | if (pclose (gz)) { |
| 324 | errstr = "\nerror: gzip process failed"; |
| 325 | goto done; |
| 326 | } |
| 327 | |
| 328 | gz = fopen (gzfilename, "r"); |
| 329 | if (!gz) { |
| 330 | errstr = "\nerror: open() on gzip data failed"; |
| 331 | goto done; |
| 332 | } |
| 333 | if (stat (gzfilename, &st)) { |
| 334 | errstr = "\nerror: stat() on gzip file failed"; |
| 335 | goto done; |
| 336 | } |
| 337 | compressed = xmalloc (st.st_size); |
| 338 | if (fread (compressed, st.st_size, 1, gz) != 1) { |
| 339 | errstr = "\nerror: reading gzip data failed"; |
| 340 | goto done; |
| 341 | } |
| 342 | fclose (gz); |
| 343 | |
| 344 | unlink (gzfilename); |
| 345 | |
| 346 | dataptr = compressed; |
| 347 | count = st.st_size; |
| 348 | fprintf (file, "#define EASYLOGO_ENABLE_GZIP %i\n\n", count); |
| 349 | if (use_gzip & 0x2) |
| 350 | fprintf (file, "static unsigned char EASYLOGO_DECOMP_BUFFER[%i];\n\n", image->size); |
| 351 | |
| 352 | done: |
| 353 | free (gzfilename); |
| 354 | free (gzcmd); |
| 355 | |
| 356 | if (errstr) { |
| 357 | perror (errstr); |
| 358 | return -1; |
| 359 | } |
| 360 | } |
| 361 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 362 | /* Headers */ |
| 363 | fprintf (file, "#include <video_easylogo.h>\n\n"); |
| 364 | /* Macros */ |
| 365 | strcpy (def_name, varname); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 366 | StringUpperCase (def_name); |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 367 | fprintf (file, "#define DEF_%s_WIDTH\t\t%d\n", def_name, |
| 368 | image->width); |
| 369 | fprintf (file, "#define DEF_%s_HEIGHT\t\t%d\n", def_name, |
| 370 | image->height); |
| 371 | fprintf (file, "#define DEF_%s_PIXELS\t\t%d\n", def_name, |
| 372 | image->pixels); |
| 373 | fprintf (file, "#define DEF_%s_BPP\t\t%d\n", def_name, image->bpp); |
| 374 | fprintf (file, "#define DEF_%s_PIXEL_SIZE\t%d\n", def_name, |
| 375 | image->pixel_size); |
| 376 | fprintf (file, "#define DEF_%s_SIZE\t\t%d\n\n", def_name, |
| 377 | image->size); |
| 378 | /* Declaration */ |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 379 | fprintf (file, "unsigned char DEF_%s_DATA[] = {\n", |
| 380 | def_name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 381 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 382 | /* Data */ |
| 383 | while (count) |
| 384 | switch (col) { |
| 385 | case 0: |
| 386 | sprintf (str, " 0x%02x", *dataptr++); |
| 387 | col++; |
| 388 | count--; |
| 389 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 390 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 391 | case 16: |
| 392 | fprintf (file, "%s", str); |
| 393 | if (count > 0) |
| 394 | fprintf (file, ","); |
| 395 | fprintf (file, "\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 396 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 397 | col = 0; |
| 398 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 399 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 400 | default: |
| 401 | strcpy (app, str); |
| 402 | sprintf (str, "%s, 0x%02x", app, *dataptr++); |
| 403 | col++; |
| 404 | count--; |
| 405 | break; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | if (col) |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 409 | fprintf (file, "%s\n", str); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 410 | |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 411 | /* End of declaration */ |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 412 | fprintf (file, "};\n\n"); |
| 413 | /* Variable */ |
| 414 | fprintf (file, "fastimage_t %s = {\n", varname); |
| 415 | fprintf (file, " DEF_%s_DATA,\n", def_name); |
| 416 | fprintf (file, " DEF_%s_WIDTH,\n", def_name); |
| 417 | fprintf (file, " DEF_%s_HEIGHT,\n", def_name); |
| 418 | fprintf (file, " DEF_%s_BPP,\n", def_name); |
| 419 | fprintf (file, " DEF_%s_PIXEL_SIZE,\n", def_name); |
| 420 | fprintf (file, " DEF_%s_SIZE\n};\n", def_name); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 421 | |
| 422 | fclose (file); |
| 423 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 424 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | #define DEF_FILELEN 256 |
| 428 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 429 | static void usage (int exit_status) |
| 430 | { |
| 431 | puts ( |
| 432 | "EasyLogo 1.0 (C) 2000 by Paolo Scaffardi\n" |
| 433 | "\n" |
| 434 | "Syntax: easylogo [options] inputfile [outputvar [outputfile]]\n" |
| 435 | "\n" |
| 436 | "Options:\n" |
| 437 | " -r Output RGB instead of YUYV\n" |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 438 | " -g Compress with gzip\n" |
| 439 | " -b Preallocate space in bss for decompressing image\n" |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 440 | " -h Help output\n" |
| 441 | "\n" |
| 442 | "Where: 'inputfile' is the TGA image to load\n" |
| 443 | " 'outputvar' is the variable name to create\n" |
| 444 | " 'outputfile' is the output header file (default is 'inputfile.h')" |
| 445 | ); |
| 446 | exit (exit_status); |
| 447 | } |
| 448 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 449 | int main (int argc, char *argv[]) |
| 450 | { |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 451 | int c; |
| 452 | bool use_rgb = false; |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 453 | char inputfile[DEF_FILELEN], |
| 454 | outputfile[DEF_FILELEN], varname[DEF_FILELEN]; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 455 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 456 | image_t rgb_logo, yuyv_logo; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 457 | |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 458 | while ((c = getopt(argc, argv, "hrgb")) > 0) { |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 459 | switch (c) { |
| 460 | case 'h': |
| 461 | usage (0); |
| 462 | break; |
| 463 | case 'r': |
| 464 | use_rgb = true; |
| 465 | puts ("Using 24-bit RGB Output Fromat"); |
| 466 | break; |
Mike Frysinger | a885a54 | 2008-12-30 03:15:38 -0500 | [diff] [blame] | 467 | case 'g': |
| 468 | use_gzip |= 0x1; |
| 469 | puts ("Compressing with gzip"); |
| 470 | break; |
| 471 | case 'b': |
| 472 | use_gzip |= 0x2; |
| 473 | puts ("Preallocating bss space for decompressing image"); |
| 474 | break; |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 475 | default: |
| 476 | usage (1); |
| 477 | break; |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 478 | } |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 479 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 480 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 481 | c = argc - optind; |
| 482 | if (c > 4 || c < 1) |
| 483 | usage (1); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 484 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 485 | strcpy (inputfile, argv[optind]); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 486 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 487 | if (c > 1) |
| 488 | strcpy (varname, argv[optind + 1]); |
| 489 | else { |
| 490 | /* transform "input.tga" to just "input" */ |
| 491 | char *dot; |
| 492 | strcpy (varname, inputfile); |
| 493 | dot = strchr (varname, '.'); |
| 494 | if (dot) |
| 495 | *dot = '\0'; |
| 496 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 497 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 498 | if (c > 2) |
| 499 | strcpy (outputfile, argv[optind + 2]); |
| 500 | else { |
| 501 | /* just append ".h" to input file name */ |
| 502 | strcpy (outputfile, inputfile); |
| 503 | strcat (outputfile, ".h"); |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 504 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 505 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 506 | /* Make sure the output is sent as soon as we printf() */ |
| 507 | setbuf(stdout, NULL); |
| 508 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 509 | printf ("Doing '%s' (%s) from '%s'...", |
| 510 | outputfile, varname, inputfile); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 511 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 512 | /* Import TGA logo */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 513 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 514 | printf ("L"); |
| 515 | if (image_load_tga (&rgb_logo, inputfile) < 0) { |
| 516 | printf ("input file not found!\n"); |
| 517 | exit (1); |
| 518 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 519 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 520 | /* Convert it to YUYV format if wanted */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 521 | |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 522 | if (!use_rgb) { |
| 523 | printf ("C"); |
| 524 | image_rgb_to_yuyv (&rgb_logo, &yuyv_logo); |
| 525 | } |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 526 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 527 | /* Save it into a header format */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 528 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 529 | printf ("S"); |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 530 | image_save_header (use_rgb ? &rgb_logo : &yuyv_logo, outputfile, varname); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 531 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 532 | /* Free original image and copy */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 533 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 534 | image_free (&rgb_logo); |
Mike Frysinger | ba5d52d | 2008-02-16 02:40:18 -0500 | [diff] [blame] | 535 | if (!use_rgb) |
| 536 | image_free (&yuyv_logo); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 537 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 538 | printf ("\n"); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 539 | |
Wolfgang Denk | cc3c2600 | 2008-01-09 15:14:46 +0100 | [diff] [blame] | 540 | return 0; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 541 | } |