You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.3 KiB
116 lines
2.3 KiB
12 years ago
|
//
|
||
|
// basics.hpp
|
||
|
// ImageStreams
|
||
|
//
|
||
|
// Created by Joshua Moerman on 9/7/12.
|
||
|
// Copyright (c) 2012 Vadovas. All rights reserved.
|
||
|
//
|
||
|
|
||
|
#ifndef ImageStreams_basics_hpp
|
||
|
#define ImageStreams_basics_hpp
|
||
|
|
||
|
/*
|
||
|
A PixelFormat p should have the following constructors (depending on number
|
||
|
of colors):
|
||
|
p(int r, int g, int b, int a)
|
||
|
p(double r, double g, double b, double a)
|
||
|
|
||
|
p(int v)
|
||
|
p(double v)
|
||
|
|
||
12 years ago
|
The order in the ctor is rgb even though the internal structure may be
|
||
|
different
|
||
|
|
||
12 years ago
|
It should be trivially copyable (for memcpy).
|
||
12 years ago
|
|
||
|
Furthermore it should have a static constexpr size_t num_colors indicating the
|
||
|
number of colors and a static constexpr size_t bit_per_color indicating the
|
||
|
number of bits per color (TODO: what to do with encodings like 565 ?)
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include <cstdint>
|
||
|
#include <algorithm>
|
||
|
|
||
12 years ago
|
namespace pixel_formats {
|
||
|
inline uint8_t clamp(int n){
|
||
|
return std::min(255, std::max(0, n));
|
||
|
}
|
||
|
|
||
12 years ago
|
struct gray {
|
||
12 years ago
|
static constexpr size_t num_colors = 1;
|
||
|
static constexpr size_t bits_per_color = 8;
|
||
|
|
||
12 years ago
|
gray() : value(0) {}
|
||
|
gray(double intensity) : value(clamp(255*intensity)) {}
|
||
|
gray(int intensity) : value(clamp(intensity)) {}
|
||
12 years ago
|
|
||
|
private:
|
||
|
uint8_t value;
|
||
|
};
|
||
|
|
||
12 years ago
|
struct rgb {
|
||
12 years ago
|
static constexpr size_t num_colors = 3;
|
||
|
static constexpr size_t bits_per_color = 8;
|
||
|
|
||
12 years ago
|
rgb()
|
||
12 years ago
|
: red(0)
|
||
|
, green(0)
|
||
|
, blue(0)
|
||
|
{}
|
||
|
|
||
12 years ago
|
rgb(double red, double green, double blue)
|
||
12 years ago
|
: red(clamp(255*red))
|
||
|
, green(clamp(255*green))
|
||
|
, blue(clamp(255*blue))
|
||
|
{}
|
||
|
|
||
12 years ago
|
rgb(int red, int green, int blue)
|
||
12 years ago
|
: red(clamp(red))
|
||
|
, green(clamp(green))
|
||
|
, blue(clamp(blue))
|
||
|
{}
|
||
|
|
||
|
private:
|
||
|
uint8_t red;
|
||
|
uint8_t green;
|
||
|
uint8_t blue;
|
||
|
};
|
||
|
|
||
12 years ago
|
struct bgr{
|
||
12 years ago
|
static constexpr size_t num_colors = 3;
|
||
|
static constexpr size_t bits_per_color = 8;
|
||
|
|
||
12 years ago
|
bgr()
|
||
12 years ago
|
: blue(0)
|
||
|
, green(0)
|
||
|
, red(0)
|
||
|
{}
|
||
|
|
||
12 years ago
|
bgr(double red, double green, double blue)
|
||
12 years ago
|
: blue(clamp(255*blue))
|
||
|
, green(clamp(255*green))
|
||
|
, red(clamp(255*red))
|
||
|
{}
|
||
|
|
||
12 years ago
|
bgr(int red, int green, int blue)
|
||
12 years ago
|
: blue(clamp(blue))
|
||
|
, green(clamp(green))
|
||
|
, red(clamp(red))
|
||
|
{}
|
||
|
|
||
|
private:
|
||
|
uint8_t blue;
|
||
|
uint8_t green;
|
||
|
uint8_t red;
|
||
|
};
|
||
|
|
||
|
template <typename PixelType>
|
||
|
struct traits {
|
||
|
static constexpr size_t num_colors = PixelType::num_colors;
|
||
|
static constexpr size_t bits_per_color = PixelType::bits_per_color;
|
||
|
static constexpr size_t bits_per_pixel = num_colors * bits_per_color;
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif
|