Browse Source

image: gather pixel-related types and enums into a single pixel.h header.

undefined
Sam Hocevar 10 years ago
parent
commit
18254349aa
12 changed files with 91 additions and 242 deletions
  1. +1
    -1
      demos/tutorial/11_fractal.cpp
  2. +2
    -2
      src/Makefile.am
  3. +1
    -1
      src/core.h
  4. +1
    -11
      src/gpu/texture.cpp
  5. +0
    -178
      src/image/analysis/histogram.cpp
  6. +6
    -2
      src/image/pixel.cpp
  7. +0
    -37
      src/lol/gpu/texture.h
  8. +1
    -0
      src/lol/image/all.h
  9. +0
    -8
      src/lol/image/image.h
  10. +73
    -0
      src/lol/image/pixel.h
  11. +2
    -1
      src/lolcore.vcxproj
  12. +4
    -1
      src/lolcore.vcxproj.filters

+ 1
- 1
demos/tutorial/11_fractal.cpp View File

@@ -457,7 +457,7 @@ public:
/* Create a texture of half the width and twice the height /* Create a texture of half the width and twice the height
* so that we can upload four different subimages each frame. */ * so that we can upload four different subimages each frame. */
m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2), m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2),
PixelFormat::ABGR_8);
PixelFormat::RGBA_8);


/* Ensure the texture data is complete at least once, otherwise /* Ensure the texture data is complete at least once, otherwise
* uploading subimages will not work. */ * uploading subimages will not work. */


+ 2
- 2
src/Makefile.am View File

@@ -50,7 +50,7 @@ liblolcore_headers = \
lol/sys/timer.h \ lol/sys/timer.h \
\ \
lol/image/all.h \ lol/image/all.h \
lol/image/color.h lol/image/image.h \
lol/image/pixel.h lol/image/color.h lol/image/image.h \
\ \
lol/gpu/all.h \ lol/gpu/all.h \
lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \ lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \
@@ -110,7 +110,7 @@ liblolcore_sources = \
sys/init.cpp sys/timer.cpp sys/file.cpp \ sys/init.cpp sys/timer.cpp sys/file.cpp \
sys/thread.cpp sys/threadbase.h \ sys/thread.cpp sys/threadbase.h \
\ \
image/image.cpp image/image-private.h image/kernel.cpp image/pixels.cpp \
image/image.cpp image/image-private.h image/kernel.cpp image/pixel.cpp \
image/crop.cpp image/resample.cpp image/noise.cpp \ image/crop.cpp image/resample.cpp image/noise.cpp \
image/codec/gdiplus-image.cpp image/codec/imlib2-image.cpp \ image/codec/gdiplus-image.cpp image/codec/imlib2-image.cpp \
image/codec/sdl-image.cpp image/codec/ios-image.cpp \ image/codec/sdl-image.cpp image/codec/ios-image.cpp \


+ 1
- 1
src/core.h View File

@@ -159,8 +159,8 @@ static inline int isnan(float f)
#include <lol/math/all.h> #include <lol/math/all.h>
#include <lol/algorithm/all.h> #include <lol/algorithm/all.h>
#include <lol/sys/all.h> #include <lol/sys/all.h>
#include <lol/gpu/all.h>
#include <lol/image/all.h> #include <lol/image/all.h>
#include <lol/gpu/all.h>
#include <lol/debug/all.h> #include <lol/debug/all.h>


#include "utils.h" #include "utils.h"


+ 1
- 11
src/gpu/texture.cpp View File

@@ -92,21 +92,17 @@ Texture::Texture(ivec2 size, PixelFormat format)


/* FIXME: this is all mixed up for the RGBA/ARGB combinations */ /* FIXME: this is all mixed up for the RGBA/ARGB combinations */
# if defined USE_D3D9 # if defined USE_D3D9
{ D3DFMT_L8, 1 }, /* Y8 */
{ D3DFMT_L8, 1 }, /* Y_8 */
{ D3DFMT_R8G8B8, 3 }, /* RGB_8 */ { D3DFMT_R8G8B8, 3 }, /* RGB_8 */
{ D3DFMT_A8R8G8B8, 4 }, /* RGBA_8 */ { D3DFMT_A8R8G8B8, 4 }, /* RGBA_8 */
{ D3DFMT_A8R8G8B8, 4 }, /* ARGB_8 */
{ D3DFMT_UNKNOWN, 0 }, /* ABGR_8 */
{ D3DFMT_UNKNOWN, 0 }, /* Y_F32 */ { D3DFMT_UNKNOWN, 0 }, /* Y_F32 */
{ D3DFMT_UNKNOWN, 0 }, /* RGB_F32 */ { D3DFMT_UNKNOWN, 0 }, /* RGB_F32 */
{ D3DFMT_UNKNOWN, 0 }, /* RGBA_F32 */ { D3DFMT_UNKNOWN, 0 }, /* RGBA_F32 */
# else # else
{ D3DFMT_LIN_L8, 1 }, { D3DFMT_LIN_L8, 1 },
{ D3DFMT_UNKNOWN, 0 }, { D3DFMT_UNKNOWN, 0 },
{ D3DFMT_UNKNOWN, 0 },
/* By default the X360 will swizzle the texture. Ask for linear. */ /* By default the X360 will swizzle the texture. Ask for linear. */
{ D3DFMT_LIN_A8R8G8B8, 4 }, { D3DFMT_LIN_A8R8G8B8, 4 },
{ D3DFMT_UNKNOWN, 0 },
{ D3DFMT_UNKNOWN, 0 }, /* Y_F32 */ { D3DFMT_UNKNOWN, 0 }, /* Y_F32 */
{ D3DFMT_UNKNOWN, 0 }, /* RGB_F32 */ { D3DFMT_UNKNOWN, 0 }, /* RGB_F32 */
{ D3DFMT_UNKNOWN, 0 }, /* RGBA_F32 */ { D3DFMT_UNKNOWN, 0 }, /* RGBA_F32 */
@@ -142,23 +138,17 @@ Texture::Texture(ivec2 size, PixelFormat format)
{ GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1 }, { GL_LUMINANCE8, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1 },
{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 }, { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 },
{ GL_ARGB_SCE, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4 }, { GL_ARGB_SCE, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4 },
{ GL_ARGB_SCE, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, 4 },
{ GL_ARGB_SCE, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8, 4 },
#elif defined __native_client__ || defined HAVE_GLES_2X #elif defined __native_client__ || defined HAVE_GLES_2X
{ GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1 }, { GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, 1 },
{ GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 3 }, { GL_RGB, GL_RGB, GL_UNSIGNED_BYTE, 3 },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 }, { GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 },
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 },
/* FIXME: if GL_RGBA is not available, we should advertise /* FIXME: if GL_RGBA is not available, we should advertise
* this format as "not available" on this platform. */ * this format as "not available" on this platform. */
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, 4 },
#else #else
{ GL_R8, GL_RED, GL_UNSIGNED_BYTE, 1 }, /* A8 */ { GL_R8, GL_RED, GL_UNSIGNED_BYTE, 1 }, /* A8 */
{ GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 }, /* RGB_8 */ { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, 3 }, /* RGB_8 */
/* Seems efficient for little endian textures */ /* Seems efficient for little endian textures */
{ GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ARGB_8 */ { GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ARGB_8 */
{ GL_RGBA8, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ARGB_8 */
{ GL_RGBA8, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, 4 }, /* ABGR_8 */
#endif #endif
{ 0, 0, 0, 0 }, /* Y_F32 */ { 0, 0, 0, 0 }, /* Y_F32 */
{ 0, 0, 0, 0 }, /* RGB_F32 */ { 0, 0, 0, 0 }, /* RGB_F32 */


+ 0
- 178
src/image/analysis/histogram.cpp View File

@@ -1,178 +0,0 @@
/*
* libpipi Pathetic image processing interface library
* Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
* 2008 Jean-Yves Lamoureux <jylam@lnxscene.org
* All Rights Reserved
*
* $Id$
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/*
* histogram.c: histogram functions
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pipi.h"
#include "pipi-internals.h"


pipi_histogram_t* pipi_new_histogram(void)
{
return malloc(sizeof(pipi_histogram_t));
}


int pipi_get_image_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags)
{
pipi_pixels_t *p;
uint8_t *data;
float n;
unsigned int max;
int i;

if(!h) return -1;

p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
data = (uint8_t *)p->pixels;
memset(h->a, 0, 256*(sizeof(unsigned int)));
memset(h->r, 0, 256*(sizeof(unsigned int)));
memset(h->g, 0, 256*(sizeof(unsigned int)));
memset(h->b, 0, 256*(sizeof(unsigned int)));
memset(h->y, 0, 256*(sizeof(unsigned int)));


for(i=0; i< img->w*img->h*4; i+=4)
{
if(flags&PIPI_COLOR_A)
h->a[data[i+3]]++;
if(flags&PIPI_COLOR_R)
h->r[data[i+2]]++;
if(flags&PIPI_COLOR_G)
h->g[data[i+1]]++;
if(flags&PIPI_COLOR_B)
h->b[data[i]]++;
if(flags&PIPI_COLOR_Y)
{
uint32_t val = 0.;
val += 0.299 * data[i];
val += 0.587 * data[i+1];
val += 0.114 * data[i+2];

h->y[val>255?255:val]++;
}
}

/* Normalize dataset */
if(flags&PIPI_COLOR_R)
{
max = 0;
for(i=0; i<256; i++)
if(h->r[i] > max) max = h->r[i];
n = 255.0f / max;
for(i=0; i<256; i++)
h->r[i]*=n;
}
if(flags&PIPI_COLOR_G)
{
max = 0;
for(i=0; i<256; i++)
if(h->g[i] > max) max = h->g[i];
n = 255.0f / max;
for(i=0; i<256; i++)
h->g[i]*=n;
}
if(flags&PIPI_COLOR_B)
{
max = 0;
for(i=0; i<256; i++)
if(h->b[i] > max) max = h->b[i];
n = 255.0f / max;
for(i=0; i<256; i++)
h->b[i]*=n;
}
if(flags&PIPI_COLOR_A)
{
max = 0;
for(i=0; i<256; i++)
if(h->a[i] > max) max = h->a[i];
n = 255.0f / max;
for(i=0; i<256; i++)
h->a[i]*=n;
}
if(flags&PIPI_COLOR_Y)
{
max = 0;
for(i=0; i<256; i++)
if(h->y[i] > max) max = h->y[i];
n = 255.0f / max;
for(i=0; i<256; i++)
h->y[i]*=n;
}

pipi_release_pixels(img, p);

return 0;
}

int pipi_render_histogram(pipi_image_t *img, pipi_histogram_t*h, int flags)
{
int x;

if(!img || !h) return -1;

for(x=0; x<256; x++)
{
if(flags&PIPI_COLOR_R)
pipi_draw_line(img,
x, 255,
x, 255 - h->r[x],
0x00FF0000,
0);
if(flags&PIPI_COLOR_G)
pipi_draw_line(img,
x, 255,
x, 255 - h->g[x],
0x0000FF00,
0);
if(flags&PIPI_COLOR_B)
pipi_draw_line(img,
x, 255,
x, 255 - h->b[x],
0x000000FF,
0);
if(flags&PIPI_COLOR_A)
pipi_draw_line(img,
x, 255,
x, 255 - h->a[x],
0x00000FFF,
0);
if(flags&PIPI_COLOR_Y)
pipi_draw_line(img,
x, 255,
x, 255 - h->y[x],
0x00FFFFFF,
0);
}

return 0;
}


int pipi_free_histogram(pipi_histogram_t* h)
{
if(h) free(h);
else return -1;

return 0;
}

src/image/pixels.cpp → src/image/pixel.cpp View File

@@ -68,8 +68,12 @@ void Image::SetFormat(PixelFormat fmt)
if (m_data->m_pixels[(int)fmt] == nullptr) if (m_data->m_pixels[(int)fmt] == nullptr)
{ {
PixelDataBase *data = nullptr; PixelDataBase *data = nullptr;
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
switch (fmt) switch (fmt)
{ {
case PixelFormat::Unknown:
break;
case PixelFormat::Y_8: case PixelFormat::Y_8:
data = new PixelData<PixelFormat::Y_8>(size); break; data = new PixelData<PixelFormat::Y_8>(size); break;
case PixelFormat::RGB_8: case PixelFormat::RGB_8:
@@ -82,9 +86,9 @@ void Image::SetFormat(PixelFormat fmt)
data = new PixelData<PixelFormat::RGB_F32>(size); break; data = new PixelData<PixelFormat::RGB_F32>(size); break;
case PixelFormat::RGBA_F32: case PixelFormat::RGBA_F32:
data = new PixelData<PixelFormat::RGBA_F32>(size); break; data = new PixelData<PixelFormat::RGBA_F32>(size); break;
default:
ASSERT(false, "invalid pixel type %d", (int)fmt);
} }
#pragma GCC diagnostic pop
ASSERT(data, "invalid pixel type %d", (int)fmt);
m_data->m_pixels[(int)fmt] = data; m_data->m_pixels[(int)fmt] = data;
} }



+ 0
- 37
src/lol/gpu/texture.h View File

@@ -19,43 +19,6 @@
namespace lol namespace lol
{ {


enum class PixelFormat
{
/* XXX: make sure to update texture.cpp when this changes */
Unknown = 0,
Y_8,
RGB_8,
RGBA_8,
ARGB_8,
ABGR_8,
Y_F32,
RGB_F32,
RGBA_F32,
};

static inline uint8_t BytesPerPixel(PixelFormat format)
{
switch (format)
{
case PixelFormat::Y_8:
return 1;
case PixelFormat::RGB_8:
return 3;
case PixelFormat::RGBA_8:
case PixelFormat::ARGB_8:
case PixelFormat::ABGR_8:
return 4;
case PixelFormat::Y_F32:
return 4;
case PixelFormat::RGB_F32:
return 12;
case PixelFormat::RGBA_F32:
return 16;
default:
return 1;
}
};

struct TextureMagFilter struct TextureMagFilter
{ {
enum Value enum Value


+ 1
- 0
src/lol/image/all.h View File

@@ -11,6 +11,7 @@
#if !defined __LOL_IMAGE_ALL_H__ #if !defined __LOL_IMAGE_ALL_H__
#define __LOL_IMAGE_ALL_H__ #define __LOL_IMAGE_ALL_H__


#include <lol/image/pixel.h>
#include <lol/image/color.h> #include <lol/image/color.h>
#include <lol/image/image.h> #include <lol/image/image.h>




+ 0
- 8
src/lol/image/image.h View File

@@ -21,14 +21,6 @@
namespace lol namespace lol
{ {


template <PixelFormat T> struct PixelType { typedef void type; };
template<> struct PixelType<PixelFormat::Y_8> { typedef uint8_t type; };
template<> struct PixelType<PixelFormat::RGB_8> { typedef u8vec3 type; };
template<> struct PixelType<PixelFormat::RGBA_8> { typedef u8vec4 type; };
template<> struct PixelType<PixelFormat::Y_F32> { typedef float type; };
template<> struct PixelType<PixelFormat::RGB_F32> { typedef vec3 type; };
template<> struct PixelType<PixelFormat::RGBA_F32> { typedef vec4 type; };

enum class WrapMode : uint8_t enum class WrapMode : uint8_t
{ {
Clamp, Clamp,


+ 73
- 0
src/lol/image/pixel.h View File

@@ -0,0 +1,73 @@
//
// Lol Engine
//
// Copyright: (c) 2004-2014 Sam Hocevar <sam@hocevar.net>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Do What The Fuck You Want To
// Public License, Version 2, as published by Sam Hocevar. See
// http://www.wtfpl.net/ for more details.
//

//
// The Pixel-related classes
// -------------------------
//

#if !defined __LOL_IMAGE_PIXEL_H__
#define __LOL_IMAGE_PIXEL_H__

namespace lol
{

/* The pixel formats we know about */
enum class PixelFormat
{
/* XXX: make sure to update image.cpp and texture.cpp when this changes */
Unknown,
Y_8,
RGB_8,
RGBA_8,
Y_F32,
RGB_F32,
RGBA_F32,
};

/* Associated storage types for each pixel format */
template <PixelFormat T> struct PixelType { typedef void type; };
template<> struct PixelType<PixelFormat::Y_8> { typedef uint8_t type; };
template<> struct PixelType<PixelFormat::RGB_8> { typedef u8vec3 type; };
template<> struct PixelType<PixelFormat::RGBA_8> { typedef u8vec4 type; };
template<> struct PixelType<PixelFormat::Y_F32> { typedef float type; };
template<> struct PixelType<PixelFormat::RGB_F32> { typedef vec3 type; };
template<> struct PixelType<PixelFormat::RGBA_F32> { typedef vec4 type; };

/* Number of bytes used by each pixel format */
static inline uint8_t BytesPerPixel(PixelFormat format)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch"
switch (format)
{
case PixelFormat::Unknown:
break;
case PixelFormat::Y_8:
return 1;
case PixelFormat::RGB_8:
return 3;
case PixelFormat::RGBA_8:
return 4;
case PixelFormat::Y_F32:
return 4;
case PixelFormat::RGB_F32:
return 12;
case PixelFormat::RGBA_F32:
return 16;
}
return 0;
#pragma GCC diagnostic pop
};

} /* namespace lol */

#endif // __LOL_IMAGE_PIXEL_H__


+ 2
- 1
src/lolcore.vcxproj View File

@@ -164,7 +164,7 @@
<ClCompile Include="image\image.cpp" /> <ClCompile Include="image\image.cpp" />
<ClCompile Include="image\kernel.cpp" /> <ClCompile Include="image\kernel.cpp" />
<ClCompile Include="image\noise.cpp" /> <ClCompile Include="image\noise.cpp" />
<ClCompile Include="image\pixels.cpp" />
<ClCompile Include="image\pixel.cpp" />
<ClCompile Include="image\resample.cpp" /> <ClCompile Include="image\resample.cpp" />
<ClCompile Include="input\controller.cpp" /> <ClCompile Include="input\controller.cpp" />
<ClCompile Include="input\input.cpp" /> <ClCompile Include="input\input.cpp" />
@@ -313,6 +313,7 @@
<ClInclude Include="lol\image\all.h" /> <ClInclude Include="lol\image\all.h" />
<ClInclude Include="lol\image\color.h" /> <ClInclude Include="lol\image\color.h" />
<ClInclude Include="lol\image\image.h" /> <ClInclude Include="lol\image\image.h" />
<ClInclude Include="lol\image\pixel.h" />
<ClInclude Include="lol\math\all.h" /> <ClInclude Include="lol\math\all.h" />
<ClInclude Include="lol\math\array2d.h" /> <ClInclude Include="lol\math\array2d.h" />
<ClInclude Include="lol\math\constants.h" /> <ClInclude Include="lol\math\constants.h" />


+ 4
- 1
src/lolcore.vcxproj.filters View File

@@ -400,7 +400,7 @@
<ClCompile Include="image\resample.cpp"> <ClCompile Include="image\resample.cpp">
<Filter>image</Filter> <Filter>image</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="image\pixels.cpp">
<ClCompile Include="image\pixel.cpp">
<Filter>image</Filter> <Filter>image</Filter>
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
@@ -699,6 +699,9 @@
<ClInclude Include="lol\image\image.h"> <ClInclude Include="lol\image\image.h">
<Filter>lol\image</Filter> <Filter>lol\image</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="lol\image\pixel.h">
<Filter>lol\image</Filter>
</ClInclude>
<ClInclude Include="input\keys.h"> <ClInclude Include="input\keys.h">
<Filter>input</Filter> <Filter>input</Filter>
</ClInclude> </ClInclude>


Loading…
Cancel
Save