Allow for multiple palette files rather than hard coding to flam3-palettes.xml.

Make xaos display a matrix rather than a single column. This will be moved out into its own tab since it's no longer xform dependent, but that will be in a future commit.

Remove xaos from/to button since it's no longer applicable.

Add test function to Isaac to return just one random byte. Might be used in the future.
This commit is contained in:
mfeemster
2015-04-08 18:23:29 -07:00
parent 4067422075
commit 053a9fcf95
19 changed files with 431 additions and 386 deletions

View File

@ -46,8 +46,8 @@ template<> unique_ptr<QTIsaac<ISAAC_SIZE, ISAAC_INT>> QTIsaac<ISAAC_SIZE, ISAAC_
template EMBER_API class Post##varName##Variation<T>;
#define EXPORT_SINGLE_TYPE_EMBER(T) \
template<> bool PaletteList<T>::m_Init = false; \
template<> vector<Palette<T>> PaletteList<T>::m_Palettes = vector<Palette<T>>(); \
template<> const char* PaletteList<T>::m_DefaultFilename = "flam3-palettes.xml"; \
template<> map<string, vector<Palette<T>>> PaletteList<T>::m_Palettes = map<string, vector<Palette<T>>>(); \
template<> bool XmlToEmber<T>::m_Init = false; \
template<> vector<string> XmlToEmber<T>::m_FlattenNames = vector<string>(); \
template<> vector<pair<string, string>> XmlToEmber<T>::m_BadParamNames = vector<pair<string, string>>(); \

View File

@ -40,6 +40,7 @@
#else
#include <malloc.h>
#endif
#include <map>
#include <math.h>
#include <memory>
#include <numeric>

View File

@ -41,6 +41,13 @@
namespace EmberNs
{
union UintBytes
{
unsigned char Bytes[4];
uint Uint;
};
/// <summary>
/// QTIsaac class which allows using ISAAC in an OOP manner.
/// </summary>
@ -49,6 +56,8 @@ class EMBER_API QTIsaac
{
public:
enum { N = (1 << ALPHA) };
UintBytes m_Cache;
int m_LastIndex;
/// <summary>
/// Global ISAAC RNG to be used from anywhere. This is not thread safe, so take caution to only
@ -83,6 +92,28 @@ public:
QTIsaac(T a = 0, T b = 0, T c = 0, T* s = nullptr)
{
Srand(a, b, c, s);
m_LastIndex = 0;
m_Cache.Uint = Rand();
T temp = RandByte();//Need to call at least once so other libraries can link.
}
/// <summary>
/// Return the next random integer in the range of 0-255.
/// If only a byte is needed, this is a more efficient way because
/// it only calls rand 1/4 of the time.
/// </summary>
/// <returns>The next random integer in the range of 0-255</returns>
inline T RandByte()
{
T ret = m_Cache.Bytes[m_LastIndex++];
if (m_LastIndex == 4)
{
m_LastIndex = 0;
m_Cache.Uint = Rand();
}
return ret;
}
/// <summary>
@ -261,7 +292,7 @@ public:
if (s == nullptr)//Default to using time plus index as the seed if s was nullptr.
{
for (int i = 0; i < N; i++)
m_Rc.randrsl[i] = static_cast<T>(time(nullptr)) + i;
m_Rc.randrsl[i] = static_cast<T>(NowMs()) + i;
}
else
{
@ -272,9 +303,9 @@ public:
#ifndef ISAAC_FLAM3_DEBUG
if (a == 0 && b == 0 && c == 0)
{
m_Rc.randa = static_cast<T>(time(nullptr));
m_Rc.randb = static_cast<T>(time(nullptr)) * static_cast<T>(time(nullptr));
m_Rc.randc = static_cast<T>(time(nullptr)) * static_cast<T>(time(nullptr)) * static_cast<T>(time(nullptr));
m_Rc.randa = static_cast<T>(NowMs());
m_Rc.randb = static_cast<T>(NowMs()) * static_cast<T>(NowMs());
m_Rc.randc = static_cast<T>(NowMs()) * static_cast<T>(NowMs()) * static_cast<T>(NowMs());
}
else
#endif

View File

@ -18,11 +18,14 @@ template <typename T>
class EMBER_API PaletteList : public EmberReport
{
public:
static const char* m_DefaultFilename;
/// <summary>
/// Empty constructor which does nothing.
/// Empty constructor which initializes the palette map with the default palette file.
/// </summary>
PaletteList()
{
Add(string(m_DefaultFilename));
}
/// <summary>
@ -31,17 +34,16 @@ public:
/// </summary>
/// <param name="filename">The full path to the file to read</param>
/// <param name="force">If true, override the initialization state and force a read, else observe the initialization state.</param>
/// <returns>The initialization state</returns>
bool Init(const string& filename, bool force = false)
/// <returns>Whether anything was read</returns>
bool Add(const string& filename, bool force = false)
{
if (!m_Init || force)
{
const char* loc = __FUNCTION__;
bool added = false;
auto& palettes = m_Palettes[filename];
m_Init = false;
m_Palettes.clear();
m_ErrorReport.clear();
if (palettes.empty() || force)
{
string buf;
const char* loc = __FUNCTION__;
if (ReadFile(filename.c_str(), buf))
{
@ -51,10 +53,11 @@ public:
{
xmlNode* rootNode = xmlDocGetRootElement(doc);
m_Palettes.reserve(buf.size() / 2048);//Roughly what it takes per palette.
ParsePalettes(rootNode);
palettes.clear();
palettes.reserve(buf.size() / 2048);//Roughly what it takes per palette.
ParsePalettes(rootNode, palettes);
xmlFreeDoc(doc);
m_Init = m_ErrorReport.empty();
added = true;
}
else
{
@ -67,54 +70,81 @@ public:
}
}
return m_Init;
return added;
}
/// <summary>
/// Gets the palette at a specified index.
/// Get the palette at a random index in a random file in the map.
/// </summary>
/// <param name="i">The index of the palette to read. A value of -1 indicates a random palette.</param>
/// <returns>A pointer to the requested palette if the index was in range, else nullptr.</returns>
Palette<T>* GetPalette(int i)
Palette<T>* GetRandomPalette()
{
if (!m_Palettes.empty())
auto p = m_Palettes.begin();
int i = 0, paletteFileIndex = QTIsaac<ISAAC_SIZE, ISAAC_INT>::GlobalRand->Rand() % Size();
while (i < paletteFileIndex && p != m_Palettes.end())
{
if (i == -1)
return &m_Palettes[QTIsaac<ISAAC_SIZE, ISAAC_INT>::GlobalRand->Rand() % Size()];
else if (i < int(m_Palettes.size()))
return &m_Palettes[i];
++i;
++p;
}
if (i < Size())
{
int paletteIndex = QTIsaac<ISAAC_SIZE, ISAAC_INT>::GlobalRand->Rand() % p->second.size();
if (paletteIndex < p->second.size())
return &p->second[paletteIndex];
}
return nullptr;
}
/// <summary>
/// Gets a pointer to a palette with a specified name.
/// Get the palette at a specified index in the specified file in the map.
/// </summary>
/// <param name="name">The name of the palette to retrieve</param>
/// <returns>A pointer to the palette if found, else nullptr</returns>
Palette<T>* GetPaletteByName(const string&& name)
/// <param name="filename">The filename of the palette to retrieve</param>
/// <param name="i">The index of the palette to read. A value of -1 indicates a random palette.</param>
/// <returns>A pointer to the requested palette if the index was in range, else nullptr.</returns>
Palette<T>* GetPalette(const string& filename, int i)
{
for (uint i = 0; i < Size(); i++)
if (m_Palettes[i].m_Name == name)
return &m_Palettes[i];
auto& palettes = m_Palettes[filename];
if (!palettes.empty() && i < int(palettes.size()))
return &palettes[i];
return nullptr;
}
/// <summary>
/// Gets a copy of the palette at a specified index with its hue adjusted by the specified amount.
/// Get a pointer to a palette with a specified name in the specified file in the map.
/// </summary>
/// <param name="i">The index of the palette to read. A value of -1 indicates a random palette.</param>
/// <param name="filename">The filename of the palette to retrieve</param>
/// <param name="name">The name of the palette to retrieve</param>
/// <returns>A pointer to the palette if found, else nullptr</returns>
Palette<T>* GetPaletteByName(const string& filename, const string& name)
{
for (auto& palettes : m_Palettes)
if (palettes.first == filename)
for (auto& palette : palettes.second)
if (palette.m_Name == name)
return &palette;
return nullptr;
}
/// <summary>
/// Get a copy of the palette at a specified index in the specified file in the map
/// with its hue adjusted by the specified amount.
/// </summary>
/// <param name="filename">The filename of the palette to retrieve</param>
/// <param name="i">The index of the palette to read.</param>
/// <param name="hue">The hue adjustment to apply</param>
/// <param name="palette">The palette to store the output</param>
/// <returns>True if successful, else false.</returns>
bool GetHueAdjustedPalette(int i, T hue, Palette<T>& palette)
bool GetHueAdjustedPalette(const string& filename, int i, T hue, Palette<T>& palette)
{
bool b = false;
Palette<T>* unadjustedPal = GetPalette(i);
if (unadjustedPal)
if (Palette<T>* unadjustedPal = GetPalette(filename, i))
{
unadjustedPal->MakeHueAdjustedPalette(palette, hue);
b = true;
@ -129,23 +159,72 @@ public:
void Clear()
{
m_Palettes.clear();
m_Init = false;
}
/// <summary>
/// Accessors.
/// Get the size of the palettes map.
/// This will be the number of files read.
/// </summary>
bool Init() { return m_Init; }
/// <returns>The size of the palettes map</returns>
size_t Size() { return m_Palettes.size(); }
/// <summary>
/// Get the size of specified palette vector in the palettes map.
/// </summary>
/// <param name="index">The index of the palette in the map to retrieve</param>
/// <returns>The size of the palette vector at the specified index in the palettes map</returns>
size_t Size(size_t index)
{
size_t i = 0;
auto p = m_Palettes.begin();
while (i < index && p != m_Palettes.end())
{
++i;
++p;
}
return p->second.size();
}
/// <summary>
/// Get the size of specified palette vector in the palettes map.
/// </summary>
/// <param name="s">The filename of the palette in the map to retrieve</param>
/// <returns>The size of the palette vector at the specified index in the palettes map</returns>
size_t Size(const string& s)
{
return m_Palettes[s].size();
}
/// <summary>
/// Get the name of specified palette in the palettes map.
/// </summary>
/// <param name="index">The index of the palette in the map to retrieve</param>
/// <returns>The name of the palette vector at the specified index in the palettes map</returns>
const string& Name(size_t index)
{
size_t i = 0;
auto p = m_Palettes.begin();
while (i < index && p != m_Palettes.end())
{
++i;
++p;
}
return p->first;
}
private:
/// <summary>
/// Parses an Xml node for all palettes present and stores in the palette list.
/// Parses an Xml node for all palettes present and store in the passed in palette vector.
/// Note that although the Xml color values are expected to be 0-255, they are converted and
/// stored as normalized colors, with values from 0-1.
/// </summary>
/// <param name="node">The parent note of all palettes in the Xml file.</param>
void ParsePalettes(xmlNode* node)
/// <param name="palettes">The vector to store the paresed palettes associated with this file in.</param>
void ParsePalettes(xmlNode* node, vector<Palette<T>>& palettes)
{
bool hexError = false;
char* val;
@ -208,19 +287,18 @@ private:
if (!hexError)
{
m_Palettes.push_back(palette);
palettes.push_back(palette);
}
}
else
{
ParsePalettes(node->children);
ParsePalettes(node->children, palettes);
}
node = node->next;
}
}
static bool m_Init;//Initialized to false in Ember.cpp, and will be set to true upon successful reading of an Xml palette file.
static vector<Palette<T>> m_Palettes;//The vector that stores the palettes.
static map<string, vector<Palette<T>>> m_Palettes;//The map of filenames to vectors that store the palettes.
};
}

View File

@ -72,7 +72,7 @@ public:
m_OffsetX = 0;
m_OffsetY = 0;
m_PaletteList.Init(palettePath);
m_PaletteList.Add(palettePath);
m_StandardIterator = unique_ptr<StandardIterator<T>>(new StandardIterator<T>());
m_XaosIterator = unique_ptr<XaosIterator<T>>(new XaosIterator<T>());
m_Renderer = unique_ptr<Renderer<T, bucketT>>(renderer);
@ -99,8 +99,8 @@ public:
ember.AddXform(xform2);
ember.AddXform(xform3);
if (m_PaletteList.Init())
ember.m_Palette = *m_PaletteList.GetPalette(-1);
if (m_PaletteList.Size())
ember.m_Palette = *m_PaletteList.GetRandomPalette();
return ember;
}
@ -386,8 +386,8 @@ public:
{
Palette<T> palette;
if (m_PaletteList.Init())
palette = *m_PaletteList.GetPalette(-1);
if (m_PaletteList.Size())
palette = *m_PaletteList.GetRandomPalette();
palette.MakeHueAdjustedPalette(ember.m_Palette, ember.m_Hue);
@ -640,8 +640,8 @@ public:
ember.Clear();
ember.m_Hue = (m_Rand.Rand() & 7) ? 0 : m_Rand.Frand01<T>();
if (m_PaletteList.Init())
palette = *m_PaletteList.GetPalette(-1);
if (m_PaletteList.Size())
palette = *m_PaletteList.GetRandomPalette();
palette.MakeHueAdjustedPalette(ember.m_Palette, ember.m_Hue);
ember.m_Time = 0;
@ -926,8 +926,8 @@ public:
ember.m_Hue = 0.0;
if (m_PaletteList.Init())
palette = m_PaletteList.GetPalette(-1);
if (m_PaletteList.Size())
palette = m_PaletteList.GetRandomPalette();
if (palette)
{

View File

@ -44,6 +44,13 @@ static inline size_t SizeOf(vector<T>& vec)
return sizeof(vec[0]) * vec.size();
}
/// <summary>
/// Thin wrapper around getting the current time in milliseconds.
/// </summary>
static inline size_t NowMs()
{
return duration_cast<milliseconds>(Clock::now().time_since_epoch()).count();
}
/// <summary>
/// After a run completes, information about what was run can be saved as strings to the comments
/// section of a jpg or png file. This class is just a container for those values.

View File

@ -344,7 +344,7 @@ public:
string buf;
//Ensure palette list is setup first.
if (!m_PaletteList.Init())
if (!m_PaletteList.Size())
{
m_ErrorReport.push_back(string(loc) + " : Palette list must be initialized before parsing embers.");
return false;
@ -515,7 +515,7 @@ private:
if (currentEmber.PaletteIndex() != -1)
{
if (!m_PaletteList.GetHueAdjustedPalette(currentEmber.PaletteIndex(), currentEmber.m_Hue, currentEmber.m_Palette))
if (!m_PaletteList.GetHueAdjustedPalette(PaletteList<T>::m_DefaultFilename, currentEmber.PaletteIndex(), currentEmber.m_Hue, currentEmber.m_Palette))
{
m_ErrorReport.push_back(string(loc) + " : Error assigning palette with index " + Itos(currentEmber.PaletteIndex()));
}
@ -834,8 +834,6 @@ private:
//Make sure BOTH are not specified, otherwise either are ok.
int numColors = 0;
int numBytes = 0;
bool oldFormat = false;
bool newFormat = false;
int index0, index1;
T hue0, hue1;
T blend = 0.5;
@ -855,40 +853,12 @@ private:
{
attStr = reinterpret_cast<char*>(xmlGetProp(childNode, curAtt->name));
if (!Compare(curAtt->name, "index0"))
if (!Compare(curAtt->name, "count"))
{
oldFormat = true;
Atoi(attStr, index0);
}
else if (!Compare(curAtt->name, "index1"))
{
oldFormat = true;
Atoi(attStr, index1);
}
else if (!Compare(curAtt->name, "hue0"))
{
oldFormat = true;
Atof(attStr, hue0);
}
else if (!Compare(curAtt->name, "hue1"))
{
oldFormat = true;
Atof(attStr, hue1);
}
else if (!Compare(curAtt->name, "blend"))
{
oldFormat = true;
Atof(attStr, blend);
}
else if (!Compare(curAtt->name, "count"))
{
newFormat = true;
Atoi(attStr, numColors);
}
else if (!Compare(curAtt->name, "format"))
{
newFormat = true;
if (!_stricmp(attStr, "RGB"))
numBytes = 3;
else if (!_stricmp(attStr, "RGBA"))
@ -907,29 +877,16 @@ private:
xmlFree(attStr);
}
//Old or new format?
if (newFormat && oldFormat)
//Removing support for whatever "old format" was in flam3.
//Read formatted string from contents of tag.
char* palStr = CX(xmlNodeGetContent(childNode));
if (!ParseHexColors(palStr, currentEmber, numColors, numBytes))
{
oldFormat = false;
m_ErrorReport.push_back(string(loc) + " : Mixing of old and new palette tag syntax not allowed, defaulting to new");
m_ErrorReport.push_back(string(loc) + " : Problem reading hexadecimal color data in palette");
}
if (oldFormat)
{
InterpolateCmap(currentEmber.m_Palette, blend, index0, hue0, index1, hue1);
}
else
{
//Read formatted string from contents of tag.
char* palStr = CX(xmlNodeGetContent(childNode));
if (!ParseHexColors(palStr, currentEmber, numColors, numBytes))
{
m_ErrorReport.push_back(string(loc) + " : Problem reading hexadecimal color data in palette");
}
xmlFree(palStr);
}
xmlFree(palStr);
}
else if (!Compare(childNode->name, "symmetry"))
{
@ -1436,51 +1393,6 @@ private:
return ok;
}
/// <summary>
/// Interpolate the palette.
/// Used with older formats, deprecated.
/// </summary>
/// <param name="palette">The palette to interpolate</param>
/// <param name="blend">The blend</param>
/// <param name="index0">The first index</param>
/// <param name="hue0">The first hue</param>
/// <param name="index1">The second index</param>
/// <param name="hue1">The second hue/param>
void InterpolateCmap(Palette<T>& palette, T blend, int index0, T hue0, int index1, T hue1)
{
int i, j;
const char* loc = __FUNCTION__;
Palette<T> adjustedPal0, adjustedPal1;
if (m_PaletteList.GetHueAdjustedPalette(index0, hue0, adjustedPal0) &&
m_PaletteList.GetHueAdjustedPalette(index1, hue1, adjustedPal1))
{
v4T* hueAdjusted0 = adjustedPal0.m_Entries.data();
v4T* hueAdjusted1 = adjustedPal1.m_Entries.data();
for (i = 0; i < 256; i++)
{
T t[4], s[4];
Palette<T>::RgbToHsv(glm::value_ptr(hueAdjusted0[i]), s);
Palette<T>::RgbToHsv(glm::value_ptr(hueAdjusted1[i]), t);
s[3] = hueAdjusted0[i][3];
t[3] = hueAdjusted1[i][3];
for (j = 0; j < 4; j++)
t[j] = ((1 - blend) * s[j]) + (blend * t[j]);
Palette<T>::HsvToRgb(t, glm::value_ptr(palette.m_Entries[i]));
palette.m_Entries[i][3] = t[3];
}
}
else
{
m_ErrorReport.push_back(string(loc) + " : Unable to retrieve palettes");
}
}
/// <summary>
/// Wrapper to parse a floating point Xml value and convert it to float.
/// </summary>