📄 ppm_image.c
字号:
/*=================================================================================
FILE: ppm_image.c
DESCRIPTION: This file is provide as a standard sample Brew source file.
Please refer to this OpenGL(R)ES brew sample application as a
reference on how to use the standard OpenGL-ES and EGL APIs.
ABSTRACT: PPM format image loader
AUTHOR: QUALCOMM
Copyright (c) 2004 QUALCOMM Incorporated.
All Rights Reserved.
QUALCOMM Proprietary/GTDR
=================================================================================*/
/*-------------------------------------------------------------------------------*
* I N C L U D E F I L E S *
*-------------------------------------------------------------------------------*/
#include "ppm_image.h"
/*-------------------------------------------------------------------------------*
* F U N C T I O N P R O T O T Y P E S *
*-------------------------------------------------------------------------------*/
int ReadPPMLine( IFile*, char * );
/*-------------------------------------------------------------------------------*
* S T R U C T *
*-------------------------------------------------------------------------------*/
/* format of each 24-bit texel in a raw ppm file: */
struct rgb
{
unsigned char r, g, b;
};
/*-------------------------------------------------------------------------------*
* B E G I N P R O G R A M *
*-------------------------------------------------------------------------------*/
/*===========================================================================
FUNCTION: PPMToTexture
DESCRIPTION:
This function will read the ppm file as texture
PROTOTYPE:
unsigned char * PPMToTexture(IShell *shell, char *filename, int *widthp, int *heightp )
PARAMETERS:
shell : A pointer pointing to the IShell Interface object
filename : A pointer pointing to a file
widthp : A pointer pointing to the width of the texture
heightp : A pointer pointing to the height of the texture
DEPENDENCIES
none
RETURN VALUE
none
===========================================================================*/
unsigned char *
PPMToTexture(IShell *shell, const char *filename, int *widthp, int *heightp )
{
IFileMgr *m_pIFileMgr;
IFile* pFile;
int n; /* # items read */
int width, height; /* image size */
unsigned char *texture; /* texture array */
int range; /* color range, should be 256 */
char line[256]; /* hold the ascii lines */
/* open the filename and read width and height: */
if (ISHELL_CreateInstance(shell, AEECLSID_FILEMGR, (void **)&m_pIFileMgr) != SUCCESS)
return NULL;
pFile = IFILEMGR_OpenFile(m_pIFileMgr, filename, _OFM_READ);
if(pFile == NULL) return NULL;
n = ReadPPMLine( pFile, line );
n = ReadPPMLine( pFile, line );
sscanf( line, "%d %d", &width, &height );
n = ReadPPMLine( pFile, line );
sscanf( line, "%d", &range );
/* allocate the texture array: */
texture = ( unsigned char *) MALLOC( 3 * width * height * 1 );
if (!texture)
{
DBGPRINTF("texture allocation failed");
return NULL;
}
/* read the entire file: */
n = IFILE_Read(pFile, texture, width*height*3);
/* close the file and return: */
if (pFile) IFILE_Release(pFile);
if (m_pIFileMgr) IFILEMGR_Release(m_pIFileMgr);
*widthp = width;
*heightp = height;
return texture;
}
/*===========================================================================
FUNCTION: ReadPPMLine
DESCRIPTION:
This function will read one line of the input file
PROTOTYPE:
int ReadPPMLine( IFile* pFile, char *line )
PARAMETERS:
pFile : pointer to a file
line : pointer to line[] char array
DEPENDENCIES
none
RETURN VALUE
1 : finish reading the line
===========================================================================*/
int
ReadPPMLine( IFile* pFile, char *line )
{
char *p; /* pointer into line[] array */
char c; /* character read */
/* keep reading until we get a non-comment: */
for( ; ; )
{
p = line;
IFILE_Read(pFile, &c, 1);
while(c != '\n')
{
*p++ = c;
IFILE_Read(pFile, &c, 1);
}
/* terminate with a null: */
*p = '\0';
/* get out if not a comment: */
if( line[0] != '#' )
break;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -