~kroq-gar78/ubuntu/precise/ace-of-penguins/fix-978446

« back to all changes in this revision

Viewing changes to .pc/20-lib--make-imglib.c-closedir.patch/lib/make-imglib.c

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona, Artur Rona, Manfred Hampl
  • Date: 2010-10-31 12:40:19 UTC
  • mfrom: (5.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20101031124019-igmqv4os4wlkxs1r
Tags: 1.3-1ubuntu1
[ Artur Rona ]
* Merge from debian unstable.  Remaining changes:
  - debian/patches/11-include-imagelib.patch:
    + Fix implicit pointer conversion.

[ Manfred Hampl ]
* Add missing information for spider game (available since 1.3):
  - debian/*.man, debian/*.pod, debian/menu:
    + Information about spider added. (LP: #643336)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <string.h>
 
4
#include <sys/types.h>
 
5
#include <sys/stat.h>
 
6
#include <unistd.h>
 
7
#include <dirent.h>
 
8
 
 
9
#include <png.h>
 
10
 
 
11
static int verbose = 0;
 
12
static char *basename = "images";
 
13
static char *imagedir = ".";
 
14
 
 
15
/*****************************************************************************/
 
16
 
 
17
char *
 
18
concat (char *a, char *b)
 
19
{
 
20
  char *buf = 0;
 
21
  int i = strlen(a) + strlen(b) + 2;
 
22
  buf = (char *)malloc(i);
 
23
  sprintf(buf, "%s/%s", a, b);
 
24
  return buf;
 
25
}
 
26
 
 
27
/*****************************************************************************/
 
28
 
 
29
typedef struct subimage_struct {
 
30
  struct subimage_struct *next;
 
31
  char *filename;
 
32
  int w, h, max;
 
33
  int index;
 
34
} subimage_struct;
 
35
 
 
36
typedef struct image_struct {
 
37
  struct image_struct *next;
 
38
  char *name;
 
39
  int a, d;
 
40
  subimage_struct *sub[3];
 
41
  int index;
 
42
} image_struct;
 
43
 
 
44
image_struct *image_list = 0;
 
45
int current_index = 0;
 
46
 
 
47
void
 
48
scan_image_directory ()
 
49
{
 
50
  DIR *dir;
 
51
  struct dirent *de;
 
52
  image_struct *img;
 
53
  subimage_struct *sub;
 
54
 
 
55
  dir = opendir(imagedir);
 
56
  if (!dir) {
 
57
    fprintf(stderr, "Unable to scan directory %s for images.\n", imagedir);
 
58
    perror("The error was");
 
59
    exit(1);
 
60
  }
 
61
  while (de = readdir(dir)) {
 
62
    FILE *f;
 
63
    char *dot;
 
64
    png_structp png_ptr;
 
65
    png_infop info_ptr;
 
66
    png_uint_32 width, height;
 
67
    int bit_depth, color_type, interlace_type, type;
 
68
 
 
69
    dot = strrchr(de->d_name, '.');
 
70
    if (!dot)
 
71
      continue;
 
72
    if (strcmp(dot, ".png"))
 
73
      continue;
 
74
 
 
75
    dot = strchr(de->d_name, '.');
 
76
    *dot = 0;
 
77
    for (img=image_list; img; img=img->next)
 
78
      if (strcmp(img->name, de->d_name) == 0)
 
79
        break;
 
80
    if (!img)
 
81
      continue;
 
82
    *dot = '.';
 
83
 
 
84
    f = fopen(concat(imagedir, de->d_name), "rb");
 
85
 
 
86
    png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
 
87
    info_ptr = png_create_info_struct (png_ptr);
 
88
 
 
89
    if (setjmp (png_ptr->jmpbuf)) {
 
90
      fclose (f);
 
91
      continue;
 
92
    }
 
93
 
 
94
    png_init_io (png_ptr, f);
 
95
 
 
96
    png_read_info (png_ptr, info_ptr);
 
97
 
 
98
    png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth,
 
99
                  &color_type, &interlace_type, 0, 0);
 
100
 
 
101
    if (bit_depth == 1)
 
102
      type = 0;
 
103
    else if (color_type & PNG_COLOR_MASK_COLOR)
 
104
      type = 2;
 
105
    else
 
106
      type = 1;
 
107
 
 
108
    sub = (subimage_struct *)malloc(sizeof(subimage_struct));
 
109
    sub->next = img->sub[type];
 
110
    img->sub[type] = sub;
 
111
    sub->filename = strdup (concat (imagedir, de->d_name));
 
112
    sub->w = width;
 
113
    sub->h = height;
 
114
    sub->index = current_index++;
 
115
 
 
116
    fclose (f);
 
117
  }
 
118
}
 
119
 
 
120
static int print_col = 0;
 
121
static int print_com = 1;
 
122
static void
 
123
print_int(FILE *f, int val)
 
124
{
 
125
  char buf[20];
 
126
  sprintf(buf, "%d", val);
 
127
  if (print_com) {
 
128
    fputc(',', f);
 
129
    print_col ++;
 
130
  } else {
 
131
    fprintf(f, "  ");
 
132
    print_col = 2;
 
133
  }
 
134
  if (print_col + strlen(buf) > 70) {
 
135
    fprintf(f, "\n  ");
 
136
    print_col = 2;
 
137
  }
 
138
  fputs(buf, f);
 
139
  print_col += strlen(buf);
 
140
  print_com = 1;
 
141
}
 
142
 
 
143
void
 
144
dump_images(FILE *o)
 
145
{
 
146
  image_struct *img;
 
147
  subimage_struct *sub;
 
148
  int i;
 
149
  static char *type_names[] = {"mono", "grey", "color"};
 
150
 
 
151
  for (img=image_list; img; img=img->next)
 
152
    for (i=0; i<3; i++)
 
153
      for (sub=img->sub[i]; sub; sub=sub->next)
 
154
        {
 
155
          FILE *f = fopen(sub->filename, "rb");
 
156
          int byte;
 
157
          fprintf(o, "/* %s */\n", sub->filename);
 
158
          fprintf(o, "static const unsigned char data_%d[] = {\n", sub->index);
 
159
          print_com = 0;
 
160
          while ((byte = fgetc (f)) != EOF)
 
161
            print_int(o, byte);
 
162
          fprintf(o, "};\n\n");
 
163
          fclose (f);
 
164
        }
 
165
 
 
166
  for (img=image_list; img; img=img->next)
 
167
    for (i=0; i<3; i++)
 
168
      if (img->sub[i])
 
169
        {
 
170
          fprintf(o, "static image sub_%d_%s[] = {\n", img->index, type_names[i]);
 
171
          for (sub=img->sub[i]; sub; sub=sub->next)
 
172
            fprintf(o, "  { %d, %d, data_%d },\n", sub->w, sub->h, sub->index);
 
173
          fprintf(o, "  { 0, 0, 0 }\n};\n\n");
 
174
        }
 
175
 
 
176
  fprintf(o, "image_list %s_imagelib[] = {\n", basename);
 
177
  for (img=image_list; img; img=img->next)
 
178
    {
 
179
      char *c = "";
 
180
      fprintf(o, "  { \"%s\", %d, %d, { ", img->name, img->a, img->d);
 
181
      for (i=0; i<3; i++)
 
182
        {
 
183
          if (img->sub[i])
 
184
            fprintf(o, "%ssub_%d_%s", c, img->index, type_names[i]);
 
185
          else
 
186
            fprintf(o, "%s0", c);
 
187
          c = ", ";
 
188
        }
 
189
      fprintf(o, " } },\n");
 
190
    }
 
191
  fprintf(o, "  { 0, 0, 0, { 0, 0, 0 } }\n};\n\n");
 
192
}
 
193
 
 
194
/*****************************************************************************/
 
195
 
 
196
char *
 
197
tokenize(char *string)
 
198
{
 
199
  static char *next;
 
200
  char *rv;
 
201
  if (string) {
 
202
    next = string;
 
203
    return;
 
204
  }
 
205
  while (*next && !isgraph(*next)) next++;
 
206
  if (!*next) return 0;
 
207
  rv = next;
 
208
  while (*next && isgraph(*next)) next++;
 
209
  if (*next)
 
210
    *next++ = 0;
 
211
  return rv;
 
212
}
 
213
 
 
214
void
 
215
give_help()
 
216
{
 
217
  fprintf(stderr, "make-imgdir [options] infile outfile [pngs]\n");
 
218
  fprintf(stderr, "  -v               verbose\n");
 
219
  fprintf(stderr, "  -h               print this help message\n");
 
220
  fprintf(stderr, "  -n basename      specify basename for symbols\n");
 
221
  fprintf(stderr, "  -i imagedir      location of images\n");
 
222
  fprintf(stderr, "  -d dependencies  write dependencies to this file\n");
 
223
  fprintf(stderr, "  infile format: <image> [across down]\n");
 
224
  fprintf(stderr, "  if infile is `-' image names are taken from the command line.\n");
 
225
  exit(1);
 
226
}
 
227
 
 
228
/*****************************************************************************/
 
229
 
 
230
int
 
231
main(int argc, char **argv)
 
232
{
 
233
  struct stat s;
 
234
  int x, y, o;
 
235
  FILE *inf, *outf, *depfile;
 
236
  char *inbuf, *outfname, *tok;
 
237
  char *imagename;
 
238
  int across, down;
 
239
  image_struct *img;
 
240
 
 
241
  while (1)
 
242
    switch (getopt(argc, argv, "vhn:i:d:m:")) {
 
243
    case '?':
 
244
    case 'h':
 
245
      give_help();
 
246
    case 'v':
 
247
      verbose++;
 
248
      break;
 
249
    case 'n':
 
250
      basename = optarg;
 
251
      break;
 
252
    case 'i':
 
253
      imagedir = optarg;
 
254
      break;
 
255
    case 'd':
 
256
      depfile = fopen(optarg, "w");
 
257
      if (!depfile) {
 
258
        fprintf(stderr, "Unable to open dependency file %s for writing\n", depfile);
 
259
        perror("The error was");
 
260
        exit(1);
 
261
      }
 
262
      break;
 
263
    case -1:
 
264
      goto done_args;
 
265
    }
 
266
 done_args:
 
267
 
 
268
  if (optind > argc-1)
 
269
    give_help();
 
270
 
 
271
  if (strcmp (argv[optind], "-") == 0)
 
272
    {
 
273
      int i;
 
274
      char *dot;
 
275
      for (i=optind+2; i<argc; i++)
 
276
        {
 
277
          printf("image %s\n", argv[i]);
 
278
          dot = strrchr(argv[i], '.');
 
279
          if (dot) *dot = 0;
 
280
 
 
281
          img = (image_struct *)malloc(sizeof(image_struct));
 
282
          memset(img, 0, sizeof(*img));
 
283
          img->name = strdup (argv[i]);
 
284
          img->a = img->d = 1;
 
285
          img->index = current_index++;
 
286
 
 
287
          img->next = image_list;
 
288
          image_list = img;
 
289
        }
 
290
    }
 
291
  else
 
292
    {
 
293
      inf = fopen(argv[optind], "r");
 
294
      if (!inf) {
 
295
        fprintf(stderr, "Unable to open %s for reading\n", argv[optind]);
 
296
        perror("The error was");
 
297
        exit(1);
 
298
      }
 
299
      fstat(fileno(inf), &s);
 
300
      inbuf = (char *)malloc(s.st_size+1);
 
301
 
 
302
      while (fgets(inbuf, s.st_size, inf)) {
 
303
 
 
304
        inbuf[strlen(inbuf)] = 0;
 
305
        tokenize(inbuf);
 
306
 
 
307
        imagename = tokenize(0);
 
308
        if (!imagename) continue;
 
309
 
 
310
        img = (image_struct *)malloc(sizeof(image_struct));
 
311
        memset(img, 0, sizeof(*img));
 
312
        img->name = strdup (imagename);
 
313
        img->a = img->d = 1;
 
314
        img->index = current_index++;
 
315
 
 
316
        tok = tokenize(0);
 
317
        if (tok)
 
318
          img->a = atoi(tok);
 
319
        tok = tokenize(0);
 
320
        if (tok)
 
321
          img->d = atoi(tok);
 
322
 
 
323
        img->next = image_list;
 
324
        image_list = img;
 
325
      }
 
326
    }
 
327
 
 
328
  scan_image_directory();
 
329
 
 
330
  if (optind > argc-2)
 
331
    outfname = 0;
 
332
  else
 
333
    outfname = argv[optind+1];
 
334
 
 
335
  if (outfname)
 
336
    outf = fopen(outfname, "w");
 
337
  else
 
338
    outf = stdout;
 
339
  if (!outf) {
 
340
    fprintf(stderr, "Unable to open %s for writing\n", outfname);
 
341
    perror("The error was");
 
342
    exit(1);
 
343
  }
 
344
 
 
345
  fprintf(outf, "/* Automatically generated, do not edit */\n");
 
346
  fprintf(outf, "#include \"imagelib.h\"\n\n");
 
347
 
 
348
  dump_images(outf);
 
349
 
 
350
  if (outfname) fclose(outf);
 
351
 
 
352
  exit(0);
 
353
}