File: readWriteSGI.c

package info (click to toggle)
xpaint 2.6.2-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,520 kB
  • ctags: 2,552
  • sloc: ansic: 26,927; makefile: 43; sh: 23
file content (137 lines) | stat: -rw-r--r-- 3,296 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: readWriteSGI.c,v 1.2 1996/04/19 09:30:38 torsten Exp $ */

#include <stdio.h>
#include <gl/image.h>
#include <sys/fcntl.h>
#include "image.h"

int WriteSGI(char *file, Image * image)
{
    IMAGE *o;
    short *rbuf = NULL, *gbuf = NULL, *bbuf = NULL;
    int x, y;
    unsigned char *ip;
    int d = 3;

    if (image->isGrey)
	d = 1;
    if ((o = iopen(file, "w", RLE(1), 3, image->width, image->height, d)) == NULL)
	return 1;

    if (d == 1) {
	rbuf = (short *) malloc(image->width * sizeof(short));
    } else {
	rbuf = (short *) malloc(image->width * sizeof(short));
	gbuf = (short *) malloc(image->width * sizeof(short));
	bbuf = (short *) malloc(image->width * sizeof(short));
    }

    for (y = 0; y < image->height; y++) {
	for (x = 0; x < image->width; x++) {
	    ip = ImagePixel(image, x, y);
	    rbuf[x] = ip[0];
	    if (d != 1) {
		gbuf[x] = ip[1];
		bbuf[x] = ip[2];
	    }
	}
	if (rbuf != NULL)
	    putrow(o, rbuf, image->height - y - 1, 0);
	if (gbuf != NULL)
	    putrow(o, gbuf, image->height - y - 1, 1);
	if (bbuf != NULL)
	    putrow(o, bbuf, image->height - y - 1, 2);
    }

    iclose(o);

    if (rbuf != NULL)
	free(rbuf);
    if (gbuf != NULL)
	free(gbuf);
    if (bbuf != NULL)
	free(bbuf);

    return 0;
}

int TestSGI(char *file)
{
    int f = open(file, O_RDONLY);
    unsigned short c;
    int ret = 0;

    if (f < 0)
	return 0;

    ret = ((read(f, &c, 2) == 2) && c == IMAGIC);

    close(f);

    return ret;
}

Image *ReadSGI(char *file)
{
    Image *image;
    IMAGE *in;
    short *rbuf, *gbuf, *bbuf;
    int x, y;
    unsigned char *ip;

    if ((in = iopen(file, "r")) == 0)
	return NULL;

    if (in->zsize == 1)
	image = ImageNewGrey(in->xsize, in->ysize);
    else
	image = ImageNew(in->xsize, in->ysize);

    rbuf = (short *) malloc(in->xsize * sizeof(short));
    gbuf = bbuf = rbuf;
    if (in->zsize != 1) {
	gbuf = (short *) malloc(in->xsize * sizeof(short));
	bbuf = (short *) malloc(in->xsize * sizeof(short));
    }
    ip = image->data;

    for (y = in->ysize - 1; y >= 0; y--) {
	getrow(in, rbuf, y, 0);
	if (gbuf != rbuf)
	    getrow(in, gbuf, y, 1);
	if (bbuf != rbuf)
	    getrow(in, bbuf, y, 2);

	for (x = 0; x < in->xsize; x++) {
	    *ip++ = rbuf[x] & 0xff;
	    if (in->zsize != 1) {
		*ip++ = gbuf[x] & 0xff;
		*ip++ = bbuf[x] & 0xff;
	    }
	}
    }

    free(rbuf);
    if (gbuf != rbuf)
	free(gbuf);
    if (bbuf != rbuf)
	free(bbuf);

    iclose(in);

    return image;
}