File: fblogo.c

package info (click to toggle)
fblogo 0.3-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,316 kB
  • ctags: 30
  • sloc: ansic: 1,311; makefile: 61
file content (362 lines) | stat: -rw-r--r-- 9,984 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
  fblogo.c
  Generate linux_logo.h header file for use with the framebuffer boot logo
  Written by Daniel Ved´┐Ży <daniel@kongsberg.online.no> 1999
  Adapted by Gordon Fraser <gordon@freakzone.net> 2001/2002
  Released under the GPL
  
  11/12/2001: 	Fixed missing includes
  		Fixed ImageMagick-Bug
  		Updated outputted defines to match current kernel
	  	Updated low_color.h to match current kernel (2.4.15-pre3)
		Use ImageMagick ReadImage instead of ReadTIFFImage
		(Gordon Fraser <gordon@freakzone.net>)
  11/25/2001:   cast signed color info to unsigned char :)
		(Gordon Fraser <gordon@freakzone.net>)
  11/27/2001:	Dither & Reduce colors to get rid of artefacts
                Add 2.2/2.4 Kernel-Option
		(Gordon Fraser <gordon@freakzone.net>)
  03/07/2002:   Get rid of imagemagick, use libpng instead
                Reason: new libmagick versions kept makeing fblogo segfault :(
		(Gordon Fraser <gordon@freakzone.net>)        
  03/11/2002:   Check if png is a palette image                
*/

#include "fblogo.h"

/*------------------------------------------------------------------*/
int main(int argc, char** argv)
{
  FILE* inputfile = NULL;
  FILE* outputfile = NULL;
  char version = LINUX_24;
  char sourcefile[255];
  int i;
  
  verbose = 0;
      
  if(argc>5)
    {
      usage();
      exit(EXIT_FAILURE);
    }

  for ( i=1; i<argc; i++ ) 
    {
      if ( *argv[i] == '-' ) 
	{
	  switch ( *(argv[i]+1) ) 
	    {
	    case '2':
	      version = LINUX_22;
	      break;
	    case 'v':
	      verbose = 1;
	      break;
	    case 'h':
	      usage();
	      exit(EXIT_SUCCESS);
	      break;
	    default:
	      usage();
	      exit(EXIT_FAILURE);
	      break;
	    }
	} 
      else
	{
	  strcpy(sourcefile, argv[i]);
	  if ((inputfile = fopen(argv[i], "rb")) == (FILE *)NULL) 
	    {
	      fprintf(stderr, "fblogo error: cannot open file %s\n", argv[i]);
	      exit(EXIT_FAILURE);
	    }
	  if(++i<argc) 
	    {
	      strcpy(sourcefile, argv[i]);
	      if ((outputfile = fopen(argv[i], "w")) == (FILE *)NULL) 
		{
		  fprintf(stderr, "fblogo error: cannot open file %s\n", argv[i]);
		  exit(EXIT_FAILURE);
		}
	    }
	}
    }

  if (inputfile == (FILE *)NULL) 
    {
      if ((inputfile = fdopen(0, "rb")) == (FILE *)NULL) 
	{
	  fprintf(stderr, "fblogo error: cannot open stdin\n");
	  exit(EXIT_FAILURE);
	}
    }
      
  if(outputfile == (FILE *)NULL) 
    {
      verbose = 0;		/* No messages if output goes to stdout */
      if ((outputfile = fdopen(1, "w")) == (FILE *)NULL) 
	{
	  fprintf(stderr, "fblogo error: cannot open stdout\n");
	  exit(EXIT_FAILURE);
	}
    }

  // Now read the source image
  if(read_image(inputfile))
    {
      exit(EXIT_FAILURE);
    }
  
  // Write the resulting header file
  if(write_header(outputfile, version))
    {
      fprintf(stderr, "fblogo error: unable to write header - %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }

  if(verbose) 
    {
      printf("Generated %s successfully\n\n", sourcefile);
      printf("Remember to modify /usr/src/linux/drivers/video/fbcon.c:\n");
      printf(" Change \"#define LOGO_H 80\" to \"#define LOGO_H %li\"\n", height);
      printf(" Change \"#define LOGO_W 80\" to \"#define LOGO_W %li\"\n\n", width);
      printf("Then copy %s to /usr/src/linux/include/linux/linux_logo.h,\n", sourcefile);
      printf("and recompile the kernel.\n");
    }
  
  fclose(inputfile);
  fclose(outputfile);

  free(image_data);
  free(palette);
  exit(EXIT_SUCCESS);
}

/*------------------------------------------------------------------*/
void usage(void)
{
    printf("Usage:\n");
    printf("fblogo [-2] [-v] [-h] [inputfile [outputfile]]\n");
}


/*------------------------------------------------------------------*/
/* Based on libpng example.c and                                    */
/*          png2linuxlogo by Greg Roelofs <newt@pobox.com>          */
int read_image(FILE *fp)
{
   char buf[PNG_SIG_BYTES];
   int  i, bit_depth, color_type;
   int num_colours = 0;
   png_colorp png_palette;

   png_structp png_ptr;
   png_infop info_ptr;

   png_uint_32 rowbytes;
   png_bytepp row_pointers = NULL;

   // First check if file really is a png
   if (fread(buf, 1, PNG_SIG_BYTES, fp) != PNG_SIG_BYTES)
     {
       fprintf(stderr, "fblogo error: image is not a png\n");
       return ERR_NOPNG;
     }

   if(png_sig_cmp(buf, 0, PNG_SIG_BYTES)) 
     {
       fprintf(stderr, "fblogo error: image is not a png\n");
       return ERR_NOPNG;
     }

   // Now create png_struct and png_info
   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL, NULL, NULL);
   if (!png_ptr)
     {
       fprintf(stderr, "fblogo error reading png\n");
       return (ERR_PNGPTR);
     }
   
   info_ptr = png_create_info_struct(png_ptr);
   if (!info_ptr)
     {
       fprintf(stderr, "fblogo error reading png\n");
       png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
       return (ERR_PNGINFO);
     }

   
   png_init_io(png_ptr, fp);
   png_set_sig_bytes(png_ptr, PNG_SIG_BYTES);
   png_read_info(png_ptr, info_ptr);
   png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);

   if (color_type != PNG_COLOR_TYPE_PALETTE) {
     fprintf(stderr, "fblogo error: only palette PNGs supported\n");
     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
     return ERR_NOPNG;
   }
   
   if (bit_depth < 8)
     png_set_packing(png_ptr);   /* expand to 1 byte per pixel */
   
   png_get_PLTE(png_ptr, info_ptr, &png_palette, &num_colours);
   
   if (num_colours > 223) 
     {
       fprintf(stderr, "fblogo error: too many colors (%d); must be less than 224\n", num_colours);
       png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
       return ERR_COLNUM;
     }

   palette = malloc(sizeof(png_color) * num_colours);
   memcpy(palette, png_palette, sizeof(png_color) * num_colours);
   num_palette = num_colours;

   /* allocate space for the PNG image data */
   rowbytes = png_get_rowbytes(png_ptr, info_ptr);

   if ((image_data = (png_bytep)malloc(rowbytes*height)) == NULL) 
     {
       fprintf(stderr, "fblogo error: can't allocate image data\n");
       png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
       return ERR_PNGPTR;
     }
   if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) 
     {
       fprintf(stderr, "fblogo error: can't allocate row pointers\n");
       png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
       free(image_data);
       return ERR_PNGPTR;
     }

    /* set the individual row_pointers to point at the correct offsets */

    for (i = 0; i < height; i++)
      row_pointers[i] = image_data + i * rowbytes;
    
    png_read_image(png_ptr, row_pointers);

    png_read_end(png_ptr, info_ptr);
    
    /* clean up after the read, and free any memory allocated - REQUIRED */
    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    return 0;
}

/*------------------------------------------------------------------*/
int write_header(FILE* fd, char version)
{
  int i;
  time_t timestamp;
  char datestr[32];
  png_bytep pixel;
  
  if(version==LINUX_22 && verbose)
    printf("Creating for Linux kernel version 2.2.x\n");
  else if(version==LINUX_24 && verbose)
    printf("Creating for Linux kernel version 2.4.x/2.5.x\n");
  
  timestamp = time((time_t *)NULL);
  strftime(datestr, 32, "%Y/%m/%d %H:%M:%S", localtime(&timestamp));
  
  fprintf(fd, "/* $Id: linux_logo.h,v 1.5 %s jj Exp $
 * include/linux/linux_logo.h: This is a linux logo
 *                             to be displayed on boot.
 *
 * Copyright (C) 1996 Larry Ewing (lewing@isc.tamu.edu)
 * Copyright (C) 1996,1998 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 *
 * You can put anything here, but:
 * LINUX_LOGO_COLORS has to be less than 224\n", datestr);

    
  fprintf(fd, " * Generated by fblogo version %s\n *\n", VERSION);
  fprintf(fd, " *\n * Remember to modify drivers/video/fbcon.c:\n");
  fprintf(fd, " * Change \"#define LOGO_H 80\" to \"#define LOGO_H %li\"\n", height);
  fprintf(fd, " * Change \"#define LOGO_W 80\" to \"#define LOGO_W %li\"\n */\n\n", width);
  
  if(version == LINUX_24) 
    {
      fprintf(fd, "#ifndef __HAVE_ARCH_LINUX_LOGO\n");
      //      fprintf(fd, "#define LINUX_LOGO_COLORS %d\n", num_palette);
      fprintf(fd, "#define LINUX_LOGO_COLORS %d\n", 223);
      fprintf(fd, "#endif\n");
      
      fprintf(fd, "#ifdef INCLUDE_LINUX_LOGO_DATA\n");
      fprintf(fd, "#ifndef __HAVE_ARCH_LINUX_LOGO\n");
      
    } 
  else
    {
      fprintf(fd, "#if LINUX_LOGO_COLORS == %d\n", num_palette);
    }
  
  
  fprintf(fd, "unsigned char linux_logo_red[] __initdata = {");
  for(i=0;i<num_palette;i++)
    {
      if(i%8==0)
	fprintf(fd, "\n  ");
      fprintf(fd, "0x%2.2X",palette[i].red);
      if(i!=num_palette-1)
	fprintf(fd, ", ");
    }
  fprintf(fd,"\n};\n\n");
  
  
  
  fprintf(fd, "unsigned char linux_logo_green[] __initdata = {");
  for(i=0;i<num_palette;i++)
    {
      if(i%8==0)
	fprintf(fd, "\n  ");
      fprintf(fd, "0x%2.2X",palette[i].green);
      if(i!=num_palette-1)
	fprintf(fd, ", ");
    }
  fprintf(fd,"\n};\n\n");
  
  fprintf(fd, "unsigned char linux_logo_blue[] __initdata = {");
  for(i=0;i<num_palette;i++)
    {
      if(i % 8 == 0)
	fprintf(fd, "\n ");
      fprintf(fd, "0x%2.2X",palette[i].blue);
      if(i!=num_palette-1)
	fprintf(fd, ", ");
    }
  fprintf(fd,"\n};\n\n");
  
  fprintf(fd, "unsigned char linux_logo[] __initdata = {");
  
  pixel = image_data;
  for (i = 0; i < width*height; i++) 
    {
      if (i > 0)
	fprintf(fd, ",");
      if (i % 8 == 0)
	fprintf(fd, "\n ");
      fprintf(fd, " 0x%2.2X", (*pixel++) + 0x20);
    }
  
  if(version == LINUX_22) 
    {
      fprintf(fd,"\n};\n\n#endif\n\n");
      fprintf(fd, low_color_logo_22);
    } 
  else
    {
      fprintf(fd,"\n};\n\n#endif /* !__HAVE_ARCH_LINUX_LOGO */\n");
      fprintf(fd, low_color_logo_24);
    }
  
  return 0;
}