File: ppm2c.c

package info (click to toggle)
thrust 0.89c-3.5
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 2,080 kB
  • ctags: 996
  • sloc: ansic: 20,409; sh: 2,483; makefile: 332
file content (193 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download | duplicates (7)
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

/* Written by Peter Ekberg, peda@lysator.liu.se */

#ifdef HAVE_CONFIG_H
#include "../src/config.h"
#endif

#if defined(HAVE_GETOPT_H) && defined(HAVE_GETOPT_LONG_ONLY)
# include <getopt.h>
#elif !defined(HAVE_GETOPT_LONG_ONLY)
# include "../src/getopt.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <pnm.h>

pixel **picture;
int cols;
int rows;
pixval maxval;

int maxcolors = 256;

pixel *colors;

char *name = NULL;

int
extractcolors(pixel **picture, pixel *colors)
{
  int c=0;
  int x=0, y=0;
  int i;
  pixel p;

  while(c<maxcolors && y<rows) {
    p = picture[y][x];
    for(i=0; i<c; i++)
      if(PPM_EQUAL(p, colors[i]))
	break;
    if(i==c) {
      colors[c] = p;
      c++;
    }
    if(c==maxcolors)
      return(0);
    if(++x == cols) {
      ++y;
      x=0;
    }
  }
  return(c);
}

void
write_c(FILE *f, pixel **picture, pixel *colors,
	int cols, int rows, int maxcolors)
{
  int x=0, y=0;
  int i;
  pixel p;

  fprintf(f,
	  "\n/* Automatically generated by the program ppm2c version " VERSION
	  "\n   written by Peter Ekberg, peda@lysator.liu.se */\n");

  fprintf(f, "\nunsigned int %s_nr_colors = %d;", name, maxcolors);
  fprintf(f, "\nunsigned int %s_cols = %d;", name, cols);
  fprintf(f, "\nunsigned int %s_rows = %d;\n", name, rows);
  fprintf(f, "\nunsigned char %s_colors[] = {\n\t", name);
  for(i=0; i<maxcolors; i++) {
    p = colors[i];
    fprintf(f, "0x%02x, 0x%02x, 0x%02x",
	    PPM_GETR(p), PPM_GETG(p), PPM_GETB(p));
    if(i != maxcolors-1)
      fprintf(f, ",\n\t");
  }
  fprintf(f, "};\n\nunsigned char %s_pixels[] = {\n\t", name);
  while(y<rows) {
    p = picture[y][x];
    for(i=0; i<maxcolors; i++)
      if(PPM_EQUAL(p, colors[i])) {
	fprintf(f, "0x%02x", i);
	break;
      }
    if(++x == cols) {
      ++y;
      x=0;
    }
    if(y<rows) {
      if(x%8==0)
	fprintf(f, ",\n\t");
      else
	fprintf(f, ", ");
    }
  }
  fprintf(f, " };\n");
}

int
string_to_int(int *i, char *s)
{
  int v;
  char *end;

  if(!*s)
    return(-1);

  v = strtol(s, &end, 10);

  if(*end)
    return(-1);

  *i = v;
  return(0);
}

int
main(int argc, char *argv[])
{
  int optc;
  int c;

  do {
    static struct option longopts[] = {
      { "version",   0, 0, 'v' },
      { "help",      0, 0, 'h' },
      { "maxcolors", 1, 0, 'm' },
      { "name",      1, 0, 'n' },
      { 0,           0, 0,  0  },
    };

    optc = getopt_long(argc, argv, "vhm:n:", longopts, (int *)0);
    switch(optc) {
    case -1:
      break;
    case 'v':
      printf("ppm2c: version " VERSION "\n");
      exit(0);
    case 'h':
      printf("ppm2c: version " VERSION "\n");
      printf("options:\n"
	     "  -v --version   Display the version.\n"
	     "  -h --help      Display this text.\n"
	     "  -m --maxcolors Maximum allowed colors (default 256).\n"
	     "  -n --name      The base name of the C identifiers.\n");
      exit(0);
    case 'm':
      if(string_to_int(&maxcolors, optarg) || maxcolors<1 || maxcolors>256) {
	fprintf(stderr, "ppm2c: illegal argument to option '-m'\n");
	exit(1);
      }
      break;
    case 'n':
      name = strdup(optarg);
      if(!name) {
	fprintf(stderr, "ppm2c: insufficient memory\n");
	exit(1);
      }
      break;
    default:
      fprintf(stderr, "ppm2c: bad usage (see 'ppm2c --help')\n");
      exit(1);
    }
  } while(optc != -1);

  if(name == NULL) {
    fprintf(stderr,
	    "ppm2c: must specify a base name for the C identifiers.\n");
    exit(1);
  }

  picture = ppm_readppm(stdin, &cols, &rows, &maxval);

  colors = (pixel *)malloc(maxcolors * sizeof(pixel));

  if(!colors) {
    fprintf(stderr, "ppm2c: insufficient memory\n");
    exit(1);
  }

  c = extractcolors(picture, colors);

  if(!c) {
    fprintf(stderr, "ppm2c: to many colors in picture\n");
    exit(1);
  }

  write_c(stdout, picture, colors, cols, rows, c);

  return(0);
}