Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
image.hpp
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <SDL.h>
7 #include <SDL_opengl.h> // for GLenum, GL_FALSE
8 #include <algorithm> // for max
9 
10 #include "SDL_pixels.h" // for SDL_PixelFormat
11 #include "SDL_surface.h" // for SDL_Surface, SDL_FreeSurface
12 #include "inexor/shared/cube_loops.hpp" // for loopi
13 #include "inexor/shared/cube_types.hpp" // for uchar, uint
14 
15 struct bvec;
16 struct vec;
17 
20 struct ImageData
21 {
22  int w, h, bpp, levels, align, pitch;
25  void *owner;
26  void(*freefunc)(void *);
27 
29  : data(nullptr), owner(nullptr), freefunc(nullptr)
30  {
31  }
32 
33 
34  ImageData(int nw, int nh, int nbpp, int nlevels = 1, int nalign = 0, GLenum ncompressed = GL_FALSE)
35  {
36  setdata(nullptr, nw, nh, nbpp, nlevels, nalign, ncompressed);
37  }
38 
39  ImageData(int nw, int nh, int nbpp, uchar *data)
40  : owner(nullptr), freefunc(nullptr)
41  {
42  setdata(data, nw, nh, nbpp);
43  }
44 
45  ImageData(SDL_Surface *s) { wrap(s); }
47 
48  void setdata(uchar *ndata, int nw, int nh, int nbpp, int nlevels = 1, int nalign = 0, GLenum ncompressed = GL_FALSE)
49  {
50  w = nw;
51  h = nh;
52  bpp = nbpp;
53  levels = nlevels;
54  align = nalign;
55  pitch = align ? 0 : w*bpp;
56  compressed = ncompressed;
57  data = ndata ? ndata : new uchar[calcsize()];
58  if (!ndata) { owner = this; freefunc = nullptr; }
59  }
60 
61  int calclevelsize(int level) const
62  {
63  return ((std::max(w >> level, 1) + align - 1) / align)*((std::max(h >> level, 1) + align - 1) / align)*bpp;
64  }
65 
66  int calcsize() const
67  {
68  if (!align) return w*h*bpp;
69  int lw = w, lh = h,
70  size = 0;
71  loopi(levels)
72  {
73  if (lw <= 0) lw = 1;
74  if (lh <= 0) lh = 1;
75  size += ((lw + align - 1) / align)*((lh + align - 1) / align)*bpp;
76  if (lw*lh == 1) break;
77  lw >>= 1;
78  lh >>= 1;
79  }
80  return size;
81  }
82 
83  void disown()
84  {
85  data = nullptr;
86  owner = nullptr;
87  freefunc = nullptr;
88  }
89 
90  void cleanup()
91  {
92  if (owner == this) delete[] data;
93  else if (freefunc) (*freefunc)(owner);
94  disown();
95  }
96 
98  {
99  cleanup();
100  *this = d;
101  if (owner == &d) owner = this;
102  d.disown();
103  }
104 
105  void wrap(SDL_Surface *s)
106  {
107  setdata((uchar *)s->pixels, s->w, s->h, s->format->BytesPerPixel);
108  pitch = s->pitch;
109  owner = s;
110  freefunc = (void(*)(void *))SDL_FreeSurface;
111  }
112 };
113 
114 extern void scaletexture(uchar *src, uint sw, uint sh, uint bpp, uint pitch, uchar *dst, uint dw, uint dh);
115 extern void resizetexture(int w, int h, bool mipmap, bool canreduce, GLenum target, int compress, int &tw, int &th);
116 extern void scaleimage(ImageData &s, int w, int h);
117 
118 extern void texoffset(ImageData &s, int xoffset, int yoffset);
119 
124 extern void texrotate(ImageData &s, int numrots, bool isnormalmap = false);
125 
131 extern void texreorient(ImageData &s, bool flipx, bool flipy, bool swapxy, bool isnormalmap = false);
132 
133 extern void texflip(ImageData &s);
134 
135 extern void texmad(ImageData &s, const vec &mul, const vec &add);
136 extern void texcolorify(ImageData &s, const vec &color, vec weights);
137 extern void texcolormask(ImageData &s, const vec &color1, const vec &color2);
138 extern void texdup(ImageData &s, int srcchan, int dstchan);
139 extern void texmix(ImageData &s, int c1, int c2, int c3, int c4);
140 extern void texgrey(ImageData &s);
141 extern void texpremul(ImageData &s);
142 extern void texagrad(ImageData &s, float x2, float y2, float x1, float y1);
143 extern void texnormal(ImageData &s, int emphasis);
144 extern void texblur(ImageData &s, int n, int r);
145 
146 extern void forcergbimage(ImageData &s);
147 
148 extern void blurtexture(int n, int bpp, int w, int h, uchar *dst, const uchar *src, int margin = 0);
149 extern void blurnormals(int n, int w, int h, bvec *dst, const bvec *src, int margin = 0);
150 
151 
typedef void(APIENTRYP PFNGLUNIFORMMATRIX2X3FVPROC)(GLint location
void texgrey(ImageData &s)
Definition: image.cpp:387
void resizetexture(int w, int h, bool mipmap, bool canreduce, GLenum target, int compress, int &tw, int &th)
Definition: image.cpp:137
const T & max(const inexor::rpc::SharedVar< T > &a, const T &b)
Definition: SharedVar.hpp:224
void texmad(ImageData &s, const vec &mul, const vec &add)
Definition: image.cpp:334
typedef GLenum(APIENTRYP PFNGLCHECKFRAMEBUFFERSTATUSPROC)(GLenum target)
uchar * data
Definition: image.hpp:24
unsigned int uint
Definition: cube_types.hpp:9
void forcergbimage(ImageData &s)
Definition: image.cpp:596
int h
Definition: image.hpp:22
Structure holding the Raw Pixel Data and minimum image info.
Definition: image.hpp:20
void texpremul(ImageData &s)
Definition: image.cpp:405
void disown()
Definition: image.hpp:83
int pitch
Definition: image.hpp:22
void texreorient(ImageData &s, bool flipx, bool flipy, bool swapxy, bool isnormalmap=false)
Definition: image.cpp:277
void blurtexture(int n, int bpp, int w, int h, uchar *dst, const uchar *src, int margin=0)
Definition: image.cpp:565
int calclevelsize(int level) const
Definition: image.hpp:61
GLenum compressed
Definition: image.hpp:23
int w
Definition: image.hpp:22
void(* freefunc)(void *)
Definition: image.hpp:26
void texflip(ImageData &s)
Definition: image.cpp:457
int bpp
Definition: image.hpp:22
ImageData()
Definition: image.hpp:28
void texdup(ImageData &s, int srcchan, int dstchan)
Definition: image.cpp:364
else loopi(numargs)
Definition: command.cpp:3019
void * owner
Definition: image.hpp:25
vector with 3 floats and some useful methods.
Definition: geom.hpp:110
void texnormal(ImageData &s, int emphasis)
Definition: image.cpp:470
int calcsize() const
Definition: image.hpp:66
void texmix(ImageData &s, int c1, int c2, int c3, int c4)
Definition: image.cpp:370
void wrap(SDL_Surface *s)
Definition: image.hpp:105
void texoffset(ImageData &s, int xoffset, int yoffset)
Definition: image.cpp:315
int align
Definition: image.hpp:22
3-dimensional UNSIGNED CHAR vector.
Definition: geom.hpp:1398
ImageData(int nw, int nh, int nbpp, uchar *data)
Definition: image.hpp:39
int levels
Definition: image.hpp:22
ImageData(SDL_Surface *s)
Definition: image.hpp:45
void texrotate(ImageData &s, int numrots, bool isnormalmap=false)
Definition: image.cpp:305
int d
Definition: octaedit.cpp:1749
void texcolorify(ImageData &s, const vec &color, vec weights)
Definition: image.cpp:342
void texcolormask(ImageData &s, const vec &color1, const vec &color2)
Definition: image.cpp:352
unsigned char uchar
Basic type definitions.
Definition: cube_types.hpp:7
~ImageData()
Definition: image.hpp:46
void setdata(uchar *ndata, int nw, int nh, int nbpp, int nlevels=1, int nalign=0, GLenum ncompressed=GL_FALSE)
Definition: image.hpp:48
GLuint GLuint GLintptr GLsizeiptr size
Definition: glexts.hpp:412
void cleanup()
Definition: image.hpp:90
void texagrad(ImageData &s, float x2, float y2, float x1, float y1)
Definition: image.cpp:425
GLenum GLenum GLuint GLint level
Definition: glexts.hpp:296
void compress(const chunk *str, chunk state[3])
Definition: crypto.cpp:36
void scaletexture(uchar *src, uint sw, uint sh, uint bpp, uint pitch, uchar *dst, uint dw, uint dh)
Definition: image.cpp:103
static void color(const bvec &v, uchar alpha=255)
Definition: glemu.hpp:71
void blurnormals(int n, int w, int h, bvec *dst, const bvec *src, int margin=0)
Definition: image.cpp:576
void scaleimage(ImageData &s, int w, int h)
Definition: image.cpp:610
void replace(ImageData &d)
Definition: image.hpp:97
bool target(fpsent *d, aistate &b, int pursue=0, bool force=false, float mindist=0.f)
Definition: ai.cpp:393
void texblur(ImageData &s, int n, int r)
Definition: image.cpp:585
ImageData(int nw, int nh, int nbpp, int nlevels=1, int nalign=0, GLenum ncompressed=GL_FALSE)
Definition: image.hpp:34