File: ppm.c

package info (click to toggle)
glide 2002.04.10ds1-21
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 59,668 kB
  • sloc: ansic: 290,122; asm: 23,305; sh: 8,090; pascal: 3,854; makefile: 1,258; perl: 73
file content (118 lines) | stat: -rw-r--r-- 2,907 bytes parent folder | download | duplicates (9)
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

/*
** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE 
** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com). 
** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER 
** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
** FULL TEXT OF THE NON-WARRANTY PROVISIONS.  
** 
** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
** THE UNITED STATES.  
** 
** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
**
** $Revision: 1.2 $
** $Date: 2000/06/15 00:11:40 $
**
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "texusint.h"


FxBool 
_txReadPPMHeader( FILE *stream, FxU32 cookie, TxMip *info)
{ 
    char buffer[256];
    FxU32 state = 1;
    FxBool done = FXFALSE;
    
    if ( stream == NULL ) {
	txPanic("PPM file: Bad file handle.");
	return FXFALSE;
    }
    
    while( !done && fgets( buffer, 256, stream ) ) {
	char *token;
	
	if ( buffer[0] == '#' ) continue;
	for (token = strtok( buffer, " \t\n\r" ); token != NULL;
			token = strtok( NULL, " \t\n\r" )) {
	    switch( state ) {
	    case 1:	// Width
			info->width = atoi( token );
			state++;
			break;

	    case 2:	// height
			info->height = atoi( token );
			state++;
			break;

	    case 3:	// Color Depth
			info->format = atoi( token );
			if ( info->format != 255 ) {
			    txPanic("Unsupported PPM format: max != 255\n");
		    	    return FXFALSE;
			}
			state++;
			done = FXTRUE;
			break;

	    default:
			txPanic("PPM file: parse error\n");
			return FXFALSE;
			break;
	    }
	}
    }
    
    if ( state < 4 ) {
    	txPanic("PPM file: Read error before end of header.");
	return FXFALSE;
    }
    info->depth = 1;
    info->format = GR_TEXFMT_ARGB_8888;
    info->size = info->width * info->height * 4;
    return FXTRUE;
}

FxBool 
_txReadPPMData( FILE *stream, TxMip *info) 
{ 
    FxU32 numPixels;
    FxU32 *data32 = info->data[0];
    
    numPixels = info->width * info->height;

    if ( stream == NULL ) {
	txPanic("PPM file: Bad file handle.");
	return FXFALSE;
    }
    
    // Read in image data
    while (numPixels--) {
	int r, g, b;

	r = getc( stream );
	g = getc( stream );
	b = getc( stream );
	if ( b == EOF ) {
	    txPanic("PPM file: Unexpected End of File.");
	    return FXFALSE;
	}
	*data32++ = (0xFF << 24) | (r << 16) | (g << 8) | b;
    }
    return FXTRUE;
}