| 1 | /* $Id: ppm.c,v 1.2 2001-09-05 14:31:12 bird Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | ** THIS SOFTWARE IS SUBJECT TO COPYRIGHT PROTECTION AND IS OFFERED ONLY
|
|---|
| 5 | ** PURSUANT TO THE 3DFX GLIDE GENERAL PUBLIC LICENSE. THERE IS NO RIGHT
|
|---|
| 6 | ** TO USE THE GLIDE TRADEMARK WITHOUT PRIOR WRITTEN PERMISSION OF 3DFX
|
|---|
| 7 | ** INTERACTIVE, INC. A COPY OF THIS LICENSE MAY BE OBTAINED FROM THE
|
|---|
| 8 | ** DISTRIBUTOR OR BY CONTACTING 3DFX INTERACTIVE INC(info@3dfx.com).
|
|---|
| 9 | ** THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
|
|---|
| 10 | ** EXPRESSED OR IMPLIED. SEE THE 3DFX GLIDE GENERAL PUBLIC LICENSE FOR A
|
|---|
| 11 | ** FULL TEXT OF THE NON-WARRANTY PROVISIONS.
|
|---|
| 12 | **
|
|---|
| 13 | ** USE, DUPLICATION OR DISCLOSURE BY THE GOVERNMENT IS SUBJECT TO
|
|---|
| 14 | ** RESTRICTIONS AS SET FORTH IN SUBDIVISION (C)(1)(II) OF THE RIGHTS IN
|
|---|
| 15 | ** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013,
|
|---|
| 16 | ** AND/OR IN SIMILAR OR SUCCESSOR CLAUSES IN THE FAR, DOD OR NASA FAR
|
|---|
| 17 | ** SUPPLEMENT. UNPUBLISHED RIGHTS RESERVED UNDER THE COPYRIGHT LAWS OF
|
|---|
| 18 | ** THE UNITED STATES.
|
|---|
| 19 | **
|
|---|
| 20 | ** COPYRIGHT 3DFX INTERACTIVE, INC. 1999, ALL RIGHTS RESERVED
|
|---|
| 21 | **
|
|---|
| 22 | ** $Revision: 1.2 $
|
|---|
| 23 | ** $Date: 2001-09-05 14:31:12 $
|
|---|
| 24 | **
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include <stdio.h>
|
|---|
| 28 | #include <stdlib.h>
|
|---|
| 29 | #include <string.h>
|
|---|
| 30 | #include <math.h>
|
|---|
| 31 |
|
|---|
| 32 | #include "texusint.h"
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 | FxBool
|
|---|
| 36 | _txReadPPMHeader( FILE *stream, FxU32 cookie, TxMip *info)
|
|---|
| 37 | {
|
|---|
| 38 | char buffer[256];
|
|---|
| 39 | FxU32 state = 1;
|
|---|
| 40 | FxBool done = FXFALSE;
|
|---|
| 41 |
|
|---|
| 42 | if ( stream == NULL ) {
|
|---|
| 43 | txPanic("PPM file: Bad file handle.");
|
|---|
| 44 | return FXFALSE;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | while( !done && fgets( buffer, 256, stream ) ) {
|
|---|
| 48 | char *token;
|
|---|
| 49 |
|
|---|
| 50 | if ( buffer[0] == '#' ) continue;
|
|---|
| 51 | for (token = strtok( buffer, " \t\n\r" ); token != NULL;
|
|---|
| 52 | token = strtok( NULL, " \t\n\r" )) {
|
|---|
| 53 | switch( state ) {
|
|---|
| 54 | case 1: // Width
|
|---|
| 55 | info->width = atoi( token );
|
|---|
| 56 | state++;
|
|---|
| 57 | break;
|
|---|
| 58 |
|
|---|
| 59 | case 2: // height
|
|---|
| 60 | info->height = atoi( token );
|
|---|
| 61 | state++;
|
|---|
| 62 | break;
|
|---|
| 63 |
|
|---|
| 64 | case 3: // Color Depth
|
|---|
| 65 | info->format = atoi( token );
|
|---|
| 66 | if ( info->format != 255 ) {
|
|---|
| 67 | txPanic("Unsupported PPM format: max != 255\n");
|
|---|
| 68 | return FXFALSE;
|
|---|
| 69 | }
|
|---|
| 70 | state++;
|
|---|
| 71 | done = FXTRUE;
|
|---|
| 72 | break;
|
|---|
| 73 |
|
|---|
| 74 | default:
|
|---|
| 75 | txPanic("PPM file: parse error\n");
|
|---|
| 76 | return FXFALSE;
|
|---|
| 77 | break;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if ( state < 4 ) {
|
|---|
| 83 | txPanic("PPM file: Read error before end of header.");
|
|---|
| 84 | return FXFALSE;
|
|---|
| 85 | }
|
|---|
| 86 | info->depth = 1;
|
|---|
| 87 | info->format = GR_TEXFMT_ARGB_8888;
|
|---|
| 88 | info->size = info->width * info->height * 4;
|
|---|
| 89 | return FXTRUE;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | FxBool
|
|---|
| 93 | _txReadPPMData( FILE *stream, TxMip *info)
|
|---|
| 94 | {
|
|---|
| 95 | FxU32 numPixels;
|
|---|
| 96 | FxU32 *data32 = info->data[0];
|
|---|
| 97 |
|
|---|
| 98 | numPixels = info->width * info->height;
|
|---|
| 99 |
|
|---|
| 100 | if ( stream == NULL ) {
|
|---|
| 101 | txPanic("PPM file: Bad file handle.");
|
|---|
| 102 | return FXFALSE;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | // Read in image data
|
|---|
| 106 | while (numPixels--) {
|
|---|
| 107 | int r, g, b;
|
|---|
| 108 |
|
|---|
| 109 | r = getc( stream );
|
|---|
| 110 | g = getc( stream );
|
|---|
| 111 | b = getc( stream );
|
|---|
| 112 | if ( b == EOF ) {
|
|---|
| 113 | txPanic("PPM file: Unexpected End of File.");
|
|---|
| 114 | return FXFALSE;
|
|---|
| 115 | }
|
|---|
| 116 | *data32++ = (0xFF << 24) | (r << 16) | (g << 8) | b;
|
|---|
| 117 | }
|
|---|
| 118 | return FXTRUE;
|
|---|
| 119 | }
|
|---|