summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorreimar <reimar@b3059339-0415-0410-9bf9-f77b7e298cf2>2010-10-10 11:20:57 +0000
committerUoti Urpala <uau@glyph.nonexistent.invalid>2010-11-02 04:18:09 +0200
commit2865993869175737a9d5033e46471d6934db2713 (patch)
tree68728664dd34e5c3b262a4baca53df2b3e7541e9
parent3ea62a9666754b128b78aaa5d46431bc38a4d501 (diff)
downloadmpv-2865993869175737a9d5033e46471d6934db2713.tar.bz2
mpv-2865993869175737a9d5033e46471d6934db2713.tar.xz
vo_gl: Extract code to read a pnm file into a separate function
git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@32479 b3059339-0415-0410-9bf9-f77b7e298cf2
-rw-r--r--libvo/gl_common.c72
1 files changed, 48 insertions, 24 deletions
diff --git a/libvo/gl_common.c b/libvo/gl_common.c
index e455acc213..048f3185ce 100644
--- a/libvo/gl_common.c
+++ b/libvo/gl_common.c
@@ -577,6 +577,47 @@ static void ppm_skip(FILE *f) {
#define MAXDIM (16 * 1024)
+static uint8_t *read_pnm(FILE *f, int *width, int *height,
+ int *bytes_per_pixel, int *maxval) {
+ uint8_t *data;
+ int type;
+ unsigned w, h, m, val, bpp;
+ *width = *height = *bytes_per_pixel = *maxval = 0;
+ ppm_skip(f);
+ if (fgetc(f) != 'P')
+ return NULL;
+ type = fgetc(f);
+ if (type != '5' && type != '6')
+ return NULL;
+ ppm_skip(f);
+ if (fscanf(f, "%u", &w) != 1)
+ return NULL;
+ ppm_skip(f);
+ if (fscanf(f, "%u", &h) != 1)
+ return NULL;
+ ppm_skip(f);
+ if (fscanf(f, "%u", &m) != 1)
+ return NULL;
+ val = fgetc(f);
+ if (!isspace(val))
+ return NULL;
+ if (w > MAXDIM || h > MAXDIM)
+ return NULL;
+ bpp = (m > 255) ? 2 : 1;
+ if (type == '6')
+ bpp *= 3;
+ data = malloc(w * h * bpp);
+ if (fread(data, w * bpp, h, f) != h) {
+ free(data);
+ return NULL;
+ }
+ *width = w;
+ *height = h;
+ *bytes_per_pixel = bpp;
+ *maxval = m;
+ return data;
+}
+
/**
* \brief creates a texture from a PPM file
* \param target texture taget, usually GL_TEXTURE_2D
@@ -591,36 +632,19 @@ static void ppm_skip(FILE *f) {
*/
int glCreatePPMTex(GLenum target, GLenum fmt, GLint filter,
FILE *f, int *width, int *height, int *maxval) {
- unsigned w, h, m, val, bpp;
- char *data;
+ int w, h, m, bpp;
GLenum type;
- ppm_skip(f);
- if (fgetc(f) != 'P' || fgetc(f) != '6')
- return 0;
- ppm_skip(f);
- if (fscanf(f, "%u", &w) != 1)
- return 0;
- ppm_skip(f);
- if (fscanf(f, "%u", &h) != 1)
- return 0;
- ppm_skip(f);
- if (fscanf(f, "%u", &m) != 1)
- return 0;
- val = fgetc(f);
- if (!isspace(val))
- return 0;
- if (w > MAXDIM || h > MAXDIM)
- return 0;
- bpp = (m > 255) ? 6 : 3;
- data = malloc(w * h * bpp);
- if (fread(data, w * bpp, h, f) != h)
+ uint8_t *data = read_pnm(f, &w, &h, &bpp, &m);
+ if (!data || (bpp != 3 && bpp != 6)) {
+ free(data);
return 0;
+ }
if (!fmt) {
- fmt = (m > 255) ? hqtexfmt : 3;
+ fmt = bpp == 6 ? hqtexfmt : 3;
if (fmt == GL_FLOAT_RGB32_NV && target != GL_TEXTURE_RECTANGLE)
fmt = GL_RGB16;
}
- type = m > 255 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
+ type = bpp == 6 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_BYTE;
glCreateClearTex(target, fmt, GL_RGB, type, filter, w, h, 0);
glUploadTex(target, GL_RGB, type,
data, w * bpp, 0, 0, w, h, 0);