#include <iostream>
#include "initialization.hpp"
#include "image_loader.hpp"
class ExampleImage:public Initialization
{
public:
ExampleImage(DemoRunner *runner, int argc, char **argv);
~ExampleImage()
{}
virtual
void
draw_frame(void) override;
private:
};
ExampleImage::
ExampleImage(DemoRunner *runner, int argc, char **argv):
Initialization(runner, argc, argv)
{
ImageSourceSDL image_loader(argv[1]);
m_image = m_painter_engine_gl->image_atlas().create(image_loader.width(),
image_loader.height(),
image_loader);
}
void
ExampleImage::
draw_frame(void)
{
m_surface_gl->viewport(vwp);
brush
.
color(1.0f, 1.0f, 1.0f, 1.0f)
m_painter->fill_rect(brush,
.min_point(0.0f, 0.0f)
.max_point(window_dims));
m_painter->end();
fastuidraw_glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
fastuidraw_glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
m_surface_gl->blit_surface(GL_NEAREST);
}
int
main(int argc, char **argv)
{
DemoRunner demo_runner;
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " image_file\n";
return -1;
}
return demo_runner.main<ExampleImage>(argc, argv);
}
#include <vector>
#include <SDL_image.h>
{
public:
explicit
ImageSourceSDL(SDL_Surface *surface);
explicit
~ImageSourceSDL();
int
width(void) const;
int
height(void) const;
virtual
bool
virtual
unsigned int
virtual
void
unsigned int w, unsigned int h,
virtual
private:
class PerMipmapLevel
{
public:
PerMipmapLevel(int w, int h):
m_pixels(w * h),
m_width(w),
m_height(h),
m_stride(w)
{}
PerMipmapLevel(PerMipmapLevel &&src) noexcept:
m_pixels(std::move(src.m_pixels)),
m_width(src.m_width),
m_height(src.m_height),
m_stride(src.m_stride)
{}
PerMipmapLevel(const PerMipmapLevel &) = delete;
pixel(int x, int y)
{
clamp(x, y);
return m_pixels[x + y * m_stride];
}
pixel(int x, int y) const
{
clamp(x, y);
return m_pixels[x + y * m_stride];
}
int
width(void) const
{
return m_width;
}
int
height(void) const
{
return m_height;
}
private:
void
clamp(int &x, int &y) const
{
}
std::vector<fastuidraw::u8vec4> m_pixels;
int m_width, m_height, m_stride;
};
void
extract_image_data_from_surface(SDL_Surface *surface);
std::vector<PerMipmapLevel> m_image_data;
};
#include <iostream>
#include "image_loader.hpp"
static
inline
GetRGBA(Uint32 pixel, const SDL_PixelFormat *fmt)
{
Uint8 r, g, b, a;
SDL_GetRGBA(pixel, fmt, &r, &g, &b, &a);
}
static
inline
GetRGBA(const SDL_Surface *surface, int x, int y)
{
const unsigned char *surface_data;
Uint32 pixel, surface_offset;
surface_data = reinterpret_cast<const unsigned char*>(surface->pixels);
surface_offset = y * surface->pitch + x * surface->format->BytesPerPixel;
pixel = *reinterpret_cast<const Uint32*>(surface_data + surface_offset);
return GetRGBA(pixel, surface->format);
}
ImageSourceSDL::
ImageSourceSDL(SDL_Surface *surface)
{
extract_image_data_from_surface(surface);
}
ImageSourceSDL::
{
SDL_Surface *surface;
surface = IMG_Load(filename);
extract_image_data_from_surface(surface);
}
ImageSourceSDL::
~ImageSourceSDL()
{
}
void
ImageSourceSDL::
extract_image_data_from_surface(SDL_Surface *surface)
{
if (!surface || surface->w == 0 || surface->h == 0)
{
std::cerr << "Warning: unable to load image, substituting with an "
<< "image with width and height 1 whose only pixel is "
<< "(255, 255, 0, 255)\n";
m_image_data.push_back(PerMipmapLevel(1, 1));
return;
}
m_image_data.push_back(std::move(PerMipmapLevel(surface->w, surface->h)));
SDL_LockSurface(surface);
for (int y = 0; y < m_image_data.back().height(); ++y)
{
for (int x = 0; x < m_image_data.back().width(); ++x)
{
m_image_data.back().pixel(x, y) = GetRGBA(surface, x, y);
}
}
SDL_UnlockSurface(surface);
SDL_FreeSurface(surface);
while (m_image_data.back().width() > 1 && m_image_data.back().height() > 1)
{
int dst_w(m_image_data.back().width() / 2);
int dst_h(m_image_data.back().height() / 2);
m_image_data.push_back(std::move(PerMipmapLevel(dst_w, dst_h)));
const PerMipmapLevel &prev(m_image_data[m_image_data.size() - 2]);
for (int y = 0; y < dst_h; ++y)
{
for (int x = 0; x < dst_w; ++x)
{
int src_x(2 * x), src_y(2 * y);
avg = 0.25f * (p00 + p10 + p10 + p11);
}
}
}
}
int
ImageSourceSDL::
width(void) const
{
return m_image_data.front().width();
}
int
ImageSourceSDL::
height(void) const
{
return m_image_data.front().height();
}
unsigned int
ImageSourceSDL::
number_levels(void) const
{
return m_image_data.size();
}
ImageSourceSDL::
format(void) const
{
}
void
ImageSourceSDL::
unsigned int w, unsigned int h,
{
for (
int src_y = location.
y(), dst_y = 0; dst_y < h; ++dst_y, ++src_y)
{
for (
int src_x = location.
x(), dst_x = 0; dst_x < w; ++dst_x, ++src_x)
{
int dst_offset;
dst_offset = dst_y * w + dst_x;
dst[dst_offset] = m_image_data[level].pixel(src_x, src_y);
}
}
}
bool
ImageSourceSDL::
{
bool return_value(true);
*dst = m_image_data.
front().pixel(location.
x(), location.
y());
for (
int src_y = location.
y(), dst_y = 0; return_value && dst_y < square_size; ++dst_y, ++src_y)
{
for (
int src_x = location.
x(), dst_x = 0; return_value && dst_x < square_size; ++dst_x, ++src_x)
{
if (*dst != m_image_data.
front().pixel(src_x, src_y))
{
return_value = false;
}
}
}
return return_value;
}