From d29c9ddf19c11fcdaa0a29d02e6540bf5ec89888 Mon Sep 17 00:00:00 2001 From: reimar Date: Sun, 25 Sep 2005 16:30:00 +0000 Subject: support loading a texture from a PPM file git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@16593 b3059339-0415-0410-9bf9-f77b7e298cf2 --- libvo/gl_common.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ libvo/gl_common.h | 2 ++ 2 files changed, 62 insertions(+) (limited to 'libvo') diff --git a/libvo/gl_common.c b/libvo/gl_common.c index 63f70cee90..2a01bedf4c 100644 --- a/libvo/gl_common.c +++ b/libvo/gl_common.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "gl_common.h" @@ -327,6 +328,65 @@ void glCreateClearTex(GLenum target, GLenum fmt, GLint filter, 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 * \param format OpenGL format diff --git a/libvo/gl_common.h b/libvo/gl_common.h index fd2a4436ae..a09824bb9b 100644 --- a/libvo/gl_common.h +++ b/libvo/gl_common.h @@ -141,6 +141,8 @@ int glFindFormat(uint32_t format, uint32_t *bpp, GLint *gl_texfmt, int glFmt2bpp(GLenum format, GLenum type); void glCreateClearTex(GLenum target, GLenum fmt, GLint filter, 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, const char *data, int stride, int x, int y, int w, int h, int slice); -- cgit v1.2.3