Tidy up PixelAttributes

This commit is contained in:
Unknown 2018-05-10 07:20:04 +02:00
parent b07df84d68
commit d40bd789e8
3 changed files with 52 additions and 62 deletions

View File

@ -7,20 +7,13 @@
* ===================================================================== * =====================================================================
*/ */
#include <cstdlib>
#include <cstring>
#include <iostream>
#include "PixelAttributes.h" #include "PixelAttributes.h"
#include <cstring> // memcpy
using namespace std; using namespace std;
PixelAttribute::AlphaMixingMode PixelAttribute::m_mixMode = PixelAttribute::AlphaMixCumulative; PixelAttribute::AlphaMixingMode PixelAttribute::m_mixMode = PixelAttribute::AlphaMixCumulative;
PixelAttributes::PixelAttributes():
m_pixelAttributes(0)
{
}
PixelAttributes::~PixelAttributes() PixelAttributes::~PixelAttributes()
{ {
freeAttributes(); freeAttributes();
@ -31,7 +24,7 @@ void PixelAttributes::setParameters(int width, int lines, int nextY, int scale,
freeAttributes(); freeAttributes();
m_width = width + 1; // 1px gradient calculation m_width = width + 1; // 1px gradient calculation
m_previousLine = 0; m_previousLine = 0;
m_firstLine = 1; //m_firstLine = 1;
m_lastLine = m_firstLine + lines - 1; m_lastLine = m_firstLine + lines - 1;
m_emptyLine = m_lastLine + 1; m_emptyLine = m_lastLine + 1;
m_lineCount = m_emptyLine + 1; m_lineCount = m_emptyLine + 1;
@ -88,12 +81,12 @@ void PixelAttributes::freeAttributes()
{ {
if (m_pixelAttributes) { if (m_pixelAttributes) {
for (int i = 0; i < m_lineCount; ++i) { for (int i = 0; i < m_lineCount; ++i) {
if (m_pixelAttributes[i] != 0) { if (m_pixelAttributes[i] != nullptr) {
delete[] m_pixelAttributes[i]; delete[] m_pixelAttributes[i];
} }
} }
delete[] m_pixelAttributes; delete[] m_pixelAttributes;
m_pixelAttributes = 0; m_pixelAttributes = nullptr;
} }
} }
@ -183,17 +176,17 @@ void PixelAttributes::renderShading(double emphasis, bool drawAlpha)
// Color values with n>0 can be summed. // Color values with n>0 can be summed.
// normalize() converts from n>0 to n==0 representation // normalize() converts from n>0 to n==0 representation
void PixelAttribute::normalize(double count, Color defColor) void PixelAttribute::normalize(double count, Color defaultColor)
{ {
if (!m_n) { if (!m_n) {
// Already normalized // Already normalized
return; return;
} }
if (m_n < count) { if (m_n < count) {
m_r += (defColor.r / 255.0) * (defColor.a / 255.0) * (count - m_n); m_r += (defaultColor.r / 255.0) * (defaultColor.a / 255.0) * (count - m_n);
m_g += (defColor.g / 255.0) * (defColor.a / 255.0) * (count - m_n); m_g += (defaultColor.g / 255.0) * (defaultColor.a / 255.0) * (count - m_n);
m_b += (defColor.b / 255.0) * (defColor.a / 255.0) * (count - m_n); m_b += (defaultColor.b / 255.0) * (defaultColor.a / 255.0) * (count - m_n);
m_a += (defColor.a / 255.0) * (count - m_n); m_a += (defaultColor.a / 255.0) * (count - m_n);
m_h *= double(count) / m_n; m_h *= double(count) / m_n;
m_t *= double(count) / m_n; m_t *= double(count) / m_n;
m_n = count; m_n = count;

View File

@ -7,16 +7,14 @@
* ===================================================================== * =====================================================================
*/ */
#ifndef PIXELATTRIBUTES_H_ADZ35GYF #pragma once
#define PIXELATTRIBUTES_H_ADZ35GYF
#include <limits> #include "Color.h"
#include <cassert>
#include <cmath> #include <cmath>
#include <cstdint> #include <cstdint>
#include <limits>
#include <stdexcept> #include <stdexcept>
#include <cassert>
#include "config.h"
#include "Color.h"
class PixelAttribute { class PixelAttribute {
public: public:
@ -27,25 +25,25 @@ public:
AlphaMixAverage = 0x04, AlphaMixAverage = 0x04,
}; };
static void setMixMode(AlphaMixingMode mode); static void setMixMode(AlphaMixingMode mode);
PixelAttribute(): nextEmpty(true), m_n(0), m_h(NAN), m_t(0), m_a(0), m_r(0), m_g(0), m_b(0) {}; PixelAttribute() = default;
// PixelAttribute(const PixelAttribute &p); // PixelAttribute(const PixelAttribute &p);
PixelAttribute(const Color &color, double height); PixelAttribute(const Color &color, double height);
PixelAttribute(const ColorEntry &entry, double height); PixelAttribute(const ColorEntry &entry, double height);
bool nextEmpty; bool nextEmpty{true};
double h(void) const { return m_h / (m_n ? m_n : 1); } double h() const { return m_h / (m_n ? m_n : 1); }
double t(void) const { return m_t / (m_n ? m_n : 1); } double t() const { return m_t / (m_n ? m_n : 1); }
double a(void) const { return m_a / (m_n ? m_n : 1); } double a() const { return m_a / (m_n ? m_n : 1); }
double r(void) const { return m_r / (m_n ? m_a : 1); } double r() const { return m_r / (m_n ? m_a : 1); }
double g(void) const { return m_g / (m_n ? m_a : 1); } double g() const { return m_g / (m_n ? m_a : 1); }
double b(void) const { return m_b / (m_n ? m_a : 1); } double b() const { return m_b / (m_n ? m_a : 1); }
uint8_t red(void) const { return int(r() * 255 + 0.5); } uint8_t red() const { return int(r() * 255 + 0.5); }
uint8_t green(void) const { return int(g() * 255 + 0.5); } uint8_t green() const { return int(g() * 255 + 0.5); }
uint8_t blue(void) const { return int(b() * 255 + 0.5); } uint8_t blue() const { return int(b() * 255 + 0.5); }
uint8_t alpha(void) const { return int(a() * 255 + 0.5); } uint8_t alpha() const { return int(a() * 255 + 0.5); }
uint8_t thicken(void) const { return int(t() * 255 + 0.5); } uint8_t thicken() const { return int(t() * 255 + 0.5); }
unsigned height(void) const { return unsigned(h() + 0.5); } unsigned height() const { return unsigned(h() + 0.5); }
bool isNormalized(void) const { return !m_n; } bool isNormalized() const { return !m_n; }
Color color(void) const { return Color(red(), green(), blue(), alpha()); } Color color() const { return Color(red(), green(), blue(), alpha()); }
inline bool is_valid() const { return !std::isnan(m_h); } inline bool is_valid() const { return !std::isnan(m_h); }
PixelAttribute &operator=(const PixelAttribute &p); PixelAttribute &operator=(const PixelAttribute &p);
@ -54,13 +52,13 @@ public:
void mixUnder(const PixelAttribute &p); void mixUnder(const PixelAttribute &p);
private: private:
static AlphaMixingMode m_mixMode; static AlphaMixingMode m_mixMode;
double m_n; double m_n{0};
double m_h; double m_h{ std::numeric_limits<double>::quiet_NaN() };
double m_t; double m_t{0};
double m_a; double m_a{0};
double m_r; double m_r{0};
double m_g; double m_g{0};
double m_b; double m_b{0};
friend class PixelAttributes; friend class PixelAttributes;
}; };
@ -68,33 +66,33 @@ friend class PixelAttributes;
class PixelAttributes class PixelAttributes
{ {
public: public:
PixelAttributes(); PixelAttributes() = default;
virtual ~PixelAttributes(); virtual ~PixelAttributes();
void setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty); void setParameters(int width, int lines, int nextY, int scale, bool defaultEmpty);
void scroll(int keepY); void scroll(int keepY);
PixelAttribute &attribute(int y, int x); PixelAttribute &attribute(int y, int x);
void renderShading(double emphasis, bool drawAlpha); void renderShading(double emphasis, bool drawAlpha);
int getNextY(void) { return m_nextY; } int getNextY() { return m_nextY; }
void setLastY(int y); void setLastY(int y);
int getLastY(void) { return m_lastY; } int getLastY() { return m_lastY; }
private: private:
int yCoord2Line(int y) { return y - m_firstY + m_firstLine; } int yCoord2Line(int y) { return y - m_firstY + m_firstLine; }
void freeAttributes(); void freeAttributes();
private: private:
int m_previousLine; int m_previousLine{};
int m_firstLine; const int m_firstLine{1};
int m_lastLine; int m_lastLine{};
int m_emptyLine; int m_emptyLine{};
int m_lineCount; int m_lineCount{};
PixelAttribute **m_pixelAttributes; PixelAttribute **m_pixelAttributes{nullptr};
int m_width; int m_width{};
int m_firstY; int m_firstY{};
int m_nextY; int m_nextY{};
int m_lastY; int m_lastY{};
int m_firstUnshadedY; int m_firstUnshadedY{};
int m_scale; int m_scale{};
}; };
inline void PixelAttributes::setLastY(int y) inline void PixelAttributes::setLastY(int y)
@ -157,5 +155,3 @@ inline PixelAttribute &PixelAttribute::operator=(const PixelAttribute &p)
return *this; return *this;
} }
#endif /* end of include guard: PIXELATTRIBUTES_H_ADZ35GYF */

View File

@ -25,6 +25,7 @@
#include "MapBlock.h" #include "MapBlock.h"
#include "PaintEngine.h" #include "PaintEngine.h"
#include "PixelAttributes.h" #include "PixelAttributes.h"
#include "config.h"
#include "db.h" #include "db.h"
#define TILESIZE_CHUNK (INT_MIN) #define TILESIZE_CHUNK (INT_MIN)