1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-25 17:58:27 +00:00

support loading a texture from a PPM file

git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@16593 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
reimar 2005-09-25 16:30:00 +00:00
parent ce8756fc4c
commit d29c9ddf19
2 changed files with 62 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h>
#include <math.h> #include <math.h>
#include "gl_common.h" #include "gl_common.h"
@ -327,6 +328,65 @@ void glCreateClearTex(GLenum target, GLenum fmt, GLint filter,
free(init); free(init);
} }
/**
* \brief skips whitespace and comments
* \param f file to read from
*/
static void ppm_skip(FILE *f) {
int c, comment = 0;
do {
c = fgetc(f);
if (c == '#')
comment = 1;
if (c == '\n')
comment = 0;
} while (c != EOF && (isspace(c) || comment));
if (c != EOF)
ungetc(c, f);
}
/**
* \brief creates a texture from a PPM file
* \param target texture taget, usually GL_TEXTURE_2D
* \param fmt internal texture format
* \param filter filter used for scaling, e.g. GL_LINEAR
* \param f file to read PPM from
* \param width [out] width of texture
* \param height [out] height of texture
* \param maxval [out] maxval value from PPM file
* \return 0 on error, 1 otherwise
*/
int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
FILE *f, int *width, int *height, int *maxval) {
int w, h, m, val;
char *data;
ppm_skip(f);
if (fgetc(f) != 'P' || fgetc(f) != '6')
return 0;
ppm_skip(f);
if (fscanf(f, "%i", &w) != 1)
return 0;
ppm_skip(f);
if (fscanf(f, "%i", &h) != 1)
return 0;
ppm_skip(f);
if (fscanf(f, "%i", &m) != 1)
return 0;
val = fgetc(f);
if (!isspace(val))
return 0;
data = (char *)malloc(w * h * 3);
if (fread(data, w * 3, h, f) != h)
return 0;
glCreateClearTex(target, fmt, filter, w, h, 0);
glUploadTex(target, GL_RGB, GL_UNSIGNED_BYTE, data, w * 3, 0, 0, w, h, 0);
free(data);
if (width) *width = w;
if (height) *height = h;
if (maxval) *maxval = m;
return 1;
}
/** /**
* \brief return the number of bytes oer pixel for the given format * \brief return the number of bytes oer pixel for the given format
* \param format OpenGL format * \param format OpenGL format

View File

@ -141,6 +141,8 @@ int glFindFormat(uint32_t format, uint32_t *bpp, GLint *gl_texfmt,
int glFmt2bpp(GLenum format, GLenum type); int glFmt2bpp(GLenum format, GLenum type);
void glCreateClearTex(GLenum target, GLenum fmt, GLint filter, void glCreateClearTex(GLenum target, GLenum fmt, GLint filter,
int w, int h, unsigned char val); int w, int h, unsigned char val);
int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
FILE *f, int *width, int *height, int *maxval);
void glUploadTex(GLenum target, GLenum format, GLenum type, void glUploadTex(GLenum target, GLenum format, GLenum type,
const char *data, int stride, const char *data, int stride,
int x, int y, int w, int h, int slice); int x, int y, int w, int h, int slice);