changeset 16592:1bb2b2696451

support loading a texture from a PPM file
author reimar
date Sun, 25 Sep 2005 16:30:00 +0000
parents e5f3a9a03914
children 1c13fb2ba5a9
files libvo/gl_common.c libvo/gl_common.h
diffstat 2 files changed, 62 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libvo/gl_common.c	Sun Sep 25 16:20:41 2005 +0000
+++ b/libvo/gl_common.c	Sun Sep 25 16:30:00 2005 +0000
@@ -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 @@
 }
 
 /**
+ * \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
--- a/libvo/gl_common.h	Sun Sep 25 16:20:41 2005 +0000
+++ b/libvo/gl_common.h	Sun Sep 25 16:30:00 2005 +0000
@@ -141,6 +141,8 @@
 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);