linux/scripts/pnmtologo.c
<<
>>
Prefs
   1
   2/*
   3 *  Convert a logo in ASCII PNM format to C source suitable for inclusion in
   4 *  the Linux kernel
   5 *
   6 *  (C) Copyright 2001-2003 by Geert Uytterhoeven <geert@linux-m68k.org>
   7 *
   8 *  --------------------------------------------------------------------------
   9 *
  10 *  This file is subject to the terms and conditions of the GNU General Public
  11 *  License. See the file COPYING in the main directory of the Linux
  12 *  distribution for more details.
  13 */
  14
  15#include <ctype.h>
  16#include <errno.h>
  17#include <stdarg.h>
  18#include <stdio.h>
  19#include <stdlib.h>
  20#include <string.h>
  21#include <unistd.h>
  22
  23
  24static const char *programname;
  25static const char *filename;
  26static const char *logoname = "linux_logo";
  27static const char *outputname;
  28static FILE *out;
  29
  30
  31#define LINUX_LOGO_MONO         1       /* monochrome black/white */
  32#define LINUX_LOGO_VGA16        2       /* 16 colors VGA text palette */
  33#define LINUX_LOGO_CLUT224      3       /* 224 colors */
  34#define LINUX_LOGO_GRAY256      4       /* 256 levels grayscale */
  35
  36static const char *logo_types[LINUX_LOGO_GRAY256+1] = {
  37    [LINUX_LOGO_MONO] = "LINUX_LOGO_MONO",
  38    [LINUX_LOGO_VGA16] = "LINUX_LOGO_VGA16",
  39    [LINUX_LOGO_CLUT224] = "LINUX_LOGO_CLUT224",
  40    [LINUX_LOGO_GRAY256] = "LINUX_LOGO_GRAY256"
  41};
  42
  43#define MAX_LINUX_LOGO_COLORS   224
  44
  45struct color {
  46    unsigned char red;
  47    unsigned char green;
  48    unsigned char blue;
  49};
  50
  51static const struct color clut_vga16[16] = {
  52    { 0x00, 0x00, 0x00 },
  53    { 0x00, 0x00, 0xaa },
  54    { 0x00, 0xaa, 0x00 },
  55    { 0x00, 0xaa, 0xaa },
  56    { 0xaa, 0x00, 0x00 },
  57    { 0xaa, 0x00, 0xaa },
  58    { 0xaa, 0x55, 0x00 },
  59    { 0xaa, 0xaa, 0xaa },
  60    { 0x55, 0x55, 0x55 },
  61    { 0x55, 0x55, 0xff },
  62    { 0x55, 0xff, 0x55 },
  63    { 0x55, 0xff, 0xff },
  64    { 0xff, 0x55, 0x55 },
  65    { 0xff, 0x55, 0xff },
  66    { 0xff, 0xff, 0x55 },
  67    { 0xff, 0xff, 0xff },
  68};
  69
  70
  71static int logo_type = LINUX_LOGO_CLUT224;
  72static unsigned int logo_width;
  73static unsigned int logo_height;
  74static struct color **logo_data;
  75static struct color logo_clut[MAX_LINUX_LOGO_COLORS];
  76static unsigned int logo_clutsize;
  77
  78static void die(const char *fmt, ...)
  79    __attribute__ ((noreturn)) __attribute ((format (printf, 1, 2)));
  80static void usage(void) __attribute ((noreturn));
  81
  82
  83static unsigned int get_number(FILE *fp)
  84{
  85    int c, val;
  86
  87    /* Skip leading whitespace */
  88    do {
  89        c = fgetc(fp);
  90        if (c == EOF)
  91            die("%s: end of file\n", filename);
  92        if (c == '#') {
  93            /* Ignore comments 'till end of line */
  94            do {
  95                c = fgetc(fp);
  96                if (c == EOF)
  97                    die("%s: end of file\n", filename);
  98            } while (c != '\n');
  99        }
 100    } while (isspace(c));
 101
 102    /* Parse decimal number */
 103    val = 0;
 104    while (isdigit(c)) {
 105        val = 10*val+c-'0';
 106        c = fgetc(fp);
 107        if (c == EOF)
 108            die("%s: end of file\n", filename);
 109    }
 110    return val;
 111}
 112
 113static unsigned int get_number255(FILE *fp, unsigned int maxval)
 114{
 115    unsigned int val = get_number(fp);
 116    return (255*val+maxval/2)/maxval;
 117}
 118
 119static void read_image(void)
 120{
 121    FILE *fp;
 122    unsigned int i, j;
 123    int magic;
 124    unsigned int maxval;
 125
 126    /* open image file */
 127    fp = fopen(filename, "r");
 128    if (!fp)
 129        die("Cannot open file %s: %s\n", filename, strerror(errno));
 130
 131    /* check file type and read file header */
 132    magic = fgetc(fp);
 133    if (magic != 'P')
 134        die("%s is not a PNM file\n", filename);
 135    magic = fgetc(fp);
 136    switch (magic) {
 137        case '1':
 138        case '2':
 139        case '3':
 140            /* Plain PBM/PGM/PPM */
 141            break;
 142
 143        case '4':
 144        case '5':
 145        case '6':
 146            /* Binary PBM/PGM/PPM */
 147            die("%s: Binary PNM is not supported\n"
 148                "Use pnmnoraw(1) to convert it to ASCII PNM\n", filename);
 149
 150        default:
 151            die("%s is not a PNM file\n", filename);
 152    }
 153    logo_width = get_number(fp);
 154    logo_height = get_number(fp);
 155
 156    /* allocate image data */
 157    logo_data = (struct color **)malloc(logo_height*sizeof(struct color *));
 158    if (!logo_data)
 159        die("%s\n", strerror(errno));
 160    for (i = 0; i < logo_height; i++) {
 161        logo_data[i] = malloc(logo_width*sizeof(struct color));
 162        if (!logo_data[i])
 163            die("%s\n", strerror(errno));
 164    }
 165
 166    /* read image data */
 167    switch (magic) {
 168        case '1':
 169            /* Plain PBM */
 170            for (i = 0; i < logo_height; i++)
 171                for (j = 0; j < logo_width; j++)
 172                    logo_data[i][j].red = logo_data[i][j].green =
 173                        logo_data[i][j].blue = 255*(1-get_number(fp));
 174            break;
 175
 176        case '2':
 177            /* Plain PGM */
 178            maxval = get_number(fp);
 179            for (i = 0; i < logo_height; i++)
 180                for (j = 0; j < logo_width; j++)
 181                    logo_data[i][j].red = logo_data[i][j].green =
 182                        logo_data[i][j].blue = get_number255(fp, maxval);
 183            break;
logo_height;   85    int '6':
  86
/* Binary PBM/PGM/PPM */  87    die = get_number(fp);
  88    do {
i = 0; i < logo_height; i++)
  89        j = 0; j < logo_width; j++)
  90        if 1(logo_data[i][j].bluea> = get_number255(fp, maxval);
  91           1 logo_data[i][j].blueen =
get_number255(fp, maxval);
  92        if 1(logo_data[i][j].red = get_number255(fp, maxval);
  93           1 /////ologo.c#L165" id="L165" class="lie" name="1L94">  94           1 do {1  95           1     1a href="+cologo.c#L165" id="L165" class="lie" name="1L96">  96           1     19pnmtologo.c#L87" id="L87" class="lin1e" name="1L97">  97           1     19omment">/* Skip leading whitespaceclosf="scripts/pnmtologo.c#L127" id="L127" class="lie" name="1L98">  98           1 } wh1le (fp,closffp);
  99        }
<1a hre1="scriologo.c#L165" id="L165" class="l2ne" name=2L100"> 100    } whil2 ( 101
i][

 (i, color   102     103    val bluea> = val blueen =
val blue =  104    while 2  95           2href=20pnmtologo.c#L176" id="L176" class="l2ne" name=2L106"> 106        i][

 (i, color   107        if2( 108          2 val bluea> = val blueen =
val blue =  109    }
 110    return2 111}
i][

 (i, color   112
 113static uns2gned 21=val" claal" class="sref">val bluea> = val blueen =
val bluea> = val blue;
 114{
 115    unsign2d int21pnmtologo.c#L176" id="L176" class="l2ne" name=2L116"> 116    return2(255*216"+code=logo="sref">i][

 (i, );
color  color   117}
 118
val bluea> = val bluea> = val blueen =
val blueen =
val blue = val blue;
 119static voi2  120{
 121     122    unsign2d int22ommenologo.c#L168" id="L168" class="l2ne" name=2L123"> 123    int 
 124    unsign2d intpan class="c" class="sref">magoutputname);
 125
val[outputname);
);
 126    [ 127    die("%s: Binary PNM is n: %cre>
, outputname);
strerror(errno));
 128    if (!<2 href2"+code=fp"} elsetologo.c#L168" id="L168" class="l2n" name="LL129"> 129        die[ 130
 131     132    magfputs
"%s: Binary PNM is n/*f="+code=filename" class="sref">outt[ 133    if ("%s: Binary PNM is n *  DO NOT EDIT THIS e=fp!f="+code=filename" class="sref">outt[ 134        "%s: Binary PNM is n *f="+code=filename" class="sref">outt[ 135    magfintf, 1, 2)));
outt[filename,  136    switch2("%s: Binary PNM is n *f="+code=filename" class="sref">outt[ 137        ca2e fp =ntf, 1, 2)));
outt[clasname,  138        ca2e fp,puts
"%s: Binary PNM is n */\nf="+code=filename" class="sref">outt[ 139        ca2e fp,puts
"%s: Binary PNM is n#includego_wilinux/linux_ass="h>\nf="+code=filename" class="sref">outt[ 140          2 fp =ntf, 1, 2)));
outt[ 141          2 brea2;
clasname,  142
 143        ca2e  144        ca2e  145        ca2e  146          2 "%s: Binary PNM is n\n};\nf="+code=filename" class="sref">outt[ 147          2 fp =ntf, 1, 2)));
outt[clasname,  148          2     24e (fp,pntf, 1, 2)));
outt[clasode=Ls
j].clasode=L
 149
fp,pntf, 1, 2)));
outt[clasoo_width;  150        de2ault:25class="cos="sref">fp =ntf, 1, 2)));
outt[clasogo_height;  151          2 magclasode=L
val 124<"scrip
 152    }
fp =ntf, 1, 2)));
outt[clasologo_clutsize;
 153    fp =ntf, 1, 2)));
outt[clasname,  154     155
fp =ntf, 1, 2)));
outt[clasname,  156    "%s: Binary PNM is n};\nf="+code=filename" class="sref">outt[ 157     158    if (!<2 href2"+code=log* Skip leading whitespaceclosf=clas="scripts/pnmtologo.c#L127" id="L127" class="l2ne" name=2L159"> 159        magoutputname);
 160    for (<2 href260 fp closffpoutt[ 161         162        if2(! 163          2 i, wrref_f=x_cntt[ 164    }
logo_height;  165
blue 166     167    switch2(magwrref_f=x_cntt[ 168        ca2e fp =ntf, 1, 2)));
outt[ 169          2 magwrref_f=x_cntt[ 170          2 for 270 fp =ntf, 1, 2)));
outt[ 171          2     2or ( 172          2     27  fp =ntf, 1, 2)));
outt[ 173          2     27=logo_width" class="srefwrref_f=x_cntt[ 174          2 brea27="+coologo.c#L165" id="L165" class="l2ne" name=2L175"> 175
 176        ca2e ));
 177          2  178          2 i, j;
 179          2 for 2bluea>+( 180          2     28pnmtologo.c#L131" id="L131" class="l2ne" name=2L181"> 181          2     28omment">/* check file type and reamaxid>
 182          2     2       i = 0; i < logo_height; i++)
 183          2 brea2;
j = 0; j < logo_width; j++)
log_data[i][j].log_data[i][j].  85    int fpa>("%s: Binary PNM is nIhref=must bf=mo/achromef="+code=filenatologo.c#L49" id="L49" class="lin2e" name="2L86">  86
  87    /* Skip leading whitespacewrref  88    do {
fpwrref_f="scr;
  89          90        if 2(  91           2 i = 0; i < logo_height; i++)
  92        if 2(j = 0; j < logo_width;   93           2 /class="sref">j =a>+(j &(vallogo_width; j++)
(  94           2 do {2mag">log_data[i][j].bluea> =   95           2     29 class="strinhref="+c hres="sref">fpa>+val(  96           2     29class="comment">/*e" class="srefwrref_f=xt[fpa>+  97           2     29f="+code=die"ologo.c#L165" id="L165" class="l2e" name="2L98">  98           2 } wh2le (  99        }
<2a hre29pnmtologo.c#L150" id="L150" class="l3ne" name=3L100"> 100    } whil3 ( 101
/*e" class="srefwrref_footcr;
 102     103     104    while 3 116  95           3href=30 clasologo.c#L168" id="L168" class="l3ne" name=3L106"> 106        i, j;
j;
e 107        if3(bluea>+  98           3   99        }
<3ef="s309 (
 110    return3i = 0; i < logo_height; i++) {
 111}
j = 0; j < logo_width; j++)
 112
j =ej &ej &e 113static uns3gned 31class="comment">/ode=c" class="sref">mag);
mag">log_data[i][j].j;
scrievgaL116"> 116i][e 114{
 115    unsign3d int31 class="strinhrefc" class="sref">mage 116    return3(255*31class="comment">/t">/*e" class="srefa>("%s: Binary PNM is nIhref=must use the 16M\n&sohree=clus only="scripts/pnmtologo.c#L148" id="L148" class="l3n" name="3L117"> 117}
/t">/*ot;Use pnmnoraw(1) to convert ipmqu 118
&qt">/*ot;Use pnmnoraw(1) to convofee=clusf="+code=filenatologo.c#L49" id="L49" class="lin3n" name="3L119"> 119static voi3  120{
 121    /* check file type and reawrref 122    unsign3d int32=magic" class="sref">magwrref_f="scr;
 123    int  124    unsign3d int3an class="* Skip leading whitespacewrref 125
i = 0; i < logo_height; i++) {
 126    j = 0; j < logo_width; j++)
 127    j =ej &ej &e 128    if (!<3 href32pan class="string">&qc" class="sref">mag);
mag">log_data[i][j].j;
scrievgaL116"> 116i][e 129         130
j =a>+i][e 131    j < logo_width;  132    j =ej &ej &e 133    if (/ode=">&qc" class="sref">mag);
mag">log_data[i][j].j;
scrievgaL116"> 116i][e 134         135    fpa>+val 136    switch3(/=logo.c#L49" id="L49" class="lin3n" name="3L137"> 137        ca3e die[fpa>+ 138        ca3e  139        ca3e  140          3  141          3 brea34omment">/*e" class="srefwrref_footcr;
 142
 143        ca3e  144        ca3e  124<"scripwrref_+codescri2L1 claspnmtologo.c#L120" id="L120" class="l3ne" name=3L145"> 145        ca3e  146          3 i, j;
j;
e 147          3  148          3     34+code=log* Skip leading whitespacemaxid>
 149
i = 0; i < logo_height; i++) {
 150        de3ault:35r (j = 0; j < logo_width; j++)
 151          3 j =ej &eloglogo_clutsize;
j &e 152    }
mag);
mag">log_data[i][j].j;
ref="scritsize;
i][e 153    /ode=">&q/pnmtologo.c#L184" id="L184" class="l3ne" name=3L154"> 154    mageloglogo_clutsize;
 155
mag">loglogo_clutsize;
;
 156    /t">/t">/*e" class="srefa>("%s: Binary PNM is nIhref=has more th;&# 157    /t">/=log* Skip leadinraw(1) to convert ipmqu 158    if (!<3 href35pan class="string">&qt">/t">/*e" class="srefMAX_
 159        j;
ref="scritsize;
i][">loglogo_clutsize;
i][">log_data[i][j]. 160    for (<3 href36r ( 161         162        if3(! 163          3  164    }
 165
 166     167    switch3(i = 0; i < logo_height; i++) {
 168        ca3e j = 0; j < logo_width; j++)
 169          3 j =ej &eloglogo_clutsize;
j &e 170          3 for 37r (mag);
mag">log_data[i][j].j;
ref="scritsize;
i][e 171          3     37
 172          3     37  die[fpe 173          3     37class="commen=logo.c#L49" id="L49" class="lin3ne" name=3L174"> 174          3 brea37=logo_height" class="srefputs
"%s: Binary PNM is n\n};\nf="+code=filename" class="sref">outt[ 175
 176        ca3e  177          3 fp =ntf, 1, 2)));
outt[ 178          3 j;
ref=name,  179          3 for 3die[ 180          3     38+code=i" class="sref">i = 0; i < lologo_clutsize;
j &)
 181          3     38
die[fpref="scritsize;
i][bluea> =  182          3     38  fpwrref_f=xt[fpref="scritsize;
i][blueen =
 183          3 brea383 fpwrref_f=xt[fpref="scritsize;
i][blue;
  85    int   86
  87    fpwrref_footcr;
  88    do {
  89          90        if 3( 156<"scripwrref_+codehref2L1 claspnmtologo.c#L120" id="L120" class="l3e" name="3L91">  91           3   92        if 3(i, j;
  93           3   94           3 do {39n class="* Skip leading whitespacemaxid>
  95           3     39code=val"class="sref">i = 0; i < logo_height; i++) {
  96           3     39class="commenclass="sref">j = 0; j < logo_width; j++)
  97           3     39f="+code=die" clao_data" class="sref");
mag">log_data[i][j].  98           3 } wh39pan class="string">&q*e" class="srefa>("%s: Binary PNM is nIhref=must bf=hrefscalef="+code=filenatologo.c#L49" id="L49" class="lin3ee" name=3L99">  99        }
<3a hre39pnmtologo.c#L150" id="L150" class="l4ne" name=4L100"> 100    } whil4 ( 101
/*e" class="srefwrref_f="scr;
 102     103     104    while 4i = 0; i < logo_height; i++) {
  95           4href=405lass="commenclass="sref">j = 0; j < logo_width; j++)
 106        /*e" class="srefwrref_f=xt[fp">log_data[i][j].bluea> =  107        if4(  98           4   99        }
<4ef="s40a href="+ss="sref">die 110    return4 111}
 112
&qode=fmt" cl*s="sref">fp might;  113static uns4gned 41classologo.c#L168" id="L168" class="l4ne" name=4L114"> 114{
 115    unsign4d int41pnmtologo.c#L176" id="L176" class="l4ne" name=4L116"> 116    return4(255*41f="+code=ght" class="sreva_+coright; fpapght; fmight;  117}
fpv =ntf, 1, 2)));
stscrr;
fmight; apght;  118
fpva_en = fpapght;  119static voi4  120{
fpext( 121     122    unsign4d int42pnmtologo.c#L143" id="L143" class="l4ne" name=4L123"> 123    int  124    unsign4d int4L1 claologo.c#L168" id="L168" class="l4ne" name=4L125"> 125
"%s: Binary PNM is nf="+code=filenalogo.c#L168" id="L168" class="l4ne" name=4L126"> 126     127     128    if (!<4 href42pan class="stuot;%s: Binary PNM is nVaxid optide=:f="+code=filenalogo.c#L168" id="L168" class="l4n9 name="4L129"> 129        /: display this usref inclamatidef="+code=filenalogo.c#L168" id="L168" class="l4ne" name=4L130"> 130
/: specify 131     132    /: specify 133    if (/: mo/achrome a hre/ hreff="+code=filenalogo.c#L168" id="L168" class="l4ne" name=4L134"> 134        /: 16M\nclus VGA text paleteff="+code=filenalogo.c#L168" id="L168" class="l4ne" name=4L135"> 135     136    switch4( 137        ca4e prohremname,  138        ca4e  139        ca4e  140          4 i, maia> =
&qlass="sref">i, argcght; fpargvght;  141          4 brea44f="+cologo.c#L168" id="L168" class="l4ne" name=4L142"> 142
i, optght;  143        ca4e  144        ca4e i][argvght;  145        ca4e  146          4  147          4  148          4     44pan class="stu"sref">i, optght; i][getoptght; fpargcght; i][argvght; );
 149
magoptght;  150        de4ault:45r ( 151          4  152    }
magoptght;  153    /casequot;r");
<#39;h<#39;=filena:logo.c#L168" id="L168" class="l4ne" name=4L154"> 154    i, usref/a>( 155
 156     157    );
<#39;n<#39;=filena:logo.c#L168" id="L168" class="l4ne" name=4L158"> 158    if (!<4 href45pan class="string">&qs="sref">fp">loname, i][optargght; , optght; /a>         461);
<#39;n<#39;=filena:logo.c#L168" id"L49" cla4s="lin3ne" name=3L162"> 462fp">lonacput p"srei][optargght;  463 464; /a>         465);
<#39;n<#39;=filena:logo.c#L168" id""L184" class="l3ne" name=3L166"> 466fpasecms/a>; i][optargght;>; );
 467fp">log_data/: fp">log_dize;
;  468fpasecms/a>; i][optargght;>; );
 469fp">log_data/: j;
scrievgaize;
/a>;  470fpasecms/a>; i][optargght;>; );
 471fp">log_data/:  472fpasecms/a>; i][optargght;>; );
 473fp">log_data/:  474 475i, usref/a>( 476; /a>         477 478 479i, usref/a>( 480; /a>         481 482 483fpva_e cli"s     !"prohremnamepnmt cl=="sref">fpargcghti, usref/a>(  454   int   4648         4 fme [optihref="prohremnamepnmt cl="sref">i][argvghtcode=die" clac" cli"sref">fpva_e cli"s       4748       if4(  4848fpva_eptgd_itesp(  4948         "s45  /: ">fp">log_data/: ;   4049         3 fof="+co"clasnameet_n ize;
fp">log_dize;
;l  if4(  4149         3     37o"clasnameet_n L156"> 156fp">log_dL156"> 156  4249         3     37optght; /a>          434          3   444          2 dof="+co"clasnameet_n ize;
j;
scrievgaize;
/a>;l  if4(  454          2     29o"clasnameet_n L156"> 156<="srefwrref_+codevgaL116"> 116  464          2     29optght; /a>          4749       if4(  484          3 } f="+co"clasnameet_n ize;
;
;l  if4(  4949         3  156<="srefwrref_+codescri2L124"> 124<"scripwrref_ref="+cusref clastologo.c#L49" id5="L150" c5ass="l4ne" name=4L100"> 500 501 502;
;
;l  if4( 503 156<="srefwrref_+codehref2L156"> 156<"scripwrref_ref="+cusref clastologo.c#L49" id5=4L143" c5a="lin3e" name="3L94">  504  555          f=36
 506fpext(  507  585          3   595      
d> The original LXR software byud>LXR =logunity/a>;>;ment"experig* Sal version byu 3 @s= lx>@s= ;.
d> lx>.s= Redpill L= pro AS/a>;>;providane=luL=