summaryrefslogtreecommitdiffstats
path: root/libvo/gl_common.c
diff options
context:
space:
mode:
authorreimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2>2005-09-25 16:30:00 +0000
committerreimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2>2005-09-25 16:30:00 +0000
commitd29c9ddf19c11fcdaa0a29d02e6540bf5ec89888 (patch)
tree80c85379554fe803b0b8ccf93c56ccd68e08d15b /libvo/gl_common.c
parentce8756fc4c0cdbf1513e81db8e525b6de8fcb84d (diff)
downloadmpv-d29c9ddf19c11fcdaa0a29d02e6540bf5ec89888.tar.bz2
mpv-d29c9ddf19c11fcdaa0a29d02e6540bf5ec89888.tar.xz
support loading a texture from a PPM file
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@16593 b3059339-0415-0410-9bf9-f77b7e298cf2
Diffstat (limited to 'libvo/gl_common.c')
-rw-r--r--libvo/gl_common.c60
1 files changed, 60 insertions, 0 deletions
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <ctype.h>
#include <math.h>
#include "gl_common.h"
@@ -328,6 +329,65 @@ void glCreateClearTex(GLenum target, GLenum fmt, GLint filter,
}
/**
+ * \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
* \param type OpenGL type