source: abuse/trunk/src/imlib/include.cpp @ 4

Last change on this file since 4 was 4, checked in by Sam Hocevar, 18 years ago
  • debian patches
File size: 1.5 KB
Line 
1#include "include.hpp"
2#include "ctype.h"
3
4void write_include(image *im, palette *pal, char *filename, char *name)
5{
6  char tmp_name[200];
7  strcpy(tmp_name,name);
8  unsigned int j;
9  int append=0,i;
10  for (j=0;j<strlen(name);j++)
11    if (toupper(tmp_name[j])<'A' || toupper(tmp_name[j])>'Z')
12      tmp_name[j]='_';
13
14  FILE *fp=fopen(filename,"rb");  // see if the file already exsist
15  if (fp)
16  {
17    fclose(fp);
18    fp=fopen(filename,"ab");  // if so, append to the end and don't write the palette
19    append=1;
20  }
21  else fp=fopen(filename,"wb");
22
23  if (!fp)
24    set_error(imWRITE_ERROR);
25  else
26  {
27    fprintf(fp,"/* File produced by Satan Paint (c) 1994 Jonathan Clark */\n\n");
28    if (!append)
29    {
30      fprintf(fp,"unsigned char %s_palette[256*3] = {\n    ",tmp_name);
31      unsigned char *p=(unsigned char *)pal->addr();
32      for (i=0;i<768;i++,p++)
33      {
34        fprintf(fp,"%d",(int)*p);
35        if (i==767)
36        fprintf(fp,"};\n\n");
37        else
38        if (i%15==14)
39        fprintf(fp,",\n    ");
40        else fprintf(fp,", ");
41      }
42    }
43    fprintf(fp,"unsigned char %s[%d*%d]={\n    ",tmp_name,
44            im->width(),im->height());
45    int x,y,max=im->width()*im->height()-1;
46    for (y=0,i=0;y<im->height();y++)
47      for (x=0;x<im->width();x++,i++)
48      {
49        fprintf(fp,"%d",(int)im->pixel(x,y));
50        if (i==max)
51          fprintf(fp,"};\n\n");
52        else
53          if (i%15==14)
54            fprintf(fp,",\n    ");
55          else fprintf(fp,", ");
56      }
57  }
58  fclose(fp);
59}
60
Note: See TracBrowser for help on using the repository browser.