1 /*
2 * psd.c - This file deals with Photoshop format image files (reading/writing)
3 *
4 * $Id: psd.c,v 1.2 2007/02/13 04:38:48 johns Exp $
5 */
6
7 #include <stdio.h>
8 #include "machine.h"
9 #include "types.h"
10 #include "util.h"
11 #include "imageio.h" /* error codes etc */
12 #include "psd.h"
13
writepsd48(char * name,int xres,int yres,unsigned char * imgdata)14 int writepsd48(char *name, int xres, int yres, unsigned char *imgdata) {
15 FILE * ofp;
16 int y, p;
17 char width[4];
18 char height[4];
19 const char *sig = "8BPS"; /* signature */
20 const char ver[] = { 0, 1, 0, 0, 0, 0, 0, 0 }; /* version info */
21 const char chn[] = { 0, 3 }; /* 3 channels */
22 const char mod[] = { 0, 16, 0, 3 }; /* 16-bit color, 3=rgb */
23 const char hdr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
24
25 ofp=fopen(name, "wb");
26 if (ofp==NULL) {
27 return IMAGEBADFILE;
28 }
29
30 width[0] = (xres >> 24) & 0xff;
31 width[1] = (xres >> 16) & 0xff;
32 width[2] = (xres >> 8) & 0xff;
33 width[3] = xres & 0xff;
34
35 height[0] = (yres >> 24) & 0xff;
36 height[1] = (yres >> 16) & 0xff;
37 height[2] = (yres >> 8) & 0xff;
38 height[3] = yres & 0xff;
39
40 fwrite(sig, 4, 1, ofp);
41 fwrite(ver, 8, 1, ofp);
42 fwrite(chn, 2, 1, ofp);
43 fwrite(height, 4, 1, ofp);
44 fwrite(width, 4, 1, ofp);
45 fwrite(mod, 4, 1, ofp);
46 fwrite(hdr, 14, 1, ofp);
47
48 for (p=0; p<3; p++) {
49 int paddr = xres * yres * 2 * p;
50 for (y=0; y<yres; y++) {
51 fwrite(&imgdata[paddr + (yres - y - 1)*xres*2], 1, 2*xres, ofp);
52 }
53 }
54
55 fclose(ofp);
56
57 return IMAGENOERR;
58 }
59
60
61