File: include.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (59 lines) | stat: -rw-r--r-- 1,482 bytes parent folder | download | duplicates (4)
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
#include "include.hpp"
#include "ctype.h"

void write_include(image *im, palette *pal, char *filename, char *name)
{
  char tmp_name[200];
  strcpy(tmp_name,name);
  int j,append=0,i;
  for (j=0;j<strlen(name);j++)
    if (toupper(tmp_name[j])<'A' || toupper(tmp_name[j])>'Z')
      tmp_name[j]='_';

  FILE *fp=fopen(filename,"rb");  // see if the file already exsist
  if (fp)
  {
    fclose(fp);
    fp=fopen(filename,"ab");  // if so, append to the end and don't write the palette
    append=1;
  }
  else fp=fopen(filename,"wb");

  if (!fp)
    set_error(imWRITE_ERROR);
  else
  {
    fprintf(fp,"/* File produced by Satan Paint (c) 1994 Jonathan Clark */\n\n");
    if (!append)
    {
      fprintf(fp,"unsigned char %s_palette[256*3] = {\n    ",tmp_name);
      unsigned char *p=(unsigned char *)pal->addr();
      for (i=0;i<768;i++,p++)
      {
	fprintf(fp,"%d",(int)*p);
	if (i==767) 
        fprintf(fp,"};\n\n");
	else
        if (i%15==14)
	fprintf(fp,",\n    ");
        else fprintf(fp,", ");
      }
    }
    fprintf(fp,"unsigned char %s[%d*%d]={\n    ",tmp_name,
            im->width(),im->height()); 
    int x,y,max=im->width()*im->height()-1;
    for (y=0,i=0;y<im->height();y++)
      for (x=0;x<im->width();x++,i++)
      {
        fprintf(fp,"%d",(int)im->pixel(x,y));
        if (i==max)
          fprintf(fp,"};\n\n");
        else
          if (i%15==14)
            fprintf(fp,",\n    ");
          else fprintf(fp,", ");
      }
  }
  fclose(fp);
}