include.c 1.4 KB

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