source: abuse/trunk/src/imlib/ppmread.cpp @ 57

Last change on this file since 57 was 56, checked in by Sam Hocevar, 14 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 2.5 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include <stdio.h>
13
14#include "image.hpp"
15#include "palette.hpp"
16#include "macs.hpp"
17#include "ppmread.hpp"
18
19
20int read_ppm_header(FILE *fp, int *parm)
21{
22  int nr;
23  char st[100],sig[50];
24  nr=0;
25  fscanf(fp,"%s",sig);
26  if (!strcmp(sig,"P6"))
27  {
28    do
29    {
30      fscanf(fp,"%s",sig);
31      if (sig[0]=='#')
32        fgets(st,100,fp);
33      else
34      {
35        if (sscanf(sig,"%d",parm))
36        {  nr++; parm++; }
37        else return 0;
38      }
39    } while (nr<3 && !feof(fp));
40  }
41  else return 0;
42//  fgets(st,100,fp);
43  return 1;
44}
45
46void write_ppm(image *im,palette *pal,char *fn)
47{
48  FILE *fp;
49  CHECK(im && pal && fn);
50  unsigned char r[3],*c;
51  int x,y;
52  clear_errors();
53  fp=fopen(fn,"wb");
54  if (!fp) set_error(imWRITE_ERROR);
55  else
56  {
57    fprintf(fp,"%s %d %d %d\n","P6",im->width(),im->height(),(int)256);
58    for (y=0;y<im->height();y++)
59    { c=(unsigned char *)im->scan_line(y);
60      for (x=0;x<im->width();x++)
61      { r[0]=pal->red(c[x]);
62        r[1]=pal->green(c[x]);
63        r[2]=pal->blue(c[x]);
64        fwrite(&r[0],1,1,fp);
65        fwrite(&r[1],1,1,fp);
66        fwrite(&r[2],1,1,fp);
67      }
68    }
69    fclose(fp);
70  }
71}
72
73#define TSIZE 1001
74
75image *read_ppm(char *fn,palette *&pal, int pal_type)
76{
77  FILE *fp;
78  image *im;
79  unsigned char *c,col[3];
80
81  char buf[30];
82  int l,h,maxc,i,j,parm[3],find_color;
83  CONDITION(fn,"Null filename");
84  clear_errors();
85  fp=fopen(fn,"rb");
86  im=NULL;
87  CONDITION(fp,"Filename not found");
88  if (!fp) { set_error(imFILE_NOT_FOUND); return NULL; }
89
90  if (read_ppm_header(fp, parm)==0) set_error(imFILE_CORRUPTED);
91  else
92  {
93    l=parm[0]; h=parm[1]; maxc=parm[2];
94   
95    if (!pal)
96      pal=new palette;
97    fgets(buf,30,fp);
98    im=new image(l,h);
99    printf("Created image %d,%d\n",l,h);
100    for (i=0;i<h;i++)
101    { c=(unsigned char *)im->scan_line(i);
102      for (j=0;j<l;j++)
103      {
104        if (fread(col,1,3,fp)!=3) set_error(imFILE_CORRUPTED);
105        if (pal_type==PPM_R3G3B2)
106          c[j]=(col[0]*7/255)|((col[1]*7/255)<<3)|((col[2]*3/255)<<6);
107        else if (pal_type==PPM_BW)
108          c[j]=col[0]*255/parm[2];
109        else
110        {
111          find_color=pal->find_color(col[0],col[1],col[2]);
112          if (find_color>=0) c[j]=find_color;
113          else c[j]=(unsigned char) pal->add_color(col[0],col[1],col[2]);
114        }
115      }
116    }
117  }
118  fclose(fp);
119  return im;
120}
Note: See TracBrowser for help on using the repository browser.