Tachyon (current)  Current Main Branch
psd.c
Go to the documentation of this file.
1 /*
2  * psd.c - This file deals with Photoshop format image files (reading/writing)
3  *
4  * (C) Copyright 1994-2022 John E. Stone
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * $Id: psd.c,v 1.5 2022/02/18 17:55:28 johns Exp $
8  *
9  */
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15 
16 #define TACHYON_INTERNAL 1
17 #include "tachyon.h"
18 #include "util.h"
19 #include "imageio.h" /* error codes etc */
20 #include "psd.h"
21 
22 int writepsd48(char *name, int xres, int yres, unsigned char *imgdata) {
23  FILE * ofp;
24  int y, p;
25  char width[4];
26  char height[4];
27  const char *sig = "8BPS"; /* signature */
28  const char ver[] = { 0, 1, 0, 0, 0, 0, 0, 0 }; /* version info */
29  const char chn[] = { 0, 3 }; /* 3 channels */
30  const char mod[] = { 0, 16, 0, 3 }; /* 16-bit color, 3=rgb */
31  const char hdr[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
32 
33  ofp=fopen(name, "wb");
34  if (ofp==NULL) {
35  return IMAGEBADFILE;
36  }
37 
38  width[0] = (xres >> 24) & 0xff;
39  width[1] = (xres >> 16) & 0xff;
40  width[2] = (xres >> 8) & 0xff;
41  width[3] = xres & 0xff;
42 
43  height[0] = (yres >> 24) & 0xff;
44  height[1] = (yres >> 16) & 0xff;
45  height[2] = (yres >> 8) & 0xff;
46  height[3] = yres & 0xff;
47 
48  fwrite(sig, 4, 1, ofp);
49  fwrite(ver, 8, 1, ofp);
50  fwrite(chn, 2, 1, ofp);
51  fwrite(height, 4, 1, ofp);
52  fwrite(width, 4, 1, ofp);
53  fwrite(mod, 4, 1, ofp);
54  fwrite(hdr, 14, 1, ofp);
55 
56  for (p=0; p<3; p++) {
57  int paddr = xres * yres * 2 * p;
58  for (y=0; y<yres; y++) {
59  fwrite(&imgdata[paddr + (yres - y - 1)*xres*2], 1, 2*xres, ofp);
60  }
61  }
62 
63  fclose(ofp);
64 
65  return IMAGENOERR;
66 }
67 
68 
int writepsd48(char *name, int xres, int yres, unsigned char *imgdata)
Definition: psd.c:22
Tachyon cross-platform timers, special math function wrappers, and RNGs.
#define IMAGENOERR
no error
Definition: imageio.h:16
Tachyon public API function prototypes and declarations used to drive the ray tracing engine...
#define IMAGEBADFILE
can&#39;t find or can&#39;t open the file
Definition: imageio.h:17