├── .gitignore ├── LICENSE ├── VoidAndCluster.sln ├── VoidAndCluster.vcxproj ├── VoidAndCluster.vcxproj.filters ├── bluenoise256.png ├── bluenoise64.png ├── convert.h ├── dft.h ├── generatebn_void_cluster.cpp ├── generatebn_void_cluster.h ├── histogram.h ├── image.cpp ├── image.h ├── main.cpp ├── misc.h ├── out ├── blueVC_1.gif ├── blueVC_1.histogram.csv ├── blueVC_1.png ├── blueVC_1M.gif ├── blueVC_1M.histogram.csv ├── blueVC_1M.png ├── blueVC_1M_1.png ├── blueVC_1M_102.png ├── blueVC_1M_128.png ├── blueVC_1M_153.png ├── blueVC_1M_179.png ├── blueVC_1M_204.png ├── blueVC_1M_230.png ├── blueVC_1M_25.png ├── blueVC_1M_254.png ├── blueVC_1M_51.png ├── blueVC_1M_76.png ├── blueVC_1_1.png ├── blueVC_1_102.png ├── blueVC_1_128.png ├── blueVC_1_153.png ├── blueVC_1_179.png ├── blueVC_1_204.png ├── blueVC_1_230.png ├── blueVC_1_25.png ├── blueVC_1_254.png ├── blueVC_1_51.png ├── blueVC_1_76.png ├── blueVC_2.gif ├── blueVC_2.histogram.csv ├── blueVC_2.png ├── blueVC_2_1.png ├── blueVC_2_102.png ├── blueVC_2_128.png ├── blueVC_2_153.png ├── blueVC_2_179.png ├── blueVC_2_204.png ├── blueVC_2_230.png ├── blueVC_2_25.png ├── blueVC_2_254.png ├── blueVC_2_51.png └── blueVC_2_76.png ├── scoped_timer.h ├── settings.h ├── simple_fft ├── check_fft.hpp ├── copy_array.hpp ├── error_handling.hpp ├── fft.h ├── fft.hpp ├── fft_impl.hpp └── fft_settings.h ├── stb ├── README.md ├── stb_image.h └── stb_image_write.h └── whitenoise.h /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | x64 3 | *.user 4 | debug 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Alan Wolfe 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /VoidAndCluster.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28010.2019 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidAndCluster", "VoidAndCluster.vcxproj", "{DF569EDD-D163-4DD1-B3D5-DB40CE302130}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Debug|x64.ActiveCfg = Debug|x64 17 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Debug|x64.Build.0 = Debug|x64 18 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Debug|x86.ActiveCfg = Debug|Win32 19 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Debug|x86.Build.0 = Debug|Win32 20 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Release|x64.ActiveCfg = Release|x64 21 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Release|x64.Build.0 = Release|x64 22 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Release|x86.ActiveCfg = Release|Win32 23 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {ED877453-1D65-4413-B656-6E14370B8FB4} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /VoidAndCluster.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 15.0 23 | {DF569EDD-D163-4DD1-B3D5-DB40CE302130} 24 | VoidAndCluster 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v143 32 | MultiByte 33 | 34 | 35 | Application 36 | false 37 | v143 38 | true 39 | MultiByte 40 | 41 | 42 | Application 43 | true 44 | v143 45 | MultiByte 46 | 47 | 48 | Application 49 | false 50 | v143 51 | true 52 | MultiByte 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | Level3 76 | Disabled 77 | true 78 | true 79 | true 80 | 81 | 82 | 83 | 84 | Level3 85 | Disabled 86 | true 87 | true 88 | true 89 | 90 | 91 | 92 | 93 | Level3 94 | MaxSpeed 95 | true 96 | true 97 | true 98 | true 99 | true 100 | 101 | 102 | true 103 | true 104 | 105 | 106 | 107 | 108 | Level3 109 | MaxSpeed 110 | true 111 | true 112 | true 113 | true 114 | true 115 | 116 | 117 | true 118 | true 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | -------------------------------------------------------------------------------- /VoidAndCluster.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | stb 13 | 14 | 15 | stb 16 | 17 | 18 | 19 | simple_fft 20 | 21 | 22 | simple_fft 23 | 24 | 25 | simple_fft 26 | 27 | 28 | simple_fft 29 | 30 | 31 | simple_fft 32 | 33 | 34 | simple_fft 35 | 36 | 37 | simple_fft 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | {abf3166e-d159-4ff5-9ed0-6129f96ee634} 49 | 50 | 51 | {4112bd56-a012-4803-9b37-7df092de5906} 52 | 53 | 54 | -------------------------------------------------------------------------------- /bluenoise256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/bluenoise256.png -------------------------------------------------------------------------------- /bluenoise64.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/bluenoise64.png -------------------------------------------------------------------------------- /convert.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "misc.h" 6 | 7 | template 8 | float ToFloat(T value) 9 | { 10 | return float(value) / float(std::numeric_limits::max()); 11 | } 12 | 13 | template 14 | T FromFloat(float value) 15 | { 16 | value = Lerp(0, float(std::numeric_limits::max() + 1), value); 17 | value = Clamp(0.0f, float(std::numeric_limits::max()), value); 18 | return T(value); 19 | } 20 | 21 | template 22 | void ToFloat(const std::vector& src, std::vector& dest) 23 | { 24 | dest.resize(src.size()); 25 | for (size_t index = 0, count = src.size(); index < count; ++index) 26 | dest[index] = ToFloat(src[index]); 27 | } 28 | 29 | template 30 | void FromFloat(const std::vector& src, std::vector& dest) 31 | { 32 | dest.resize(src.size()); 33 | for (size_t index = 0, count = src.size(); index < count; ++index) 34 | dest[index] = FromFloat(src[index]); 35 | } -------------------------------------------------------------------------------- /dft.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "simple_fft/fft_settings.h" 4 | #include "simple_fft/fft.h" 5 | 6 | #include "misc.h" 7 | 8 | #include 9 | #include 10 | 11 | struct ComplexImage2D 12 | { 13 | ComplexImage2D(size_t w, size_t h) 14 | { 15 | m_width = w; 16 | m_height = h; 17 | pixels.resize(w*h, real_type(0.0f)); 18 | } 19 | 20 | size_t m_width; 21 | size_t m_height; 22 | std::vector pixels; 23 | 24 | complex_type& operator()(size_t x, size_t y) 25 | { 26 | return pixels[y*m_width + x]; 27 | } 28 | 29 | const complex_type& operator()(size_t x, size_t y) const 30 | { 31 | return pixels[y*m_width + x]; 32 | } 33 | }; 34 | 35 | template 36 | void DFT(const std::vector& imageSrc, std::vector& imageDest, size_t width) 37 | { 38 | // convert the source image to float and store it in a complex image so it can be DFTd 39 | ComplexImage2D complexImageIn(width, width); 40 | for (size_t index = 0, count = width * width; index < count; ++index) 41 | complexImageIn.pixels[index] = float(imageSrc[index]) / float(std::numeric_limits::max()); 42 | 43 | // DFT the image to get frequency of the samples 44 | const char* error = nullptr; 45 | ComplexImage2D complexImageOut(width, width); 46 | simple_fft::FFT(complexImageIn, complexImageOut, width, width, error); 47 | 48 | // TODO: for some reason, the DC is huge. i'm not sure why... 49 | complexImageOut(0, 0) = 0.0f; 50 | 51 | // get the magnitudes and max magnitude 52 | std::vector magnitudes; 53 | float maxMag = 0.0f; 54 | { 55 | magnitudes.resize(width * width, 0.0f); 56 | float* dest = magnitudes.data(); 57 | for (size_t y = 0; y < width; ++y) 58 | { 59 | size_t srcY = (y + width / 2) % width; 60 | for (size_t x = 0; x < width; ++x) 61 | { 62 | size_t srcX = (x + width / 2) % width; 63 | 64 | const complex_type& c = complexImageOut(srcX, srcY); 65 | float mag = float(sqrt(c.real()*c.real() + c.imag()*c.imag())); 66 | maxMag = std::max(mag, maxMag); 67 | *dest = mag; 68 | ++dest; 69 | } 70 | } 71 | } 72 | 73 | // normalize the magnitudes and convert it back to a type T image 74 | //const float c = 1.0f / log(1.0f / 255.0f + maxMag); 75 | { 76 | imageDest.resize(width * width); 77 | const float* src = magnitudes.data(); 78 | T* dest = imageDest.data(); 79 | for (size_t y = 0; y < width; ++y) 80 | { 81 | for (size_t x = 0; x < width; ++x) 82 | { 83 | //float normalized = c * log(1.0f / 255.0f + *src); 84 | float normalized = *src / maxMag; 85 | 86 | float value = Lerp(0, float(std::numeric_limits::max() + 1), normalized); 87 | value = Clamp(0.0f, float(std::numeric_limits::max()), value); 88 | *dest = T(value); 89 | 90 | ++src; 91 | ++dest; 92 | } 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /generatebn_void_cluster.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include "generatebn_void_cluster.h" 4 | #include "whitenoise.h" 5 | #include "convert.h" 6 | #include "stb/stb_image_write.h" 7 | #include "scoped_timer.h" 8 | 9 | static const float c_sigma = 1.9f;// 1.5f; 10 | static const float c_2sigmaSquared = 2.0f * c_sigma * c_sigma; 11 | static const int c_3sigmaint = int(ceil(c_sigma * 3.0f)); 12 | 13 | static void SaveLUTImage(const std::vector& binaryPattern, std::vector& LUT, size_t width, const char* fileName) 14 | { 15 | // get the LUT min and max 16 | float LUTMin = LUT[0]; 17 | float LUTMax = LUT[0]; 18 | for (float f : LUT) 19 | { 20 | LUTMin = std::min(LUTMin, f); 21 | LUTMax = std::max(LUTMax, f); 22 | } 23 | 24 | size_t c_scale = 4; 25 | 26 | std::vector image(width*width * c_scale*c_scale * 3); 27 | for (size_t index = 0; index < width*width*c_scale*c_scale; ++index) 28 | { 29 | size_t x = (index % (width * c_scale)) / c_scale; 30 | size_t y = index / (width * c_scale * c_scale); 31 | 32 | float percent = (LUT[y*width + x] - LUTMin) / (LUTMax - LUTMin); 33 | uint8_t value = FromFloat(percent); 34 | 35 | image[index * 3 + 0] = value; 36 | image[index * 3 + 1] = value; 37 | image[index * 3 + 2] = value; 38 | 39 | if (binaryPattern[y*width + x]) 40 | { 41 | image[index * 3 + 0] = 0; 42 | image[index * 3 + 1] = 255; 43 | image[index * 3 + 2] = 0; 44 | } 45 | } 46 | stbi_write_png(fileName, int(width*c_scale), int(width*c_scale), 3, image.data(), 0); 47 | } 48 | 49 | #if 1 50 | 51 | template 52 | static bool FindWinnerLUT(const std::vector& LUT, const std::vector& binaryPattern, size_t width, int &bestPixelX, int& bestPixelY, std::mt19937& rng) 53 | { 54 | float bestValue = CLUSTER ? -FLT_MAX : FLT_MAX; 55 | std::vector bestIndices; 56 | for (size_t index = 0, count = LUT.size(); index < count; ++index) 57 | { 58 | if (binaryPattern[index] == CLUSTER) 59 | { 60 | if (LUT[index] == bestValue) 61 | { 62 | bestIndices.push_back(index); 63 | } 64 | else if ((CLUSTER == true && LUT[index] > bestValue) || (CLUSTER == false && LUT[index] < bestValue)) 65 | { 66 | bestValue = LUT[index]; 67 | bestIndices.clear(); 68 | bestIndices.push_back(index); 69 | } 70 | } 71 | } 72 | 73 | if (bestIndices.size() == 0) 74 | return false; 75 | 76 | size_t bestIndex = bestIndices[0]; 77 | 78 | // can randomize the winners 79 | /* 80 | if (bestIndices.size() > 1) 81 | { 82 | std::uniform_int_distribution dist(0, bestIndices.size() - 1); 83 | bestIndex = bestIndices[dist(rng)]; 84 | } 85 | */ 86 | 87 | bestPixelX = int(bestIndex % width); 88 | bestPixelY = int(bestIndex / width); 89 | 90 | return true; 91 | } 92 | 93 | static bool FindTightestClusterLUT(const std::vector& LUT, const std::vector& binaryPattern, size_t width, int &bestPixelX, int& bestPixelY, std::mt19937& rng) 94 | { 95 | return FindWinnerLUT(LUT, binaryPattern, width, bestPixelX, bestPixelY, rng); 96 | } 97 | 98 | static bool FindLargestVoidLUT(const std::vector& LUT, const std::vector& binaryPattern, size_t width, int &bestPixelX, int& bestPixelY, std::mt19937& rng) 99 | { 100 | return FindWinnerLUT(LUT, binaryPattern, width, bestPixelX, bestPixelY, rng); 101 | } 102 | 103 | #else 104 | static bool FindTightestClusterLUT(const std::vector& LUT, const std::vector& binaryPattern, size_t width, int &bestPixelX, int& bestPixelY, std::mt19937& rn) 105 | { 106 | float bestValue = -FLT_MAX; 107 | size_t bestIndex = ~size_t(0); 108 | for (size_t index = 0, count = LUT.size(); index < count; ++index) 109 | { 110 | if (binaryPattern[index] && LUT[index] > bestValue) 111 | { 112 | bestValue = LUT[index]; 113 | bestIndex = index; 114 | } 115 | } 116 | 117 | if (bestIndex == ~size_t(0)) 118 | return false; 119 | 120 | bestPixelX = int(bestIndex % width); 121 | bestPixelY = int(bestIndex / width); 122 | 123 | return true; 124 | } 125 | 126 | static bool FindLargestVoidLUT(const std::vector& LUT, const std::vector& binaryPattern, size_t width, int &bestPixelX, int& bestPixelY, std::mt19937& rn) 127 | { 128 | float bestValue = FLT_MAX; 129 | size_t bestIndex = ~size_t(0); 130 | for (size_t index = 0, count = LUT.size(); index < count; ++index) 131 | { 132 | if (!binaryPattern[index] && LUT[index] < bestValue) 133 | { 134 | bestValue = LUT[index]; 135 | bestIndex = index; 136 | } 137 | } 138 | 139 | if (bestIndex == ~size_t(0)) 140 | return false; 141 | 142 | bestPixelX = int(bestIndex % width); 143 | bestPixelY = int(bestIndex / width); 144 | 145 | return true; 146 | } 147 | #endif 148 | 149 | static void WriteLUTValue(std::vector& LUT, size_t width, bool value, int basex, int basey) 150 | { 151 | #pragma omp parallel for 152 | for (int y = 0; y < width; ++y) 153 | { 154 | float disty = abs(float(y) - float(basey)); 155 | if (disty > float(width / 2)) 156 | disty = float(width) - disty; 157 | 158 | for (size_t x = 0; x < width; ++x) 159 | { 160 | float distx = abs(float(x) - float(basex)); 161 | if (distx > float(width / 2)) 162 | distx = float(width) - distx; 163 | 164 | float distanceSquared = float(distx*distx) + float(disty*disty); 165 | float energy = exp(-distanceSquared / c_2sigmaSquared) * (value ? 1.0f : -1.0f); 166 | LUT[y*width + x] += energy; 167 | } 168 | } 169 | } 170 | 171 | static void MakeLUT(const std::vector& binaryPattern, std::vector& LUT, size_t width, bool writeOnes) 172 | { 173 | LUT.clear(); 174 | LUT.resize(width*width, 0.0f); 175 | for (size_t index = 0; index < width*width; ++index) 176 | { 177 | if (binaryPattern[index] == writeOnes) 178 | { 179 | int x = int(index % width); 180 | int y = int(index / width); 181 | WriteLUTValue(LUT, width, writeOnes, x, y); 182 | } 183 | } 184 | } 185 | 186 | #if SAVE_VOIDCLUSTER_INITIALPOINTS() 187 | static bool FileExists(const char* fileName) 188 | { 189 | FILE* file = nullptr; 190 | fopen_s(&file, fileName, "rb"); 191 | if (!file) 192 | return false; 193 | fclose(file); 194 | return true; 195 | } 196 | 197 | static void SaveInitialPoints(const std::vector& ranks, size_t width, bool useMitchellsBestCandidate) 198 | { 199 | struct IndexRank 200 | { 201 | size_t index; 202 | size_t rank; 203 | }; 204 | 205 | // gather the pixels that are set 206 | std::vector indexRanks; 207 | for (size_t index = 0; index < ranks.size(); ++index) 208 | if (ranks[index] != ~size_t(0)) 209 | indexRanks.push_back({ index, ranks[index] }); 210 | 211 | // Sort them by rank 212 | std::sort(indexRanks.begin(), indexRanks.end(), 213 | [] (const IndexRank& A, const IndexRank& B) 214 | { 215 | return A.rank < B.rank; 216 | } 217 | ); 218 | 219 | // Make images with different point counts 220 | static const size_t c_numOutputImages = 100; 221 | for (size_t imageIndex = 1; imageIndex <= c_numOutputImages; ++imageIndex) 222 | { 223 | std::vector pixels(width * width, 255); 224 | 225 | size_t targetPointCount = indexRanks.size(); 226 | if (imageIndex < c_numOutputImages) 227 | targetPointCount = targetPointCount * imageIndex / c_numOutputImages; 228 | 229 | for (size_t indexRank = 0; indexRank < targetPointCount; ++indexRank) 230 | pixels[indexRanks[indexRank].index] = 0; 231 | 232 | int fileIndex = 0; 233 | while (1) 234 | { 235 | char fileName[256]; 236 | sprintf_s(fileName, "debug/initial_%s_%i.png", useMitchellsBestCandidate ? "MBC" : "Reg", fileIndex); 237 | fileIndex++; 238 | 239 | if (!FileExists(fileName)) 240 | { 241 | stbi_write_png(fileName, (int)width, (int)width, 1, pixels.data(), 0); 242 | break; 243 | } 244 | } 245 | } 246 | 247 | // Could also save out a 16 bit image (R,G have bits, B = 0, A = 1) of the rank 248 | } 249 | #endif 250 | 251 | #if SAVE_VOIDCLUSTER_INITIALBP() 252 | 253 | static void SaveBinaryPattern(const std::vector& binaryPattern, size_t width, const char* baseFileName, int iterationCount, int tightestClusterX, int tightestClusterY, int largestVoidX, int largestVoidY) 254 | { 255 | size_t c_scale = 4; 256 | 257 | std::vector binaryPatternImage(width*width * c_scale*c_scale * 3); 258 | for (size_t index = 0; index < width*width*c_scale*c_scale; ++index) 259 | { 260 | size_t x = (index % (width * c_scale)) / c_scale; 261 | size_t y = index / (width * c_scale * c_scale); 262 | 263 | bool isCluster = (x == tightestClusterX && y == tightestClusterY); 264 | bool isVoid = (x == largestVoidX && y == largestVoidY); 265 | 266 | if (isCluster == isVoid) 267 | { 268 | if (isCluster) 269 | { 270 | binaryPatternImage[index * 3 + 0] = 255; 271 | binaryPatternImage[index * 3 + 1] = 255; 272 | binaryPatternImage[index * 3 + 2] = 0; 273 | } 274 | else 275 | { 276 | binaryPatternImage[index * 3 + 0] = binaryPattern[y*width+x] ? 255 : 0; 277 | binaryPatternImage[index * 3 + 1] = binaryPattern[y*width + x] ? 255 : 0; 278 | binaryPatternImage[index * 3 + 2] = binaryPattern[y*width + x] ? 255 : 0; 279 | } 280 | } 281 | else if (isCluster) 282 | { 283 | binaryPatternImage[index * 3 + 0] = 255; 284 | binaryPatternImage[index * 3 + 1] = 0; 285 | binaryPatternImage[index * 3 + 2] = 0; 286 | } 287 | else if (isVoid) 288 | { 289 | binaryPatternImage[index * 3 + 0] = 0; 290 | binaryPatternImage[index * 3 + 1] = 255; 291 | binaryPatternImage[index * 3 + 2] = 0; 292 | } 293 | } 294 | 295 | char fileName[256]; 296 | sprintf(fileName, "%s%i.png", baseFileName, iterationCount); 297 | stbi_write_png(fileName, int(width*c_scale), int(width*c_scale), 3, binaryPatternImage.data(), 0); 298 | } 299 | 300 | #endif 301 | 302 | static void MakeInitialBinaryPattern(std::vector& binaryPattern, size_t width, const char* baseFileName, std::mt19937& rng) 303 | { 304 | ScopedTimer timer("Initial Pattern", false); 305 | 306 | std::uniform_int_distribution dist(0, width * width - 1); 307 | 308 | std::vector LUT; 309 | LUT.resize(width*width, 0.0f); 310 | 311 | binaryPattern.resize(width*width, false); 312 | size_t ones = size_t(float(width*width) * 0.1f); // start 10% of the pixels as white 313 | for (size_t index = 0; index < ones; ++index) 314 | { 315 | size_t pixel = dist(rng); 316 | binaryPattern[pixel] = true; 317 | WriteLUTValue(LUT, width, true, int(pixel % width), int(pixel / width)); 318 | } 319 | 320 | int iterationCount = 0; 321 | while (1) 322 | { 323 | printf("\r%i iterations", iterationCount); 324 | iterationCount++; 325 | 326 | // find the location of the tightest cluster 327 | int tightestClusterX = -1; 328 | int tightestClusterY = -1; 329 | FindTightestClusterLUT(LUT, binaryPattern, width, tightestClusterX, tightestClusterY, rng); 330 | 331 | // remove the 1 from the tightest cluster 332 | binaryPattern[tightestClusterY*width + tightestClusterX] = false; 333 | WriteLUTValue(LUT, width, false, tightestClusterX, tightestClusterY); 334 | 335 | // find the largest void 336 | int largestVoidX = -1; 337 | int largestVoidY = -1; 338 | FindLargestVoidLUT(LUT, binaryPattern, width, largestVoidX, largestVoidY, rng); 339 | 340 | // put the 1 in the largest void 341 | binaryPattern[largestVoidY*width + largestVoidX] = true; 342 | WriteLUTValue(LUT, width, true, largestVoidX, largestVoidY); 343 | 344 | #if SAVE_VOIDCLUSTER_INITIALBP() 345 | // save the binary pattern out for debug purposes 346 | SaveBinaryPattern(binaryPattern, width, baseFileName, iterationCount, tightestClusterX, tightestClusterY, largestVoidX, largestVoidY); 347 | #endif 348 | 349 | // exit condition. the pattern is stable 350 | if (tightestClusterX == largestVoidX && tightestClusterY == largestVoidY) 351 | break; 352 | } 353 | printf("\n"); 354 | } 355 | 356 | // Phase 1: Start with initial binary pattern and remove the tightest cluster until there are none left, entering ranks for those pixels 357 | static void Phase1(std::vector& binaryPattern, std::vector& LUT, std::vector& ranks, size_t width, std::mt19937& rng, const char* baseFileName) 358 | { 359 | ScopedTimer timer("Phase 1", false); 360 | 361 | #if 1 362 | // count how many ones there are 363 | struct P 364 | { 365 | size_t x; 366 | size_t y; 367 | }; 368 | std::vector

points; 369 | for (size_t i = 0; i < binaryPattern.size(); ++i) 370 | { 371 | if (binaryPattern[i]) 372 | points.push_back({ i % width, i / width }); 373 | } 374 | 375 | // Shuffling will break patterns and ties better 376 | std::shuffle(points.begin(), points.end(), rng); 377 | 378 | // Use a "mitchell's worst candidate" to make a sequence out of the initial binary pattern 379 | std::vector pointScores(points.size()); 380 | size_t startingPoints = points.size(); 381 | while (!points.empty()) 382 | { 383 | printf("\r%i%%", int(100.0f * (1.0f - float(points.size()) / float(startingPoints)))); 384 | 385 | // score the points. Score = distance to closest other point. 386 | #pragma omp parallel for 387 | for (int srcPointIndex = 0; srcPointIndex < (int)points.size(); ++srcPointIndex) 388 | { 389 | const P& srcPoint = points[srcPointIndex]; 390 | float minDist = FLT_MAX; 391 | 392 | for (int destPointIndex = 0; destPointIndex < (int)points.size(); ++destPointIndex) 393 | { 394 | if (srcPointIndex == destPointIndex) 395 | continue; 396 | 397 | const P& destPoint = points[destPointIndex]; 398 | 399 | // Calculate toroidal distance. Closest distance is the score 400 | size_t dx = (srcPoint.x >= destPoint.x) ? srcPoint.x - destPoint.x : destPoint.x - srcPoint.x; 401 | if (dx > width / 2) 402 | dx = width - dx; 403 | 404 | size_t dy = (srcPoint.y >= destPoint.y) ? srcPoint.y - destPoint.y : destPoint.y - srcPoint.y; 405 | if (dy > width / 2) 406 | dy = width - dy; 407 | 408 | float distance = std::sqrt(float(dx) * float(dx) + float(dy) * float(dy)); 409 | minDist = std::min(minDist, distance); 410 | } 411 | 412 | pointScores[srcPointIndex] = minDist; 413 | } 414 | 415 | // Find the worst scoring point 416 | float worstPointScore = FLT_MAX; 417 | size_t worstPointIndex = 0; 418 | for (int pointIndex = 0; pointIndex < (int)points.size(); ++pointIndex) 419 | { 420 | if (pointScores[pointIndex] >= worstPointScore) 421 | continue; 422 | 423 | worstPointScore = pointScores[pointIndex]; 424 | worstPointIndex = pointIndex; 425 | } 426 | 427 | // Remove the point with the lowest score, as the next point 428 | const P& worstP = points[worstPointIndex]; 429 | binaryPattern[worstP.y * width + worstP.x] = false; 430 | ranks[worstP.y * width + worstP.x] = points.size(); 431 | points.erase(points.begin() + worstPointIndex); 432 | } 433 | printf("\n"); 434 | #else 435 | size_t ones = 0; 436 | for (bool b : binaryPattern) 437 | { 438 | if (b) 439 | ones++; 440 | } 441 | size_t startingOnes = ones; 442 | 443 | // remove the tightest cluster repeatedly 444 | while (ones > 0) 445 | { 446 | printf("\r%i%%", int(100.0f * (1.0f - float(ones) / float(startingOnes)))); 447 | 448 | int bestX, bestY; 449 | FindTightestClusterLUT(LUT, binaryPattern, width, bestX, bestY, rng); 450 | binaryPattern[bestY * width + bestX] = false; 451 | WriteLUTValue(LUT, width, false, bestX, bestY); 452 | ones--; 453 | ranks[bestY*width + bestX] = ones; 454 | 455 | #if SAVE_VOIDCLUSTER_PHASE1() 456 | // save the binary pattern out for debug purposes 457 | SaveBinaryPattern(binaryPattern, width, baseFileName, int(startingOnes - ones), bestX, bestY, -1, -1); 458 | #endif 459 | } 460 | printf("\n"); 461 | #endif 462 | } 463 | 464 | struct Point 465 | { 466 | size_t x; 467 | size_t y; 468 | }; 469 | typedef std::vector TPoints; 470 | typedef std::vector TPointGrid; 471 | 472 | static bool DistanceSqToClosestPoint(const TPoints& points, const Point& point, float& minDistSq, size_t width) 473 | { 474 | if (points.size() == 0) 475 | return false; 476 | 477 | // calculate the closest distance from this point to an existing sample 478 | for (const Point& p : points) 479 | { 480 | float distx = std::abs(float(p.x) - float(point.x)); 481 | float disty = std::abs(float(p.y) - float(point.y)); 482 | 483 | if (distx > float(width) / 2.0f) 484 | distx = float(width) - distx; 485 | 486 | if (disty > float(width) / 2.0f) 487 | disty = float(width) - disty; 488 | 489 | float distSq = distx * distx + disty * disty; 490 | if (distSq < minDistSq) 491 | minDistSq = distSq; 492 | } 493 | return true; 494 | } 495 | 496 | static float DistanceSqToClosestPoint(const TPointGrid& grid, size_t cellCount, size_t cellSize, const Point& point, size_t width) 497 | { 498 | const int basex = int(point.x / cellSize); 499 | const int basey = int(point.y / cellSize); 500 | 501 | const int maxRadius = int(cellCount / 2); 502 | 503 | float minDistSq = FLT_MAX; 504 | bool foundAPoint = false; 505 | bool didAnExtraRing = false; 506 | 507 | for (int radius = 0; radius <= maxRadius; ++radius) 508 | { 509 | // top and bottom rows 510 | { 511 | for (int offsetX = -radius; offsetX <= radius; ++offsetX) 512 | { 513 | int x = int(basex + offsetX + cellCount) % int(cellCount); 514 | 515 | int offsetY = -radius; 516 | int y = int(basey + offsetY + cellCount) % int(cellCount); 517 | foundAPoint |= DistanceSqToClosestPoint(grid[y*cellCount + x], point, minDistSq, width); 518 | 519 | offsetY = radius; 520 | y = int(basey + offsetY + cellCount) % int(cellCount); 521 | foundAPoint |= DistanceSqToClosestPoint(grid[y*cellCount + x], point, minDistSq, width); 522 | } 523 | } 524 | 525 | // left and right 526 | { 527 | for (int offsetY = -radius + 1; offsetY <= radius - 1; ++offsetY) 528 | { 529 | int y = int(basey + offsetY + cellCount) % int(cellCount); 530 | 531 | int offsetX = -radius; 532 | int x = int(basex + offsetX + cellCount) % int(cellCount); 533 | foundAPoint |= DistanceSqToClosestPoint(grid[y*cellCount + x], point, minDistSq, width); 534 | 535 | offsetX = +radius; 536 | x = int(basex + offsetX + cellCount) % int(cellCount); 537 | foundAPoint |= DistanceSqToClosestPoint(grid[y*cellCount + x], point, minDistSq, width); 538 | } 539 | } 540 | 541 | // we stop when we've found a point, then do another ring to make sure there isn't something closer to what we found. 542 | if (foundAPoint) 543 | { 544 | if (didAnExtraRing) 545 | break; 546 | else 547 | didAnExtraRing = true; 548 | } 549 | } 550 | 551 | return minDistSq; 552 | } 553 | 554 | static void AddPointToPointGrid(TPointGrid& grid, size_t cellCount, size_t cellSize, const Point& point) 555 | { 556 | Point cell; 557 | cell.x = point.x / cellSize; 558 | cell.y = point.y / cellSize; 559 | grid[cell.y * cellCount + cell.x].push_back(point); 560 | } 561 | 562 | // This replaces "Initial Binary Pattern" and "Phase 1" in the void and cluster algorithm. 563 | // Initial binary pattern makes blue noise distributed points. 564 | // Phase 1 makes them be progressive, so any points from 0 to N are blue noise. 565 | // Mitchell's best candidate algorithm makes progressive blue noise so can be used instead of those 2 steps. 566 | // https://blog.demofox.org/2017/10/20/generating-blue-noise-sample-points-with-mitchells-best-candidate-algorithm/ 567 | static void MitchellsBestCandidate(std::vector& binaryPattern, std::vector& ranks, size_t width) 568 | { 569 | ScopedTimer timer("Mitchells Best Candidate", false); 570 | 571 | std::mt19937 rng(GetRNGSeed()); 572 | std::uniform_int_distribution dist(0, width*width - 1); 573 | 574 | binaryPattern.resize(width*width, false); 575 | ranks.resize(width*width, ~size_t(0)); 576 | 577 | static const size_t gridCellCount = 32; 578 | TPointGrid grid(gridCellCount*gridCellCount); 579 | const size_t gridCellSize = width / gridCellCount; 580 | 581 | size_t ones = size_t(float(width * width)*0.1f); 582 | for (size_t i = 0; i < ones; ++i) 583 | { 584 | printf("\r%i%%", int(100.0f * float(i) / float(ones - 1))); 585 | 586 | // we scale up the candidates each iteration like in the paper, to keep frequency behavior consistent 587 | size_t numCandidates = i + 1; 588 | 589 | // keep the candidate that is farthest from the closest existing point 590 | float bestDistanceSq = 0.0f; 591 | Point best; 592 | for (size_t candidate = 0; candidate < numCandidates; ++candidate) 593 | { 594 | size_t index = dist(rng); 595 | Point c; 596 | c.x = index % width; 597 | c.y = index / width; 598 | 599 | float minDistSq = DistanceSqToClosestPoint(grid, gridCellCount, gridCellSize, c, width); 600 | 601 | if (minDistSq > bestDistanceSq) 602 | { 603 | bestDistanceSq = minDistSq; 604 | best = c; 605 | } 606 | } 607 | 608 | // take the best candidate 609 | binaryPattern[best.y * width + best.x] = true; 610 | ranks[best.y * width + best.x] = i; 611 | AddPointToPointGrid(grid, gridCellCount, gridCellSize, best); 612 | } 613 | printf("\n"); 614 | } 615 | 616 | // Phase 2: Start with initial binary pattern and add points to the largest void until half the pixels are white, entering ranks for those pixels 617 | static void Phase2(std::vector& binaryPattern, std::vector& LUT, std::vector& ranks, size_t width, std::mt19937& rng) 618 | { 619 | ScopedTimer timer("Phase 2", false); 620 | 621 | // count how many ones there are 622 | size_t ones = 0; 623 | for (bool b : binaryPattern) 624 | { 625 | if (b) 626 | ones++; 627 | } 628 | size_t startingOnes = ones; 629 | size_t onesToDo = (width*width / 2) - startingOnes; 630 | 631 | // add to the largest void repeatedly 632 | while (ones <= (width*width/2)) 633 | { 634 | size_t onesDone = ones - startingOnes; 635 | printf("\r%i%%", int(100.0f * float(onesDone) / float(onesToDo))); 636 | 637 | int bestX, bestY; 638 | FindLargestVoidLUT(LUT, binaryPattern, width, bestX, bestY, rng); 639 | binaryPattern[bestY * width + bestX] = true; 640 | WriteLUTValue(LUT, width, true, bestX, bestY); 641 | ranks[bestY*width + bestX] = ones; 642 | ones++; 643 | } 644 | printf("\n"); 645 | } 646 | 647 | // Phase 3: Continue with the last binary pattern, repeatedly find the tightest cluster of 0s and insert a 1 into them 648 | static void Phase3(std::vector& binaryPattern, std::vector& LUT, std::vector& ranks, size_t width, std::mt19937& rng) 649 | { 650 | ScopedTimer timer("Phase 3", false); 651 | 652 | // count how many ones there are 653 | size_t ones = 0; 654 | for (bool b : binaryPattern) 655 | { 656 | if (b) 657 | ones++; 658 | } 659 | size_t startingOnes = ones; 660 | size_t onesToDo = (width*width) - startingOnes; 661 | 662 | // add 1 to the largest cluster of 0's repeatedly 663 | int bestX, bestY; 664 | while (FindLargestVoidLUT(LUT, binaryPattern, width, bestX, bestY, rng)) 665 | { 666 | size_t onesDone = ones - startingOnes; 667 | printf("\r%i%%", int(100.0f * float(onesDone) / float(onesToDo))); 668 | 669 | WriteLUTValue(LUT, width, true, bestX, bestY); 670 | binaryPattern[bestY * width + bestX] = true; 671 | ranks[bestY*width + bestX] = ones; 672 | ones++; 673 | } 674 | printf("\n"); 675 | } 676 | 677 | void GenerateBN_Void_Cluster(std::vector& blueNoise, size_t width, bool useMitchellsBestCandidate, const char* baseFileName) 678 | { 679 | std::mt19937 rng(GetRNGSeed()); 680 | 681 | std::vector ranks(width*width, ~size_t(0)); 682 | 683 | std::vector initialBinaryPattern; 684 | std::vector binaryPattern; 685 | std::vector initialLUT; 686 | std::vector LUT; 687 | 688 | if (!useMitchellsBestCandidate) 689 | { 690 | // make the initial binary pattern and initial LUT 691 | MakeInitialBinaryPattern(initialBinaryPattern, width, baseFileName, rng); 692 | MakeLUT(initialBinaryPattern, initialLUT, width, true); 693 | 694 | // Phase 1: Start with initial binary pattern and remove the tightest cluster until there are none left, entering ranks for those pixels 695 | binaryPattern = initialBinaryPattern; 696 | LUT = initialLUT; 697 | Phase1(binaryPattern, LUT, ranks, width, rng, baseFileName); 698 | } 699 | else 700 | { 701 | // replace initial binary pattern and phase 1 with Mitchell's best candidate algorithm, and then making the LUT 702 | MitchellsBestCandidate(initialBinaryPattern, ranks, width); 703 | MakeLUT(initialBinaryPattern, initialLUT, width, true); 704 | 705 | //SaveBinaryPattern(initialBinaryPattern, width, "out/_blah", 0, -1, -1, -1, -1); 706 | } 707 | 708 | #if SAVE_VOIDCLUSTER_INITIALPOINTS() 709 | SaveInitialPoints(ranks, width, useMitchellsBestCandidate); 710 | #endif 711 | 712 | // Phase 2: Start with initial binary pattern and add points to the largest void until half the pixels are white, entering ranks for those pixels 713 | binaryPattern = initialBinaryPattern; 714 | LUT = initialLUT; 715 | Phase2(binaryPattern, LUT, ranks, width, rng); 716 | 717 | // Phase 3: Continue with the last binary pattern, repeatedly find the tightest cluster of 0s and insert a 1 into them 718 | // Note: we do need to re-make the LUT, because we are writing 0s instead of 1s 719 | MakeLUT(binaryPattern, LUT, width, false); 720 | Phase3(binaryPattern, LUT, ranks, width, rng); 721 | 722 | // convert to U8 723 | { 724 | ScopedTimer timer("Converting to U8", false); 725 | blueNoise.resize(width*width); 726 | for (size_t index = 0; index < width*width; ++index) 727 | blueNoise[index] = uint8_t(ranks[index] * 256 / (width*width)); 728 | } 729 | } 730 | -------------------------------------------------------------------------------- /generatebn_void_cluster.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | // http://cv.ulichney.com/papers/1993-void-cluster.pdf 6 | void GenerateBN_Void_Cluster(std::vector& blueNoise, size_t width, bool useMitchellsBestCandidate, const char* baseFileName); 7 | -------------------------------------------------------------------------------- /histogram.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | template 6 | void WriteHistogram(const std::vector& values, const char* fileName) 7 | { 8 | std::vector histogram; 9 | histogram.resize(std::numeric_limits::max() + 1, 0); 10 | for (const T& value : values) 11 | histogram[value]++; 12 | 13 | FILE * file = nullptr; 14 | fopen_s(&file, fileName, "w+t"); 15 | fprintf(file, "\"Value\",\"Count\"\n"); 16 | for (size_t index = 0, count = histogram.size(); index < count; ++index) 17 | fprintf(file, "\"%zu\",\"%zu\"\n", index, histogram[index]); 18 | fclose(file); 19 | } -------------------------------------------------------------------------------- /image.cpp: -------------------------------------------------------------------------------- 1 | #include "image.h" 2 | #include 3 | 4 | void AppendImageHorizontal( 5 | const std::vector& srcA, 6 | size_t widthA, 7 | size_t heightA, 8 | const std::vector& srcB, 9 | size_t widthB, 10 | size_t heightB, 11 | std::vector& dest, 12 | size_t& destWidth, 13 | size_t& destHeight 14 | ) 15 | { 16 | // calculate dims of desination and allocate it, filled with 0s 17 | destWidth = widthA + widthB; 18 | destHeight = std::max(heightA, heightB); 19 | dest.resize(destWidth*destHeight, 0); 20 | 21 | // copy srcA in 22 | for (size_t y = 0; y < heightA; ++y) 23 | { 24 | const uint8_t* srcptr = &srcA[y*widthA]; 25 | uint8_t* destptr = &dest[y*destWidth]; 26 | 27 | memcpy(destptr, srcptr, widthA); 28 | } 29 | 30 | // copy srcB in 31 | for (size_t y = 0; y < heightB; ++y) 32 | { 33 | const uint8_t* srcptr = &srcB[y*widthB]; 34 | uint8_t* destptr = &dest[y*destWidth + widthA]; 35 | 36 | memcpy(destptr, srcptr, widthA); 37 | } 38 | } -------------------------------------------------------------------------------- /image.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | void AppendImageHorizontal( 6 | const std::vector& srcA, 7 | size_t widthA, 8 | size_t heightA, 9 | const std::vector& srcB, 10 | size_t widthB, 11 | size_t heightB, 12 | std::vector& dest, 13 | size_t& destWidth, 14 | size_t& destHeight 15 | ); -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #define THRESHOLD_SAMPLES() 11 // the number of samples for threshold testing. 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include "convert.h" 10 | #include "histogram.h" 11 | #include "image.h" 12 | #include "scoped_timer.h" 13 | #include "generatebn_void_cluster.h" 14 | #include "dft.h" 15 | 16 | #define STB_IMAGE_WRITE_IMPLEMENTATION 17 | #include "stb/stb_image_write.h" 18 | 19 | #define STB_IMAGE_IMPLEMENTATION 20 | #include "stb/stb_image.h" 21 | 22 | void TestMask(const std::vector& noise, size_t noiseSize, const char* baseFileName) 23 | { 24 | std::vector thresholdImage(noise.size()); 25 | 26 | for (size_t testIndex = 0; testIndex < THRESHOLD_SAMPLES(); ++testIndex) 27 | { 28 | float percent = float(testIndex) / float(THRESHOLD_SAMPLES() - 1); 29 | uint8_t thresholdValue = FromFloat(percent); 30 | if (thresholdValue == 0) 31 | thresholdValue = 1; 32 | else if (thresholdValue == 255) 33 | thresholdValue = 254; 34 | 35 | for (size_t pixelIndex = 0, pixelCount = noise.size(); pixelIndex < pixelCount; ++pixelIndex) 36 | thresholdImage[pixelIndex] = noise[pixelIndex] > thresholdValue ? 255 : 0; 37 | 38 | std::vector thresholdImageDFT; 39 | DFT(thresholdImage, thresholdImageDFT, noiseSize); 40 | 41 | std::vector noiseAndDFT; 42 | size_t noiseAndDFT_width = 0; 43 | size_t noiseAndDFT_height = 0; 44 | AppendImageHorizontal(thresholdImage, noiseSize, noiseSize, thresholdImageDFT, noiseSize, noiseSize, noiseAndDFT, noiseAndDFT_width, noiseAndDFT_height); 45 | 46 | char fileName[256]; 47 | sprintf(fileName, "%s_%u.png", baseFileName, thresholdValue); 48 | stbi_write_png(fileName, int(noiseAndDFT_width), int(noiseAndDFT_height), 1, noiseAndDFT.data(), 0); 49 | } 50 | } 51 | 52 | void TestNoise(const std::vector& noise, size_t noiseSize, const char* baseFileName) 53 | { 54 | char fileName[256]; 55 | sprintf(fileName, "%s.noise.png", baseFileName); 56 | stbi_write_png(fileName, int(noiseSize), int(noiseSize), 1, noise.data(), 0); 57 | 58 | sprintf(fileName, "%s.histogram.csv", baseFileName); 59 | 60 | WriteHistogram(noise, fileName); 61 | std::vector noiseDFT; 62 | DFT(noise, noiseDFT, noiseSize); 63 | 64 | std::vector noiseAndDFT; 65 | size_t noiseAndDFT_width = 0; 66 | size_t noiseAndDFT_height = 0; 67 | AppendImageHorizontal(noise, noiseSize, noiseSize, noiseDFT, noiseSize, noiseSize, noiseAndDFT, noiseAndDFT_width, noiseAndDFT_height); 68 | 69 | sprintf(fileName, "%s.png", baseFileName); 70 | stbi_write_png(fileName, int(noiseAndDFT_width), int(noiseAndDFT_height), 1, noiseAndDFT.data(), 0); 71 | 72 | TestMask(noise, noiseSize, baseFileName); 73 | } 74 | 75 | int main(int argc, char** argv) 76 | { 77 | _mkdir("out"); 78 | _mkdir("debug"); 79 | 80 | // generate blue noise using void and cluster 81 | { 82 | static size_t c_width = 256; 83 | 84 | std::vector noise; 85 | 86 | { 87 | ScopedTimer timer("Blue noise by void and cluster"); 88 | GenerateBN_Void_Cluster(noise, c_width, false, "out/blueVC_1"); 89 | } 90 | 91 | TestNoise(noise, c_width, "out/blueVC_1"); 92 | } 93 | 94 | // generate blue noise using void and cluster but using mitchell's best candidate instead of initial binary pattern and phase 1 95 | { 96 | static size_t c_width = 256; 97 | 98 | std::vector noise; 99 | 100 | { 101 | ScopedTimer timer("Blue noise by void and cluster with Mitchells best candidate"); 102 | GenerateBN_Void_Cluster(noise, c_width, true, "out/blueVC_1M"); 103 | } 104 | 105 | TestNoise(noise, c_width, "out/blueVC_1M"); 106 | } 107 | 108 | // load a blue noise texture 109 | { 110 | int width, height, channels; 111 | 112 | std::vector noise; 113 | 114 | { 115 | ScopedTimer timer("Blue noise by void and cluster from loading the texture"); 116 | uint8_t* image = stbi_load("bluenoise256.png", &width, &height, &channels, 0); 117 | 118 | noise.reserve(width*height); 119 | for (int i = 0; i < width*height; ++i) 120 | noise.push_back(image[i*channels]); 121 | 122 | stbi_image_free(image); 123 | } 124 | 125 | TestNoise(noise, width, "out/blueVC_2"); 126 | } 127 | 128 | system("pause"); 129 | 130 | return 0; 131 | } -------------------------------------------------------------------------------- /misc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | template 4 | T Clamp(T min, T max, T value) 5 | { 6 | if (value < min) 7 | return min; 8 | else if (value > max) 9 | return max; 10 | else 11 | return value; 12 | } 13 | 14 | inline float Lerp(float a, float b, float t) 15 | { 16 | return a * (1.0f - t) + b * t; 17 | } -------------------------------------------------------------------------------- /out/blueVC_1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1.gif -------------------------------------------------------------------------------- /out/blueVC_1.histogram.csv: -------------------------------------------------------------------------------- 1 | "Value","Count" 2 | "0","256" 3 | "1","256" 4 | "2","256" 5 | "3","256" 6 | "4","256" 7 | "5","256" 8 | "6","256" 9 | "7","256" 10 | "8","256" 11 | "9","256" 12 | "10","256" 13 | "11","256" 14 | "12","256" 15 | "13","256" 16 | "14","256" 17 | "15","256" 18 | "16","256" 19 | "17","256" 20 | "18","256" 21 | "19","256" 22 | "20","256" 23 | "21","256" 24 | "22","256" 25 | "23","256" 26 | "24","256" 27 | "25","256" 28 | "26","256" 29 | "27","256" 30 | "28","256" 31 | "29","256" 32 | "30","256" 33 | "31","256" 34 | "32","256" 35 | "33","256" 36 | "34","256" 37 | "35","256" 38 | "36","256" 39 | "37","256" 40 | "38","256" 41 | "39","256" 42 | "40","256" 43 | "41","256" 44 | "42","256" 45 | "43","256" 46 | "44","256" 47 | "45","256" 48 | "46","256" 49 | "47","256" 50 | "48","256" 51 | "49","256" 52 | "50","256" 53 | "51","256" 54 | "52","256" 55 | "53","256" 56 | "54","256" 57 | "55","256" 58 | "56","256" 59 | "57","256" 60 | "58","256" 61 | "59","256" 62 | "60","256" 63 | "61","256" 64 | "62","256" 65 | "63","256" 66 | "64","256" 67 | "65","256" 68 | "66","256" 69 | "67","256" 70 | "68","256" 71 | "69","256" 72 | "70","256" 73 | "71","256" 74 | "72","256" 75 | "73","256" 76 | "74","256" 77 | "75","256" 78 | "76","256" 79 | "77","256" 80 | "78","256" 81 | "79","256" 82 | "80","256" 83 | "81","256" 84 | "82","256" 85 | "83","256" 86 | "84","256" 87 | "85","256" 88 | "86","256" 89 | "87","256" 90 | "88","256" 91 | "89","256" 92 | "90","256" 93 | "91","256" 94 | "92","256" 95 | "93","256" 96 | "94","256" 97 | "95","256" 98 | "96","256" 99 | "97","256" 100 | "98","256" 101 | "99","256" 102 | "100","256" 103 | "101","256" 104 | "102","256" 105 | "103","256" 106 | "104","256" 107 | "105","256" 108 | "106","256" 109 | "107","256" 110 | "108","256" 111 | "109","256" 112 | "110","256" 113 | "111","256" 114 | "112","256" 115 | "113","256" 116 | "114","256" 117 | "115","256" 118 | "116","256" 119 | "117","256" 120 | "118","256" 121 | "119","256" 122 | "120","256" 123 | "121","256" 124 | "122","256" 125 | "123","256" 126 | "124","256" 127 | "125","256" 128 | "126","256" 129 | "127","256" 130 | "128","256" 131 | "129","256" 132 | "130","256" 133 | "131","256" 134 | "132","256" 135 | "133","256" 136 | "134","256" 137 | "135","256" 138 | "136","256" 139 | "137","256" 140 | "138","256" 141 | "139","256" 142 | "140","256" 143 | "141","256" 144 | "142","256" 145 | "143","256" 146 | "144","256" 147 | "145","256" 148 | "146","256" 149 | "147","256" 150 | "148","256" 151 | "149","256" 152 | "150","256" 153 | "151","256" 154 | "152","256" 155 | "153","256" 156 | "154","256" 157 | "155","256" 158 | "156","256" 159 | "157","256" 160 | "158","256" 161 | "159","256" 162 | "160","256" 163 | "161","256" 164 | "162","256" 165 | "163","256" 166 | "164","256" 167 | "165","256" 168 | "166","256" 169 | "167","256" 170 | "168","256" 171 | "169","256" 172 | "170","256" 173 | "171","256" 174 | "172","256" 175 | "173","256" 176 | "174","256" 177 | "175","256" 178 | "176","256" 179 | "177","256" 180 | "178","256" 181 | "179","256" 182 | "180","256" 183 | "181","256" 184 | "182","256" 185 | "183","256" 186 | "184","256" 187 | "185","256" 188 | "186","256" 189 | "187","256" 190 | "188","256" 191 | "189","256" 192 | "190","256" 193 | "191","256" 194 | "192","256" 195 | "193","256" 196 | "194","256" 197 | "195","256" 198 | "196","256" 199 | "197","256" 200 | "198","256" 201 | "199","256" 202 | "200","256" 203 | "201","256" 204 | "202","256" 205 | "203","256" 206 | "204","256" 207 | "205","256" 208 | "206","256" 209 | "207","256" 210 | "208","256" 211 | "209","256" 212 | "210","256" 213 | "211","256" 214 | "212","256" 215 | "213","256" 216 | "214","256" 217 | "215","256" 218 | "216","256" 219 | "217","256" 220 | "218","256" 221 | "219","256" 222 | "220","256" 223 | "221","256" 224 | "222","256" 225 | "223","256" 226 | "224","256" 227 | "225","256" 228 | "226","256" 229 | "227","256" 230 | "228","256" 231 | "229","256" 232 | "230","256" 233 | "231","256" 234 | "232","256" 235 | "233","256" 236 | "234","256" 237 | "235","256" 238 | "236","256" 239 | "237","256" 240 | "238","256" 241 | "239","256" 242 | "240","256" 243 | "241","256" 244 | "242","256" 245 | "243","256" 246 | "244","256" 247 | "245","256" 248 | "246","256" 249 | "247","256" 250 | "248","256" 251 | "249","256" 252 | "250","256" 253 | "251","256" 254 | "252","256" 255 | "253","256" 256 | "254","256" 257 | "255","256" 258 | -------------------------------------------------------------------------------- /out/blueVC_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1.png -------------------------------------------------------------------------------- /out/blueVC_1M.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M.gif -------------------------------------------------------------------------------- /out/blueVC_1M.histogram.csv: -------------------------------------------------------------------------------- 1 | "Value","Count" 2 | "0","256" 3 | "1","256" 4 | "2","256" 5 | "3","256" 6 | "4","256" 7 | "5","256" 8 | "6","256" 9 | "7","256" 10 | "8","256" 11 | "9","256" 12 | "10","256" 13 | "11","256" 14 | "12","256" 15 | "13","256" 16 | "14","256" 17 | "15","256" 18 | "16","256" 19 | "17","256" 20 | "18","256" 21 | "19","256" 22 | "20","256" 23 | "21","256" 24 | "22","256" 25 | "23","256" 26 | "24","256" 27 | "25","256" 28 | "26","256" 29 | "27","256" 30 | "28","256" 31 | "29","256" 32 | "30","256" 33 | "31","256" 34 | "32","256" 35 | "33","256" 36 | "34","256" 37 | "35","256" 38 | "36","256" 39 | "37","256" 40 | "38","256" 41 | "39","256" 42 | "40","256" 43 | "41","256" 44 | "42","256" 45 | "43","256" 46 | "44","256" 47 | "45","256" 48 | "46","256" 49 | "47","256" 50 | "48","256" 51 | "49","256" 52 | "50","256" 53 | "51","256" 54 | "52","256" 55 | "53","256" 56 | "54","256" 57 | "55","256" 58 | "56","256" 59 | "57","256" 60 | "58","256" 61 | "59","256" 62 | "60","256" 63 | "61","256" 64 | "62","256" 65 | "63","256" 66 | "64","256" 67 | "65","256" 68 | "66","256" 69 | "67","256" 70 | "68","256" 71 | "69","256" 72 | "70","256" 73 | "71","256" 74 | "72","256" 75 | "73","256" 76 | "74","256" 77 | "75","256" 78 | "76","256" 79 | "77","256" 80 | "78","256" 81 | "79","256" 82 | "80","256" 83 | "81","256" 84 | "82","256" 85 | "83","256" 86 | "84","256" 87 | "85","256" 88 | "86","256" 89 | "87","256" 90 | "88","256" 91 | "89","256" 92 | "90","256" 93 | "91","256" 94 | "92","256" 95 | "93","256" 96 | "94","256" 97 | "95","256" 98 | "96","256" 99 | "97","256" 100 | "98","256" 101 | "99","256" 102 | "100","256" 103 | "101","256" 104 | "102","256" 105 | "103","256" 106 | "104","256" 107 | "105","256" 108 | "106","256" 109 | "107","256" 110 | "108","256" 111 | "109","256" 112 | "110","256" 113 | "111","256" 114 | "112","256" 115 | "113","256" 116 | "114","256" 117 | "115","256" 118 | "116","256" 119 | "117","256" 120 | "118","256" 121 | "119","256" 122 | "120","256" 123 | "121","256" 124 | "122","256" 125 | "123","256" 126 | "124","256" 127 | "125","256" 128 | "126","256" 129 | "127","256" 130 | "128","256" 131 | "129","256" 132 | "130","256" 133 | "131","256" 134 | "132","256" 135 | "133","256" 136 | "134","256" 137 | "135","256" 138 | "136","256" 139 | "137","256" 140 | "138","256" 141 | "139","256" 142 | "140","256" 143 | "141","256" 144 | "142","256" 145 | "143","256" 146 | "144","256" 147 | "145","256" 148 | "146","256" 149 | "147","256" 150 | "148","256" 151 | "149","256" 152 | "150","256" 153 | "151","256" 154 | "152","256" 155 | "153","256" 156 | "154","256" 157 | "155","256" 158 | "156","256" 159 | "157","256" 160 | "158","256" 161 | "159","256" 162 | "160","256" 163 | "161","256" 164 | "162","256" 165 | "163","256" 166 | "164","256" 167 | "165","256" 168 | "166","256" 169 | "167","256" 170 | "168","256" 171 | "169","256" 172 | "170","256" 173 | "171","256" 174 | "172","256" 175 | "173","256" 176 | "174","256" 177 | "175","256" 178 | "176","256" 179 | "177","256" 180 | "178","256" 181 | "179","256" 182 | "180","256" 183 | "181","256" 184 | "182","256" 185 | "183","256" 186 | "184","256" 187 | "185","256" 188 | "186","256" 189 | "187","256" 190 | "188","256" 191 | "189","256" 192 | "190","256" 193 | "191","256" 194 | "192","256" 195 | "193","256" 196 | "194","256" 197 | "195","256" 198 | "196","256" 199 | "197","256" 200 | "198","256" 201 | "199","256" 202 | "200","256" 203 | "201","256" 204 | "202","256" 205 | "203","256" 206 | "204","256" 207 | "205","256" 208 | "206","256" 209 | "207","256" 210 | "208","256" 211 | "209","256" 212 | "210","256" 213 | "211","256" 214 | "212","256" 215 | "213","256" 216 | "214","256" 217 | "215","256" 218 | "216","256" 219 | "217","256" 220 | "218","256" 221 | "219","256" 222 | "220","256" 223 | "221","256" 224 | "222","256" 225 | "223","256" 226 | "224","256" 227 | "225","256" 228 | "226","256" 229 | "227","256" 230 | "228","256" 231 | "229","256" 232 | "230","256" 233 | "231","256" 234 | "232","256" 235 | "233","256" 236 | "234","256" 237 | "235","256" 238 | "236","256" 239 | "237","256" 240 | "238","256" 241 | "239","256" 242 | "240","256" 243 | "241","256" 244 | "242","256" 245 | "243","256" 246 | "244","256" 247 | "245","256" 248 | "246","256" 249 | "247","256" 250 | "248","256" 251 | "249","256" 252 | "250","256" 253 | "251","256" 254 | "252","256" 255 | "253","256" 256 | "254","256" 257 | "255","256" 258 | -------------------------------------------------------------------------------- /out/blueVC_1M.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M.png -------------------------------------------------------------------------------- /out/blueVC_1M_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_1.png -------------------------------------------------------------------------------- /out/blueVC_1M_102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_102.png -------------------------------------------------------------------------------- /out/blueVC_1M_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_128.png -------------------------------------------------------------------------------- /out/blueVC_1M_153.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_153.png -------------------------------------------------------------------------------- /out/blueVC_1M_179.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_179.png -------------------------------------------------------------------------------- /out/blueVC_1M_204.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_204.png -------------------------------------------------------------------------------- /out/blueVC_1M_230.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_230.png -------------------------------------------------------------------------------- /out/blueVC_1M_25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_25.png -------------------------------------------------------------------------------- /out/blueVC_1M_254.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_254.png -------------------------------------------------------------------------------- /out/blueVC_1M_51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_51.png -------------------------------------------------------------------------------- /out/blueVC_1M_76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1M_76.png -------------------------------------------------------------------------------- /out/blueVC_1_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_1.png -------------------------------------------------------------------------------- /out/blueVC_1_102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_102.png -------------------------------------------------------------------------------- /out/blueVC_1_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_128.png -------------------------------------------------------------------------------- /out/blueVC_1_153.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_153.png -------------------------------------------------------------------------------- /out/blueVC_1_179.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_179.png -------------------------------------------------------------------------------- /out/blueVC_1_204.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_204.png -------------------------------------------------------------------------------- /out/blueVC_1_230.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_230.png -------------------------------------------------------------------------------- /out/blueVC_1_25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_25.png -------------------------------------------------------------------------------- /out/blueVC_1_254.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_254.png -------------------------------------------------------------------------------- /out/blueVC_1_51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_51.png -------------------------------------------------------------------------------- /out/blueVC_1_76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_1_76.png -------------------------------------------------------------------------------- /out/blueVC_2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2.gif -------------------------------------------------------------------------------- /out/blueVC_2.histogram.csv: -------------------------------------------------------------------------------- 1 | "Value","Count" 2 | "0","256" 3 | "1","256" 4 | "2","256" 5 | "3","256" 6 | "4","256" 7 | "5","256" 8 | "6","256" 9 | "7","256" 10 | "8","256" 11 | "9","256" 12 | "10","256" 13 | "11","256" 14 | "12","256" 15 | "13","256" 16 | "14","256" 17 | "15","256" 18 | "16","256" 19 | "17","256" 20 | "18","256" 21 | "19","256" 22 | "20","256" 23 | "21","256" 24 | "22","256" 25 | "23","256" 26 | "24","256" 27 | "25","256" 28 | "26","256" 29 | "27","256" 30 | "28","256" 31 | "29","256" 32 | "30","256" 33 | "31","256" 34 | "32","256" 35 | "33","256" 36 | "34","256" 37 | "35","256" 38 | "36","256" 39 | "37","256" 40 | "38","256" 41 | "39","256" 42 | "40","256" 43 | "41","256" 44 | "42","256" 45 | "43","256" 46 | "44","256" 47 | "45","256" 48 | "46","256" 49 | "47","256" 50 | "48","256" 51 | "49","256" 52 | "50","256" 53 | "51","256" 54 | "52","256" 55 | "53","256" 56 | "54","256" 57 | "55","256" 58 | "56","256" 59 | "57","256" 60 | "58","256" 61 | "59","256" 62 | "60","256" 63 | "61","256" 64 | "62","256" 65 | "63","256" 66 | "64","256" 67 | "65","256" 68 | "66","256" 69 | "67","256" 70 | "68","256" 71 | "69","256" 72 | "70","256" 73 | "71","256" 74 | "72","256" 75 | "73","256" 76 | "74","256" 77 | "75","256" 78 | "76","256" 79 | "77","256" 80 | "78","256" 81 | "79","256" 82 | "80","256" 83 | "81","256" 84 | "82","256" 85 | "83","256" 86 | "84","256" 87 | "85","256" 88 | "86","256" 89 | "87","256" 90 | "88","256" 91 | "89","256" 92 | "90","256" 93 | "91","256" 94 | "92","256" 95 | "93","256" 96 | "94","256" 97 | "95","256" 98 | "96","256" 99 | "97","256" 100 | "98","256" 101 | "99","256" 102 | "100","256" 103 | "101","256" 104 | "102","256" 105 | "103","256" 106 | "104","256" 107 | "105","256" 108 | "106","256" 109 | "107","256" 110 | "108","256" 111 | "109","256" 112 | "110","256" 113 | "111","256" 114 | "112","256" 115 | "113","256" 116 | "114","256" 117 | "115","256" 118 | "116","256" 119 | "117","256" 120 | "118","256" 121 | "119","256" 122 | "120","256" 123 | "121","256" 124 | "122","256" 125 | "123","256" 126 | "124","256" 127 | "125","256" 128 | "126","256" 129 | "127","256" 130 | "128","256" 131 | "129","256" 132 | "130","256" 133 | "131","256" 134 | "132","256" 135 | "133","256" 136 | "134","256" 137 | "135","256" 138 | "136","256" 139 | "137","256" 140 | "138","256" 141 | "139","256" 142 | "140","256" 143 | "141","256" 144 | "142","256" 145 | "143","256" 146 | "144","256" 147 | "145","256" 148 | "146","256" 149 | "147","256" 150 | "148","256" 151 | "149","256" 152 | "150","256" 153 | "151","256" 154 | "152","256" 155 | "153","256" 156 | "154","256" 157 | "155","256" 158 | "156","256" 159 | "157","256" 160 | "158","256" 161 | "159","256" 162 | "160","256" 163 | "161","256" 164 | "162","256" 165 | "163","256" 166 | "164","256" 167 | "165","256" 168 | "166","256" 169 | "167","256" 170 | "168","256" 171 | "169","256" 172 | "170","256" 173 | "171","256" 174 | "172","256" 175 | "173","256" 176 | "174","256" 177 | "175","256" 178 | "176","256" 179 | "177","256" 180 | "178","256" 181 | "179","256" 182 | "180","256" 183 | "181","256" 184 | "182","256" 185 | "183","256" 186 | "184","256" 187 | "185","256" 188 | "186","256" 189 | "187","256" 190 | "188","256" 191 | "189","256" 192 | "190","256" 193 | "191","256" 194 | "192","256" 195 | "193","256" 196 | "194","256" 197 | "195","256" 198 | "196","256" 199 | "197","256" 200 | "198","256" 201 | "199","256" 202 | "200","256" 203 | "201","256" 204 | "202","256" 205 | "203","256" 206 | "204","256" 207 | "205","256" 208 | "206","256" 209 | "207","256" 210 | "208","256" 211 | "209","256" 212 | "210","256" 213 | "211","256" 214 | "212","256" 215 | "213","256" 216 | "214","256" 217 | "215","256" 218 | "216","256" 219 | "217","256" 220 | "218","256" 221 | "219","256" 222 | "220","256" 223 | "221","256" 224 | "222","256" 225 | "223","256" 226 | "224","256" 227 | "225","256" 228 | "226","256" 229 | "227","256" 230 | "228","256" 231 | "229","256" 232 | "230","256" 233 | "231","256" 234 | "232","256" 235 | "233","256" 236 | "234","256" 237 | "235","256" 238 | "236","256" 239 | "237","256" 240 | "238","256" 241 | "239","256" 242 | "240","256" 243 | "241","256" 244 | "242","256" 245 | "243","256" 246 | "244","256" 247 | "245","256" 248 | "246","256" 249 | "247","256" 250 | "248","256" 251 | "249","256" 252 | "250","256" 253 | "251","256" 254 | "252","256" 255 | "253","256" 256 | "254","256" 257 | "255","256" 258 | -------------------------------------------------------------------------------- /out/blueVC_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2.png -------------------------------------------------------------------------------- /out/blueVC_2_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_1.png -------------------------------------------------------------------------------- /out/blueVC_2_102.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_102.png -------------------------------------------------------------------------------- /out/blueVC_2_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_128.png -------------------------------------------------------------------------------- /out/blueVC_2_153.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_153.png -------------------------------------------------------------------------------- /out/blueVC_2_179.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_179.png -------------------------------------------------------------------------------- /out/blueVC_2_204.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_204.png -------------------------------------------------------------------------------- /out/blueVC_2_230.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_230.png -------------------------------------------------------------------------------- /out/blueVC_2_25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_25.png -------------------------------------------------------------------------------- /out/blueVC_2_254.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_254.png -------------------------------------------------------------------------------- /out/blueVC_2_51.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_51.png -------------------------------------------------------------------------------- /out/blueVC_2_76.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Atrix256/VoidAndCluster/1e5be47eb4eba2c87926d00907c2b027bd1a8942/out/blueVC_2_76.png -------------------------------------------------------------------------------- /scoped_timer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | struct ScopedTimer 6 | { 7 | ScopedTimer(const char* label, bool twoNewLines = true) 8 | : m_twoNewLines(twoNewLines) 9 | { 10 | m_start = std::chrono::high_resolution_clock::now(); 11 | printf("%s...\n", label); 12 | } 13 | 14 | ~ScopedTimer() 15 | { 16 | std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); 17 | std::chrono::duration time_span = std::chrono::duration_cast>(end - m_start); 18 | printf("%f ms\n", time_span.count() * 1000.0f); 19 | if (m_twoNewLines) 20 | printf("\n"); 21 | } 22 | 23 | bool m_twoNewLines; 24 | std::chrono::high_resolution_clock::time_point m_start; 25 | }; -------------------------------------------------------------------------------- /settings.h: -------------------------------------------------------------------------------- 1 | 2 | #pragma once 3 | 4 | #define DETERMINISTIC() true // if true, will use the seed below for everything, else will randomly generate a seed. 5 | 6 | #define DETERMINISTIC_SEED() unsigned(783104853), unsigned(4213684301), unsigned(3526061164), unsigned(614346169), unsigned(478811579), unsigned(2044310268), unsigned(3671768129), unsigned(206439072) 7 | 8 | #define THRESHOLD_SAMPLES() 11 // the number of samples for threshold testing. 9 | 10 | #define SAVE_VOIDCLUSTER_INITIALBP() false 11 | #define SAVE_VOIDCLUSTER_PHASE1() false 12 | #define SAVE_VOIDCLUSTER_INITIALPOINTS() false 13 | -------------------------------------------------------------------------------- /simple_fft/check_fft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __SIMPLE_FFT__CHECK_FFT_HPP__ 2 | #define __SIMPLE_FFT__CHECK_FFT_HPP__ 3 | 4 | #include "fft_settings.h" 5 | #include "error_handling.hpp" 6 | #include "copy_array.hpp" 7 | #include 8 | #include 9 | #include 10 | 11 | using std::size_t; 12 | 13 | namespace simple_fft { 14 | namespace check_fft_private { 15 | 16 | enum CheckMode 17 | { 18 | CHECK_FFT_PARSEVAL, 19 | CHECK_FFT_ENERGY, 20 | CHECK_FFT_EQUALITY 21 | }; 22 | 23 | template 24 | void getMaxAbsoluteAndRelativeErrorNorms(const TArray1D & array1, 25 | const TComplexArray1D & array2, const size_t size, 26 | real_type & max_absolute_error_norm, 27 | real_type & max_relative_error_norm) 28 | { 29 | using std::abs; 30 | 31 | real_type current_error; 32 | 33 | // NOTE: no parallelization here, it is a completely sequential loop! 34 | for(size_t i = 0; i < size; ++i) { 35 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 36 | current_error = abs(array1[i] - array2[i]); 37 | #else 38 | current_error = abs(array1(i) - array2(i)); 39 | #endif 40 | if (current_error > max_absolute_error_norm) { 41 | max_absolute_error_norm = current_error; 42 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 43 | if (abs(array1[i]) > abs(array2[i])) { 44 | max_relative_error_norm = (abs(array1[i]) > 1e-20 45 | ? max_absolute_error_norm / abs(array1[i]) 46 | : 0.0); 47 | } 48 | else { 49 | max_relative_error_norm = (abs(array2[i]) > 1e-20 50 | ? max_absolute_error_norm / abs(array2[i]) 51 | : 0.0); 52 | } 53 | #else 54 | if (abs(array1(i)) > abs(array2(i))) { 55 | max_relative_error_norm = (abs(array1(i)) > 1e-20 56 | ? max_absolute_error_norm / abs(array1(i)) 57 | : 0.0); 58 | } 59 | else { 60 | max_relative_error_norm = (abs(array2(i)) > 1e-20 61 | ? max_absolute_error_norm / abs(array2(i)) 62 | : 0.0); 63 | } 64 | #endif 65 | } 66 | } 67 | } 68 | 69 | template 70 | void getMaxAbsoluteAndRelativeErrorNorms(const TArray2D & array1, 71 | const TComplexArray2D & array2, 72 | const size_t size1, const size_t size2, 73 | real_type & max_absolute_error_norm, 74 | real_type & max_relative_error_norm) 75 | { 76 | using std::abs; 77 | 78 | real_type current_error; 79 | 80 | // NOTE: no parallelization here, it is a completely sequential loop! 81 | for(int i = 0; i < static_cast(size1); ++i) { 82 | for(int j = 0; j < static_cast(size2); ++j) { 83 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 84 | current_error = abs(array1[i][j] - array2[i][j]); 85 | #else 86 | current_error = abs(array1(i,j) - array2(i,j)); 87 | #endif 88 | if (current_error > max_absolute_error_norm) { 89 | max_absolute_error_norm = current_error; 90 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 91 | if (abs(array1[i][j]) > abs(array2[i][j])) { 92 | max_relative_error_norm = (abs(array1[i][j]) > 1e-20 93 | ? max_absolute_error_norm / abs(array1[i][j]) 94 | : 0.0); 95 | } 96 | else { 97 | max_relative_error_norm = (abs(array2[i][j]) > 1e-20 98 | ? max_absolute_error_norm / abs(array2[i][j]) 99 | : 0.0); 100 | } 101 | #else 102 | if (abs(array1(i,j)) > abs(array2(i,j))) { 103 | max_relative_error_norm = (abs(array1(i,j)) > 1e-20 104 | ? max_absolute_error_norm / abs(array1(i,j)) 105 | : 0.0); 106 | } 107 | else { 108 | max_relative_error_norm = (abs(array2(i,j)) > 1e-20 109 | ? max_absolute_error_norm / abs(array2(i,j)) 110 | : 0.0); 111 | } 112 | #endif 113 | } 114 | } 115 | } 116 | } 117 | 118 | template 119 | void getMaxAbsoluteAndRelativeErrorNorms(const TArray3D & array1, const TComplexArray3D & array2, 120 | const size_t size1, const size_t size2, 121 | const size_t size3, real_type & max_absolute_error_norm, 122 | real_type & max_relative_error_norm) 123 | { 124 | using std::abs; 125 | 126 | real_type current_error; 127 | 128 | // NOTE: no parallelization here, it is a completely sequential loop! 129 | for(int i = 0; i < static_cast(size1); ++i) { 130 | for(int j = 0; j < static_cast(size2); ++j) { 131 | for(int k = 0; k < static_cast(size3); ++k) { 132 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 133 | current_error = abs(array1[i][j][k] - array2[i][j][k]); 134 | #else 135 | current_error = abs(array1(i,j,k) - array2(i,j,k)); 136 | #endif 137 | if (current_error > max_absolute_error_norm) { 138 | max_absolute_error_norm = current_error; 139 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 140 | if (abs(array1[i][j][k]) > abs(array2[i][j][k])) { 141 | max_relative_error_norm = (abs(array1[i][j][k]) > 1e-20 142 | ? max_absolute_error_norm / abs(array1[i][j][k]) 143 | : 0.0); 144 | } 145 | else { 146 | max_relative_error_norm = (abs(array2[i][j][k]) > 1e-20 147 | ? max_absolute_error_norm / abs(array2[i][j][k]) 148 | : 0.0); 149 | } 150 | #else 151 | if (abs(array1(i,j,k)) > abs(array2(i,j,k))) { 152 | max_relative_error_norm = (abs(array1(i,j,k)) > 1e-20 153 | ? max_absolute_error_norm / abs(array1(i,j,k)) 154 | : 0.0); 155 | } 156 | else { 157 | max_relative_error_norm = (abs(array2(i,j,k)) > 1e-20 158 | ? max_absolute_error_norm / abs(array2(i,j,k)) 159 | : 0.0); 160 | } 161 | #endif 162 | } 163 | } 164 | } 165 | } 166 | } 167 | 168 | template 169 | real_type squareAbsAccumulate(const TArray1D & array, const size_t size, 170 | const real_type init) 171 | { 172 | int size_signed = static_cast(size); 173 | real_type sum = init; 174 | 175 | using std::abs; 176 | 177 | #ifndef __clang__ 178 | #ifdef __USE_OPENMP 179 | #pragma omp parallel for reduction(+:sum) 180 | #endif 181 | #endif 182 | for(int i = 0; i < size_signed; ++i) { 183 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 184 | sum += abs(array[i] * array[i]); 185 | #else 186 | sum += abs(array(i) * array(i)); 187 | #endif 188 | } 189 | 190 | return sum; 191 | } 192 | 193 | template 194 | real_type squareAbsAccumulate(const TArray2D & array, const size_t size1, 195 | const size_t size2, const real_type init) 196 | { 197 | int size1_signed = static_cast(size1); 198 | int size2_signed = static_cast(size2); 199 | real_type sum = init; 200 | 201 | using std::abs; 202 | 203 | #ifndef __clang__ 204 | #ifdef __USE_OPENMP 205 | #pragma omp parallel for reduction(+:sum) 206 | #endif 207 | #endif 208 | for(int i = 0; i < size1_signed; ++i) { 209 | for(int j = 0; j < size2_signed; ++j) { 210 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 211 | sum += abs(array[i][j] * array[i][j]); 212 | #else 213 | sum += abs(array(i,j) * array(i,j)); 214 | #endif 215 | } 216 | } 217 | 218 | return sum; 219 | } 220 | 221 | template 222 | real_type squareAbsAccumulate(const TArray3D & array, const size_t size1, 223 | const size_t size2, const size_t size3, 224 | const real_type init) 225 | { 226 | int size1_signed = static_cast(size1); 227 | int size2_signed = static_cast(size2); 228 | int size3_signed = static_cast(size3); 229 | real_type sum = init; 230 | 231 | using std::abs; 232 | 233 | #ifndef __clang__ 234 | #ifdef __USE_OPENMP 235 | #pragma omp parallel for reduction(+:sum) 236 | #endif 237 | #endif 238 | for(int i = 0; i < size1_signed; ++i) { 239 | for(int j = 0; j < size2_signed; ++j) { 240 | for(int k = 0; k < size3_signed; ++k) { 241 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 242 | sum += abs(array[i][j][k] * array[i][j][k]); 243 | #else 244 | sum += abs(array(i,j,k) * array(i,j,k)); 245 | #endif 246 | } 247 | } 248 | } 249 | 250 | return sum; 251 | } 252 | 253 | // Generic template for CCheckFFT struct followed by its explicit specializations 254 | // for certain numbers of dimensions. TArray can be either of real or complex type. 255 | // The technique is similar to the one applied for CFFT struct. 256 | template 257 | struct CCheckFFT 258 | {}; 259 | 260 | template 261 | struct CCheckFFT 262 | { 263 | static bool check_fft(const TArray1D & data_before, 264 | const TComplexArray1D & data_after, 265 | const size_t size, const real_type relative_tolerance, 266 | real_type & discrepancy, const CheckMode check_mode, 267 | const char *& error_description) 268 | { 269 | using namespace error_handling; 270 | 271 | if(0 == size) { 272 | GetErrorDescription(EC_NUM_OF_ELEMS_IS_ZERO, error_description); 273 | return false; 274 | } 275 | 276 | if ( (CHECK_FFT_PARSEVAL != check_mode) && 277 | (CHECK_FFT_ENERGY != check_mode) && 278 | (CHECK_FFT_EQUALITY != check_mode) ) 279 | { 280 | GetErrorDescription(EC_WRONG_CHECK_FFT_MODE, error_description); 281 | return false; 282 | } 283 | 284 | if (CHECK_FFT_EQUALITY != check_mode) 285 | { 286 | real_type sum_before = squareAbsAccumulate(data_before, size, 0.0); 287 | real_type sum_after = squareAbsAccumulate(data_after, size, 0.0); 288 | 289 | if (CHECK_FFT_PARSEVAL == check_mode) { 290 | sum_after /= size; 291 | } 292 | 293 | using std::abs; 294 | 295 | discrepancy = abs(sum_before - sum_after); 296 | 297 | if (discrepancy / ((sum_before < 1e-20) ? (sum_before + 1e-20) : sum_before) > relative_tolerance) { 298 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 299 | return false; 300 | } 301 | else { 302 | return true; 303 | } 304 | } 305 | else { 306 | real_type relative_error; 307 | getMaxAbsoluteAndRelativeErrorNorms(data_before, data_after, size, 308 | discrepancy, relative_error); 309 | if (relative_error < relative_tolerance) { 310 | return true; 311 | } 312 | else { 313 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 314 | return false; 315 | } 316 | } 317 | } 318 | }; 319 | 320 | template 321 | struct CCheckFFT 322 | { 323 | static bool check_fft(const TArray2D & data_before, 324 | const TComplexArray2D & data_after, 325 | const size_t size1, const size_t size2, 326 | const real_type relative_tolerance, real_type & discrepancy, 327 | const CheckMode check_mode, const char *& error_description) 328 | { 329 | using namespace error_handling; 330 | 331 | if( (0 == size1) || (0 == size2) ) { 332 | GetErrorDescription(EC_NUM_OF_ELEMS_IS_ZERO, error_description); 333 | return false; 334 | } 335 | 336 | if ( (CHECK_FFT_PARSEVAL != check_mode) && 337 | (CHECK_FFT_ENERGY != check_mode) && 338 | (CHECK_FFT_EQUALITY != check_mode) ) 339 | { 340 | GetErrorDescription(EC_WRONG_CHECK_FFT_MODE, error_description); 341 | return false; 342 | } 343 | 344 | if (CHECK_FFT_EQUALITY != check_mode) 345 | { 346 | real_type sum_before = squareAbsAccumulate(data_before, size1, size2, 0.0); 347 | real_type sum_after = squareAbsAccumulate(data_after, size1, size2, 0.0); 348 | 349 | if (CHECK_FFT_PARSEVAL == check_mode) { 350 | sum_after /= size1 * size2; 351 | } 352 | 353 | using std::abs; 354 | 355 | discrepancy = abs(sum_before - sum_after); 356 | 357 | if (discrepancy / ((sum_before < 1e-20) ? (sum_before + 1e-20) : sum_before) > relative_tolerance) { 358 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 359 | return false; 360 | } 361 | else { 362 | return true; 363 | } 364 | } 365 | else { 366 | real_type relative_error; 367 | getMaxAbsoluteAndRelativeErrorNorms(data_before, data_after, size1, 368 | size2, discrepancy, relative_error); 369 | if (relative_error < relative_tolerance) { 370 | return true; 371 | } 372 | else { 373 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 374 | return false; 375 | } 376 | } 377 | } 378 | }; 379 | 380 | template 381 | struct CCheckFFT 382 | { 383 | static bool check_fft(const TArray3D & data_before, 384 | const TComplexArray3D & data_after, 385 | const size_t size1, const size_t size2, const size_t size3, 386 | const real_type relative_tolerance, real_type & discrepancy, 387 | const CheckMode check_mode, const char *& error_description) 388 | { 389 | using namespace error_handling; 390 | 391 | if( (0 == size1) || (0 == size2) || (0 == size3) ) { 392 | GetErrorDescription(EC_NUM_OF_ELEMS_IS_ZERO, error_description); 393 | return false; 394 | } 395 | 396 | if ( (CHECK_FFT_PARSEVAL != check_mode) && 397 | (CHECK_FFT_ENERGY != check_mode) && 398 | (CHECK_FFT_EQUALITY != check_mode) ) 399 | { 400 | GetErrorDescription(EC_WRONG_CHECK_FFT_MODE, error_description); 401 | return false; 402 | } 403 | 404 | if (CHECK_FFT_EQUALITY != check_mode) 405 | { 406 | real_type sum_before = squareAbsAccumulate(data_before, size1, size2, size3, 0.0); 407 | real_type sum_after = squareAbsAccumulate(data_after, size1, size2, size3, 0.0); 408 | 409 | if (CHECK_FFT_PARSEVAL == check_mode) { 410 | sum_after /= size1 * size2 * size3; 411 | } 412 | 413 | using std::abs; 414 | 415 | discrepancy = abs(sum_before - sum_after); 416 | 417 | if (discrepancy / ((sum_before < 1e-20) ? (sum_before + 1e-20) : sum_before) > relative_tolerance) { 418 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 419 | return false; 420 | } 421 | else { 422 | return true; 423 | } 424 | } 425 | else { 426 | real_type relative_error; 427 | getMaxAbsoluteAndRelativeErrorNorms(data_before, data_after, size1, 428 | size2, size3, discrepancy, relative_error); 429 | if (relative_error < relative_tolerance) { 430 | return true; 431 | } 432 | else { 433 | GetErrorDescription(EC_RELATIVE_ERROR_TOO_LARGE, error_description); 434 | return false; 435 | } 436 | } 437 | } 438 | }; 439 | 440 | } // namespace check_fft_private 441 | 442 | namespace check_fft { 443 | 444 | template 445 | bool checkParsevalTheorem(const TArray1D & data_before_FFT, 446 | const TComplexArray1D & data_after_FFT, 447 | const size_t size, const real_type relative_tolerance, 448 | real_type & discrepancy, const char *& error_description) 449 | { 450 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 451 | data_after_FFT, size, relative_tolerance, 452 | discrepancy, check_fft_private::CHECK_FFT_PARSEVAL, 453 | error_description); 454 | } 455 | 456 | template 457 | bool checkParsevalTheorem(const TArray2D & data_before_FFT, 458 | const TComplexArray2D & data_after_FFT, 459 | const size_t size1, const size_t size2, 460 | const real_type relative_tolerance, 461 | real_type & discrepancy, const char *& error_description) 462 | { 463 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 464 | data_after_FFT, size1, size2, relative_tolerance, 465 | discrepancy, check_fft_private::CHECK_FFT_PARSEVAL, 466 | error_description); 467 | } 468 | 469 | template 470 | bool checkParsevalTheorem(const TArray3D & data_before_FFT, 471 | const TComplexArray3D & data_after_FFT, 472 | const size_t size1, const size_t size2, const size_t size3, 473 | const real_type relative_tolerance, real_type & discrepancy, 474 | const char *& error_description) 475 | { 476 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 477 | data_after_FFT, size1, size2, size3, 478 | relative_tolerance, discrepancy, 479 | check_fft_private::CHECK_FFT_PARSEVAL, 480 | error_description); 481 | } 482 | 483 | template 484 | bool checkEnergyConservation(const TArray1D & data_before_FFT, 485 | const TComplexArray1D & data_after_FFT_and_IFFT, 486 | const size_t size, const real_type relative_tolerance, 487 | real_type & discrepancy, const char *& error_description) 488 | { 489 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 490 | data_after_FFT_and_IFFT, size, relative_tolerance, 491 | discrepancy, check_fft_private::CHECK_FFT_ENERGY, 492 | error_description); 493 | } 494 | 495 | template 496 | bool checkEnergyConservation(const TArray2D & data_before_FFT, 497 | const TComplexArray2D & data_after_FFT_and_IFFT, 498 | const size_t size1, const size_t size2, 499 | const real_type relative_tolerance, 500 | real_type & discrepancy, const char *& error_description) 501 | { 502 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 503 | data_after_FFT_and_IFFT, size1, size2, 504 | relative_tolerance, discrepancy, 505 | check_fft_private::CHECK_FFT_ENERGY, 506 | error_description); 507 | } 508 | 509 | template 510 | bool checkEnergyConservation(const TArray3D & data_before_FFT, 511 | const TComplexArray3D & data_after_FFT_and_IFFT, 512 | const size_t size1, const size_t size2, const size_t size3, 513 | const real_type relative_tolerance, real_type & discrepancy, 514 | const char *& error_description) 515 | { 516 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 517 | data_after_FFT_and_IFFT, size1, size2, 518 | size3, relative_tolerance, discrepancy, 519 | check_fft_private::CHECK_FFT_ENERGY, 520 | error_description); 521 | } 522 | 523 | template 524 | bool checkEquality(const TArray1D & data_before_FFT, 525 | const TComplexArray1D & data_after_FFT_and_IFFT, 526 | const size_t size, const real_type relative_tolerance, 527 | real_type & discrepancy, const char *& error_description) 528 | { 529 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 530 | data_after_FFT_and_IFFT, size, relative_tolerance, 531 | discrepancy, check_fft_private::CHECK_FFT_EQUALITY, 532 | error_description); 533 | } 534 | 535 | template 536 | bool checkEquality(const TArray2D & data_before_FFT, 537 | const TComplexArray2D & data_after_FFT_and_IFFT, const size_t size1, 538 | const size_t size2, const real_type relative_tolerance, 539 | real_type & discrepancy, const char *& error_description) 540 | { 541 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 542 | data_after_FFT_and_IFFT, size1, size2, 543 | relative_tolerance, discrepancy, 544 | check_fft_private::CHECK_FFT_EQUALITY, 545 | error_description); 546 | } 547 | 548 | template 549 | bool checkEquality(const TArray3D & data_before_FFT, 550 | const TComplexArray3D & data_after_FFT_and_IFFT, const size_t size1, 551 | const size_t size2, const size_t size3, const real_type relative_tolerance, 552 | real_type & discrepancy, const char *& error_description) 553 | { 554 | return check_fft_private::CCheckFFT::check_fft(data_before_FFT, 555 | data_after_FFT_and_IFFT, size1, size2, 556 | size3, relative_tolerance, discrepancy, 557 | check_fft_private::CHECK_FFT_EQUALITY, 558 | error_description); 559 | } 560 | 561 | } // namespace check_fft 562 | } // namespace simple_fft 563 | 564 | #endif // __SIMPLE_FFT__CHECK_FFT_HPP__ 565 | -------------------------------------------------------------------------------- /simple_fft/copy_array.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __SIMPLE_FFT__COPY_ARRAY_HPP 2 | #define __SIMPLE_FFT__COPY_ARRAY_HPP 3 | 4 | #include "fft_settings.h" 5 | #include "error_handling.hpp" 6 | #include 7 | 8 | using std::size_t; 9 | 10 | namespace simple_fft { 11 | namespace copy_array { 12 | 13 | template 14 | void copyArray(const TComplexArray1D & data_from, TComplexArray1D & data_to, 15 | const size_t size) 16 | { 17 | int size_signed = static_cast(size); 18 | 19 | #ifndef __clang__ 20 | #ifdef __USE_OPENMP 21 | #pragma omp parallel for 22 | #endif 23 | #endif 24 | for(int i = 0; i < size_signed; ++i) { 25 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 26 | data_to[i] = data_from[i]; 27 | #else 28 | data_to(i) = data_from(i); 29 | #endif 30 | } 31 | } 32 | 33 | template 34 | void copyArray(const TRealArray1D & data_from, TComplexArray1D & data_to, 35 | const size_t size) 36 | { 37 | int size_signed = static_cast(size); 38 | 39 | // NOTE: user's complex type should have constructor like 40 | // "complex(real, imag)", where each of real and imag has 41 | // real type. 42 | 43 | #ifndef __clang__ 44 | #ifdef __USE_OPENMP 45 | #pragma omp parallel for 46 | #endif 47 | #endif 48 | for(int i = 0; i < size_signed; ++i) { 49 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 50 | data_to[i] = complex_type(data_from[i], 0.0); 51 | #else 52 | data_to(i) = complex_type(data_from(i), 0.0); 53 | #endif 54 | } 55 | } 56 | 57 | template 58 | void copyArray(const TComplexArray2D & data_from, TComplexArray2D & data_to, 59 | const size_t size1, const size_t size2) 60 | { 61 | int size1_signed = static_cast(size1); 62 | int size2_signed = static_cast(size2); 63 | 64 | #ifndef __clang__ 65 | #ifdef __USE_OPENMP 66 | #pragma omp parallel for 67 | #endif 68 | #endif 69 | for(int i = 0; i < size1_signed; ++i) { 70 | for(int j = 0; j < size2_signed; ++j) { 71 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 72 | data_to[i][j] = data_from[i][j]; 73 | #else 74 | data_to(i,j) = data_from(i,j); 75 | #endif 76 | } 77 | } 78 | } 79 | 80 | template 81 | void copyArray(const TRealArray2D & data_from, TComplexArray2D & data_to, 82 | const size_t size1, const size_t size2) 83 | { 84 | int size1_signed = static_cast(size1); 85 | int size2_signed = static_cast(size2); 86 | 87 | // NOTE: user's complex type should have constructor like 88 | // "complex(real, imag)", where each of real and imag has 89 | // real type. 90 | 91 | #ifndef __clang__ 92 | #ifdef __USE_OPENMP 93 | #pragma omp parallel for 94 | #endif 95 | #endif 96 | for(int i = 0; i < size1_signed; ++i) { 97 | for(int j = 0; j < size2_signed; ++j) { 98 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 99 | data_to[i][j] = complex_type(data_from[i][j], 0.0); 100 | #else 101 | data_to(i,j) = complex_type(data_from(i,j), 0.0); 102 | #endif 103 | } 104 | } 105 | } 106 | 107 | template 108 | void copyArray(const TComplexArray3D & data_from, TComplexArray3D & data_to, 109 | const size_t size1, const size_t size2, const size_t size3) 110 | { 111 | int size1_signed = static_cast(size1); 112 | int size2_signed = static_cast(size2); 113 | int size3_signed = static_cast(size3); 114 | 115 | #ifndef __clang__ 116 | #ifdef __USE_OPENMP 117 | #pragma omp parallel for 118 | #endif 119 | #endif 120 | for(int i = 0; i < size1_signed; ++i) { 121 | for(int j = 0; j < size2_signed; ++j) { 122 | for(int k = 0; k < size3_signed; ++k) { 123 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 124 | data_to[i][j][k] = data_from[i][j][k]; 125 | #else 126 | data_to(i,j,k) = data_from(i,j,k); 127 | #endif 128 | } 129 | } 130 | } 131 | } 132 | 133 | template 134 | void copyArray(const TRealArray3D & data_from, TComplexArray3D & data_to, 135 | const size_t size1, const size_t size2, const size_t size3) 136 | { 137 | int size1_signed = static_cast(size1); 138 | int size2_signed = static_cast(size2); 139 | int size3_signed = static_cast(size3); 140 | 141 | // NOTE: user's complex type should have constructor like 142 | // "complex(real, imag)", where each of real and imag has 143 | // real type. 144 | 145 | #ifndef __clang__ 146 | #ifdef __USE_OPENMP 147 | #pragma omp parallel for 148 | #endif 149 | #endif 150 | for(int i = 0; i < size1_signed; ++i) { 151 | for(int j = 0; j < size2_signed; ++j) { 152 | for(int k = 0; k < size3_signed; ++k) { 153 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 154 | data_to[i][j][k] = complex_type(data_from[i][j][k], 0.0); 155 | #else 156 | data_to(i,j,k) = complex_type(data_from(i,j,k), 0.0); 157 | #endif 158 | } 159 | } 160 | } 161 | } 162 | 163 | } // namespace copy_array 164 | } // namespace simple_fft 165 | 166 | #endif // __SIMPLE_FFT__COPY_ARRAY_HPP 167 | -------------------------------------------------------------------------------- /simple_fft/error_handling.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __SIMPLE_FFT__ERROR_HANDLING_HPP 2 | #define __SIMPLE_FFT__ERROR_HANDLING_HPP 3 | 4 | namespace simple_fft { 5 | namespace error_handling { 6 | 7 | enum EC_SimpleFFT 8 | { 9 | EC_SUCCESS = 0, 10 | EC_UNSUPPORTED_DIMENSIONALITY, 11 | EC_WRONG_FFT_DIRECTION, 12 | EC_ONE_OF_DIMS_ISNT_POWER_OF_TWO, 13 | EC_NUM_OF_ELEMS_IS_ZERO, 14 | EC_WRONG_CHECK_FFT_MODE, 15 | EC_RELATIVE_ERROR_TOO_LARGE 16 | }; 17 | 18 | inline void GetErrorDescription(const EC_SimpleFFT error_code, 19 | const char *& error_description) 20 | { 21 | switch(error_code) 22 | { 23 | case EC_SUCCESS: 24 | error_description = "Calculation was successful!"; 25 | break; 26 | case EC_UNSUPPORTED_DIMENSIONALITY: 27 | error_description = "Unsupported dimensionality: currently only 1D, 2D " 28 | "and 3D arrays are supported"; 29 | break; 30 | case EC_WRONG_FFT_DIRECTION: 31 | error_description = "Wrong direction for FFT was specified"; 32 | break; 33 | case EC_ONE_OF_DIMS_ISNT_POWER_OF_TWO: 34 | error_description = "Unsupported dimensionality: one of dimensions is not " 35 | "a power of 2"; 36 | break; 37 | case EC_NUM_OF_ELEMS_IS_ZERO: 38 | error_description = "Number of elements for FFT or IFFT is zero!"; 39 | break; 40 | case EC_WRONG_CHECK_FFT_MODE: 41 | error_description = "Wrong check FFT mode was specified (should be either " 42 | "Parseval theorem or energy conservation check"; 43 | break; 44 | case EC_RELATIVE_ERROR_TOO_LARGE: 45 | error_description = "Relative error returned by FFT test exceeds specified " 46 | "relative tolerance"; 47 | break; 48 | default: 49 | error_description = "Unknown error"; 50 | break; 51 | } 52 | } 53 | 54 | } // namespace error_handling 55 | } // namespace simple_fft 56 | 57 | #endif // __SIMPLE_FFT__ERROR_HANDLING_HPP 58 | -------------------------------------------------------------------------------- /simple_fft/fft.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ---------------------------------------------------------------------------- 3 | * "THE BEER-WARE LICENSE" (Revision 42): 4 | * Dmitry Ivanov wrote this file. As long as you retain 5 | * this notice you can do whatever you want with this stuff. If we meet some day, 6 | * and you think this stuff is worth it, you can buy me a beer in return. 7 | * ---------------------------------------------------------------------------- 8 | */ 9 | 10 | #ifndef __SIMPLE_FFT__FFT_H__ 11 | #define __SIMPLE_FFT__FFT_H__ 12 | 13 | #include 14 | 15 | using std::size_t; 16 | 17 | /// The public API 18 | namespace simple_fft { 19 | 20 | /// FFT and IFFT functions 21 | 22 | // in-place, complex, forward 23 | template 24 | bool FFT(TComplexArray1D & data, const size_t size, const char *& error_description); 25 | 26 | template 27 | bool FFT(TComplexArray2D & data, const size_t size1, const size_t size2, 28 | const char *& error_description); 29 | 30 | template 31 | bool FFT(TComplexArray3D & data, const size_t size1, const size_t size2, const size_t size3, 32 | const char *& error_description); 33 | 34 | // in-place, complex, inverse 35 | template 36 | bool IFFT(TComplexArray1D & data, const size_t size, const char *& error_description); 37 | 38 | template 39 | bool IFFT(TComplexArray2D & data, const size_t size1, const size_t size2, 40 | const char *& error_description); 41 | 42 | template 43 | bool IFFT(TComplexArray3D & data, const size_t size1, const size_t size2, const size_t size3, 44 | const char *& error_description); 45 | 46 | // not-in-place, complex, forward 47 | template 48 | bool FFT(const TComplexArray1D & data_in, TComplexArray1D & data_out, 49 | const size_t size, const char *& error_description); 50 | 51 | template 52 | bool FFT(const TComplexArray2D & data_in, TComplexArray2D & data_out, 53 | const size_t size1, const size_t size2, const char *& error_description); 54 | 55 | template 56 | bool FFT(const TComplexArray3D & data_in, TComplexArray3D & data_out, 57 | const size_t size1, const size_t size2, const size_t size3, 58 | const char *& error_description); 59 | 60 | // not-in-place, complex, inverse 61 | template 62 | bool IFFT(const TComplexArray1D & data_in, TComplexArray1D & data_out, 63 | const size_t size, const char *& error_description); 64 | 65 | template 66 | bool IFFT(const TComplexArray2D & data_in, TComplexArray2D & data_out, 67 | const size_t size1, const size_t size2, const char *& error_description); 68 | 69 | template 70 | bool IFFT(const TComplexArray3D & data_in, TComplexArray3D & data_out, 71 | const size_t size1, const size_t size2, const size_t size3, 72 | const char *& error_description); 73 | 74 | // not-in-place, real, forward 75 | template 76 | bool FFT(const TRealArray1D & data_in, TComplexArray1D & data_out, 77 | const size_t size, const char *& error_description); 78 | 79 | template 80 | bool FFT(const TRealArray2D & data_in, TComplexArray2D & data_out, 81 | const size_t size1, const size_t size2, const char *& error_description); 82 | 83 | template 84 | bool FFT(const TRealArray3D & data_in, TComplexArray3D & data_out, 85 | const size_t size1, const size_t size2, const size_t size3, 86 | const char *& error_description); 87 | 88 | // NOTE: There is no inverse transform from complex spectrum to real signal 89 | // because round-off errors during computation of inverse FFT lead to the appearance 90 | // of signal imaginary components even though they are small by absolute value. 91 | // These can be ignored but the author of this file thinks adding such an function 92 | // would be wrong methodogically: looking at complex result, you can estimate 93 | // the value of spurious imaginary part. Otherwise you may never know that IFFT 94 | // provides too large imaginary values due to too small grid size, for example. 95 | 96 | } // namespace simple_fft 97 | 98 | #endif // __SIMPLE_FFT__FFT_H__ 99 | 100 | #include "fft.hpp" 101 | -------------------------------------------------------------------------------- /simple_fft/fft.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __SIMPLE_FFT__FFT_HPP__ 2 | #define __SIMPLE_FFT__FFT_HPP__ 3 | 4 | #include "copy_array.hpp" 5 | #include "fft_impl.hpp" 6 | 7 | namespace simple_fft { 8 | 9 | // in-place, complex, forward 10 | template 11 | bool FFT(TComplexArray1D & data, const size_t size, const char *& error_description) 12 | { 13 | return impl::CFFT::FFT_inplace(data, size, impl::FFT_FORWARD, 14 | error_description); 15 | } 16 | 17 | template 18 | bool FFT(TComplexArray2D & data, const size_t size1, const size_t size2, 19 | const char *& error_description) 20 | { 21 | return impl::CFFT::FFT_inplace(data, size1, size2, impl::FFT_FORWARD, 22 | error_description); 23 | } 24 | 25 | template 26 | bool FFT(TComplexArray3D & data, const size_t size1, const size_t size2, const size_t size3, 27 | const char *& error_description) 28 | { 29 | return impl::CFFT::FFT_inplace(data, size1, size2, size3, 30 | impl::FFT_FORWARD, 31 | error_description); 32 | } 33 | 34 | // in-place, complex, inverse 35 | template 36 | bool IFFT(TComplexArray1D & data, const size_t size, const char *& error_description) 37 | { 38 | return impl::CFFT::FFT_inplace(data, size, impl::FFT_BACKWARD, 39 | error_description); 40 | } 41 | 42 | template 43 | bool IFFT(TComplexArray2D & data, const size_t size1, const size_t size2, 44 | const char *& error_description) 45 | { 46 | return impl::CFFT::FFT_inplace(data, size1, size2, impl::FFT_BACKWARD, 47 | error_description); 48 | } 49 | 50 | template 51 | bool IFFT(TComplexArray3D & data, const size_t size1, const size_t size2, const size_t size3, 52 | const char *& error_description) 53 | { 54 | return impl::CFFT::FFT_inplace(data, size1, size2, size3, 55 | impl::FFT_BACKWARD, 56 | error_description); 57 | } 58 | 59 | // not-in-place, complex, forward 60 | template 61 | bool FFT(const TComplexArray1D & data_in, TComplexArray1D & data_out, 62 | const size_t size, const char *& error_description) 63 | { 64 | copy_array::copyArray(data_in, data_out, size); 65 | return impl::CFFT::FFT_inplace(data_out, size, impl::FFT_FORWARD, 66 | error_description); 67 | } 68 | 69 | template 70 | bool FFT(const TComplexArray2D & data_in, TComplexArray2D & data_out, 71 | const size_t size1, const size_t size2, const char *& error_description) 72 | { 73 | copy_array::copyArray(data_in, data_out, size1, size2); 74 | return impl::CFFT::FFT_inplace(data_out, size1, size2, 75 | impl::FFT_FORWARD, 76 | error_description); 77 | } 78 | 79 | template 80 | bool FFT(const TComplexArray3D & data_in, TComplexArray3D & data_out, 81 | const size_t size1, const size_t size2, const size_t size3, 82 | const char *& error_description) 83 | { 84 | copy_array::copyArray(data_in, data_out, size1, size2, size3); 85 | return impl::CFFT::FFT_inplace(data_out, size1, size2, size3, 86 | impl::FFT_FORWARD, 87 | error_description); 88 | } 89 | 90 | // not-in-place, complex, inverse 91 | template 92 | bool IFFT(const TComplexArray1D & data_in, TComplexArray1D & data_out, 93 | const size_t size, const char *& error_description) 94 | { 95 | copy_array::copyArray(data_in, data_out, size); 96 | return impl::CFFT::FFT_inplace(data_out, size, impl::FFT_BACKWARD, 97 | error_description); 98 | } 99 | 100 | template 101 | bool IFFT(const TComplexArray2D & data_in, TComplexArray2D & data_out, 102 | const size_t size1, const size_t size2, const char *& error_description) 103 | { 104 | copy_array::copyArray(data_in, data_out, size1, size2); 105 | return impl::CFFT::FFT_inplace(data_out, size1, size2, 106 | impl::FFT_BACKWARD, 107 | error_description); 108 | } 109 | 110 | template 111 | bool IFFT(const TComplexArray3D & data_in, TComplexArray3D & data_out, 112 | const size_t size1, const size_t size2, const size_t size3, 113 | const char *& error_description) 114 | { 115 | copy_array::copyArray(data_in, data_out, size1, size2, size3); 116 | return impl::CFFT::FFT_inplace(data_out, size1, size2, size3, 117 | impl::FFT_BACKWARD, 118 | error_description); 119 | } 120 | 121 | // not-in-place, real, forward 122 | template 123 | bool FFT(const TRealArray1D & data_in, TComplexArray1D & data_out, 124 | const size_t size, const char *& error_description) 125 | { 126 | copy_array::copyArray(data_in, data_out, size); 127 | return impl::CFFT::FFT_inplace(data_out, size, 128 | impl::FFT_FORWARD, 129 | error_description); 130 | } 131 | 132 | template 133 | bool FFT(const TRealArray2D & data_in, TComplexArray2D & data_out, 134 | const size_t size1, const size_t size2, const char *& error_description) 135 | { 136 | copy_array::copyArray(data_in, data_out, size1, size2); 137 | return impl::CFFT::FFT_inplace(data_out, size1, size2, 138 | impl::FFT_FORWARD, 139 | error_description); 140 | } 141 | 142 | template 143 | bool FFT(const TRealArray3D & data_in, TComplexArray3D & data_out, 144 | const size_t size1, const size_t size2, const size_t size3, 145 | const char *& error_description) 146 | { 147 | copy_array::copyArray(data_in, data_out, size1, size2, size3); 148 | return impl::CFFT::FFT_inplace(data_out, size1, size2, size3, 149 | impl::FFT_FORWARD, 150 | error_description); 151 | } 152 | 153 | } // simple_fft 154 | 155 | #endif // __SIMPLE_FFT__FFT_HPP__ 156 | -------------------------------------------------------------------------------- /simple_fft/fft_impl.hpp: -------------------------------------------------------------------------------- 1 | #ifndef __SIMPLE_FFT__FFT_IMPL_HPP__ 2 | #define __SIMPLE_FFT__FFT_IMPL_HPP__ 3 | 4 | #include "fft_settings.h" 5 | #include "error_handling.hpp" 6 | #include 7 | #include 8 | #include 9 | 10 | using std::size_t; 11 | 12 | #ifndef M_PI 13 | #define M_PI 3.1415926535897932 14 | #endif 15 | 16 | namespace simple_fft { 17 | namespace impl { 18 | 19 | enum FFT_direction 20 | { 21 | FFT_FORWARD = 0, 22 | FFT_BACKWARD 23 | }; 24 | 25 | // checking whether the size of array dimension is power of 2 26 | // via "complement and compare" method 27 | inline bool isPowerOfTwo(const size_t num) 28 | { 29 | if ((num == 0) || !(num & (~num + 1))) 30 | return false; 31 | 32 | return true; 33 | } 34 | 35 | inline bool checkNumElements(const size_t num_elements, const char *& error_description) 36 | { 37 | using namespace error_handling; 38 | 39 | if (!isPowerOfTwo(num_elements)) { 40 | GetErrorDescription(EC_ONE_OF_DIMS_ISNT_POWER_OF_TWO, error_description); 41 | return false; 42 | } 43 | 44 | return true; 45 | } 46 | 47 | template 48 | inline void scaleValues(TComplexArray1D & data, const size_t num_elements) 49 | { 50 | real_type mult = 1.0 / num_elements; 51 | int num_elements_signed = static_cast(num_elements); 52 | 53 | #ifndef __clang__ 54 | #ifdef __USE_OPENMP 55 | #pragma omp parallel for 56 | #endif 57 | #endif 58 | for(int i = 0; i < num_elements_signed; ++i) { 59 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 60 | data[i] *= mult; 61 | #else 62 | data(i) *= mult; 63 | #endif 64 | } 65 | } 66 | 67 | // NOTE: explicit template specialization for the case of std::vector 68 | // because it is used in 2D and 3D FFT for both array classes with square and round 69 | // brackets of element access operator; I need to guarantee that sub-FFT 1D will 70 | // use square brackets for element access operator anyway. It is pretty ugly 71 | // to duplicate the code but I haven't found more elegant solution. 72 | template <> 73 | inline void scaleValues >(std::vector & data, 74 | const size_t num_elements) 75 | { 76 | real_type mult = 1.0 / num_elements; 77 | int num_elements_signed = static_cast(num_elements); 78 | 79 | #ifndef __clang__ 80 | #ifdef __USE_OPENMP 81 | #pragma omp parallel for 82 | #endif 83 | #endif 84 | for(int i = 0; i < num_elements_signed; ++i) { 85 | data[i] *= mult; 86 | } 87 | } 88 | 89 | template 90 | inline void bufferExchangeHelper(TComplexArray1D & data, const size_t index_from, 91 | const size_t index_to, complex_type & buf) 92 | { 93 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 94 | buf = data[index_from]; 95 | data[index_from] = data[index_to]; 96 | data[index_to]= buf; 97 | #else 98 | buf = data(index_from); 99 | data(index_from) = data(index_to); 100 | data(index_to)= buf; 101 | #endif 102 | } 103 | 104 | // NOTE: explicit template specialization for the case of std::vector 105 | // because it is used in 2D and 3D FFT for both array classes with square and round 106 | // brackets of element access operator; I need to guarantee that sub-FFT 1D will 107 | // use square brackets for element access operator anyway. It is pretty ugly 108 | // to duplicate the code but I haven't found more elegant solution. 109 | template <> 110 | inline void bufferExchangeHelper >(std::vector & data, 111 | const size_t index_from, 112 | const size_t index_to, 113 | complex_type & buf) 114 | { 115 | buf = data[index_from]; 116 | data[index_from] = data[index_to]; 117 | data[index_to]= buf; 118 | } 119 | 120 | template 121 | void rearrangeData(TComplexArray1D & data, const size_t num_elements) 122 | { 123 | complex_type buf; 124 | 125 | size_t target_index = 0; 126 | size_t bit_mask; 127 | 128 | for (size_t i = 0; i < num_elements; ++i) 129 | { 130 | if (target_index > i) 131 | { 132 | bufferExchangeHelper(data, target_index, i, buf); 133 | } 134 | 135 | // Initialize the bit mask 136 | bit_mask = num_elements; 137 | 138 | // While bit is 1 139 | while (target_index & (bit_mask >>= 1)) // bit_mask = bit_mask >> 1 140 | { 141 | // Drop bit: 142 | // & is bitwise AND, 143 | // ~ is bitwise NOT 144 | target_index &= ~bit_mask; // target_index = target_index & (~bit_mask) 145 | } 146 | 147 | // | is bitwise OR 148 | target_index |= bit_mask; // target_index = target_index | bit_mask 149 | } 150 | } 151 | 152 | template 153 | inline void fftTransformHelper(TComplexArray1D & data, const size_t match, 154 | const size_t k, complex_type & product, 155 | const complex_type factor) 156 | { 157 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 158 | product = data[match] * factor; 159 | data[match] = data[k] - product; 160 | data[k] += product; 161 | #else 162 | product = data(match) * factor; 163 | data(match) = data(k) - product; 164 | data(k) += product; 165 | #endif 166 | } 167 | 168 | // NOTE: explicit template specialization for the case of std::vector 169 | // because it is used in 2D and 3D FFT for both array classes with square and round 170 | // brackets of element access operator; I need to guarantee that sub-FFT 1D will 171 | // use square brackets for element access operator anyway. It is pretty ugly 172 | // to duplicate the code but I haven't found more elegant solution. 173 | template <> 174 | inline void fftTransformHelper >(std::vector & data, 175 | const size_t match, 176 | const size_t k, 177 | complex_type & product, 178 | const complex_type factor) 179 | { 180 | product = data[match] * factor; 181 | data[match] = data[k] - product; 182 | data[k] += product; 183 | } 184 | 185 | template 186 | bool makeTransform(TComplexArray1D & data, const size_t num_elements, 187 | const FFT_direction fft_direction, const char *& error_description) 188 | { 189 | using namespace error_handling; 190 | using std::sin; 191 | 192 | double local_pi; 193 | switch(fft_direction) 194 | { 195 | case(FFT_FORWARD): 196 | local_pi = -M_PI; 197 | break; 198 | case(FFT_BACKWARD): 199 | local_pi = M_PI; 200 | break; 201 | default: 202 | GetErrorDescription(EC_WRONG_FFT_DIRECTION, error_description); 203 | return false; 204 | } 205 | 206 | // declare variables to cycle the bits of initial signal 207 | size_t next, match; 208 | real_type sine; 209 | real_type delta; 210 | complex_type mult, factor, product; 211 | 212 | // NOTE: user's complex type should have constructor like 213 | // "complex(real, imag)", where each of real and imag has 214 | // real type. 215 | 216 | // cycle for all bit positions of initial signal 217 | for (size_t i = 1; i < num_elements; i <<= 1) 218 | { 219 | next = i << 1; // getting the next bit 220 | delta = local_pi / i; // angle increasing 221 | sine = sin(0.5 * delta); // supplementary sin 222 | // multiplier for trigonometric recurrence 223 | mult = complex_type(-2.0 * sine * sine, sin(delta)); 224 | factor = 1.0; // start transform factor 225 | 226 | for (size_t j = 0; j < i; ++j) // iterations through groups 227 | // with different transform factors 228 | { 229 | for (size_t k = j; k < num_elements; k += next) // iterations through 230 | // pairs within group 231 | { 232 | match = k + i; 233 | fftTransformHelper(data, match, k, product, factor); 234 | } 235 | factor = mult * factor + factor; 236 | } 237 | } 238 | 239 | return true; 240 | } 241 | 242 | // Generic template for complex FFT followed by its explicit specializations 243 | template 244 | struct CFFT 245 | {}; 246 | 247 | // 1D FFT: 248 | template 249 | struct CFFT 250 | { 251 | // NOTE: passing by pointer is needed to avoid using element access operator 252 | static bool FFT_inplace(TComplexArray1D & data, const size_t size, 253 | const FFT_direction fft_direction, 254 | const char *& error_description) 255 | { 256 | if(!checkNumElements(size, error_description)) { 257 | return false; 258 | } 259 | 260 | rearrangeData(data, size); 261 | 262 | if(!makeTransform(data, size, fft_direction, error_description)) { 263 | return false; 264 | } 265 | 266 | if (FFT_BACKWARD == fft_direction) { 267 | scaleValues(data, size); 268 | } 269 | 270 | return true; 271 | } 272 | }; 273 | 274 | // 2D FFT 275 | template 276 | struct CFFT 277 | { 278 | static bool FFT_inplace(TComplexArray2D & data, const size_t size1, const size_t size2, 279 | const FFT_direction fft_direction, const char *& error_description) 280 | { 281 | int n_rows = static_cast(size1); 282 | int n_cols = static_cast(size2); 283 | 284 | // fft for columns 285 | std::vector subarray(n_rows); // each column has n_rows elements 286 | 287 | for(int j = 0; j < n_cols; ++j) 288 | { 289 | #ifndef __clang__ 290 | #ifdef __USE_OPENMP 291 | #pragma omp parallel for 292 | #endif 293 | #endif 294 | for(int i = 0; i < n_rows; ++i) { 295 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 296 | subarray[i] = data[i][j]; 297 | #else 298 | subarray[i] = data(i,j); 299 | #endif 300 | } 301 | 302 | if(!CFFT,1>::FFT_inplace(subarray, size1, 303 | fft_direction, 304 | error_description)) 305 | { 306 | return false; 307 | } 308 | 309 | #ifndef __clang__ 310 | #ifdef __USE_OPENMP 311 | #pragma omp parallel for 312 | #endif 313 | #endif 314 | for(int i = 0; i < n_rows; ++i) { 315 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 316 | data[i][j] = subarray[i]; 317 | #else 318 | data(i,j) = subarray[i]; 319 | #endif 320 | } 321 | } 322 | 323 | // fft for rows 324 | subarray.resize(n_cols); // each row has n_cols elements 325 | 326 | for(int i = 0; i < n_rows; ++i) 327 | { 328 | #ifndef __clang__ 329 | #ifdef __USE_OPENMP 330 | #pragma omp parallel for 331 | #endif 332 | #endif 333 | for(int j = 0; j < n_cols; ++j) { 334 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 335 | subarray[j] = data[i][j]; 336 | #else 337 | subarray[j] = data(i,j); 338 | #endif 339 | } 340 | 341 | if(!CFFT,1>::FFT_inplace(subarray, size2, 342 | fft_direction, 343 | error_description)) 344 | { 345 | return false; 346 | } 347 | 348 | #ifndef __clang__ 349 | #ifdef __USE_OPENMP 350 | #pragma omp parallel for 351 | #endif 352 | #endif 353 | for(int j = 0; j < n_cols; ++j) { 354 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 355 | data[i][j] = subarray[j]; 356 | #else 357 | data(i,j) = subarray[j]; 358 | #endif 359 | } 360 | } 361 | 362 | return true; 363 | } 364 | }; 365 | 366 | // 3D FFT 367 | template 368 | struct CFFT 369 | { 370 | static bool FFT_inplace(TComplexArray3D & data, const size_t size1, const size_t size2, 371 | const size_t size3, const FFT_direction fft_direction, 372 | const char *& error_description) 373 | { 374 | int n_rows = static_cast(size1); 375 | int n_cols = static_cast(size2); 376 | int n_depth = static_cast(size3); 377 | 378 | std::vector subarray(n_rows); // for fft for columns: each column has n_rows elements 379 | 380 | for(int k = 0; k < n_depth; ++k) // for all depth layers 381 | { 382 | // fft for columns 383 | for(int j = 0; j < n_cols; ++j) 384 | { 385 | #ifndef __clang__ 386 | #ifdef __USE_OPENMP 387 | #pragma omp parallel for 388 | #endif 389 | #endif 390 | for(int i = 0; i < n_rows; ++i) { 391 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 392 | subarray[i] = data[i][j][k]; 393 | #else 394 | subarray[i] = data(i,j,k); 395 | #endif 396 | } 397 | 398 | if(!CFFT,1>::FFT_inplace(subarray, size1, 399 | fft_direction, 400 | error_description)) 401 | { 402 | return false; 403 | } 404 | 405 | #ifndef __clang__ 406 | #ifdef __USE_OPENMP 407 | #pragma omp parallel for 408 | #endif 409 | #endif 410 | for(int i = 0; i < n_rows; ++i) { 411 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 412 | data[i][j][k] = subarray[i]; 413 | #else 414 | data(i,j,k) = subarray[i]; 415 | #endif 416 | } 417 | } 418 | } 419 | 420 | subarray.resize(n_cols); // for fft for rows: each row has n_cols elements 421 | 422 | for(int k = 0; k < n_depth; ++k) // for all depth layers 423 | { 424 | // fft for rows 425 | for(int i = 0; i < n_rows; ++i) 426 | { 427 | #ifndef __clang__ 428 | #ifdef __USE_OPENMP 429 | #pragma omp parallel for 430 | #endif 431 | #endif 432 | for(int j = 0; j < n_cols; ++j) { 433 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 434 | subarray[j] = data[i][j][k]; 435 | #else 436 | subarray[j] = data(i,j,k); 437 | #endif 438 | } 439 | 440 | if(!CFFT,1>::FFT_inplace(subarray, size2, 441 | fft_direction, 442 | error_description)) 443 | { 444 | return false; 445 | } 446 | 447 | #ifndef __clang__ 448 | #ifdef __USE_OPENMP 449 | #pragma omp parallel for 450 | #endif 451 | #endif 452 | for(int j = 0; j < n_cols; ++j) { 453 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 454 | data[i][j][k] = subarray[j]; 455 | #else 456 | data(i,j,k) = subarray[j]; 457 | #endif 458 | } 459 | } 460 | } 461 | 462 | // fft for depth 463 | subarray.resize(n_depth); // each depth strip contains n_depth elements 464 | 465 | for(int i = 0; i < n_rows; ++i) // for all rows layers 466 | { 467 | for(int j = 0; j < n_cols; ++j) // for all cols layers 468 | { 469 | #ifndef __clang__ 470 | #ifdef __USE_OPENMP 471 | #pragma omp parallel for 472 | #endif 473 | #endif 474 | for(int k = 0; k < n_depth; ++k) { 475 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 476 | subarray[k] = data[i][j][k]; 477 | #else 478 | subarray[k] = data(i,j,k); 479 | #endif 480 | } 481 | 482 | if(!CFFT,1>::FFT_inplace(subarray, size3, 483 | fft_direction, 484 | error_description)) 485 | { 486 | return false; 487 | } 488 | 489 | #ifndef __clang__ 490 | #ifdef __USE_OPENMP 491 | #pragma omp parallel for 492 | #endif 493 | #endif 494 | for(int k = 0; k < n_depth; ++k) { 495 | #ifdef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 496 | data[i][j][k] = subarray[k]; 497 | #else 498 | data(i,j,k) = subarray[k]; 499 | #endif 500 | } 501 | } 502 | } 503 | 504 | return true; 505 | } 506 | }; 507 | 508 | } // namespace impl 509 | } // namespace simple_fft 510 | 511 | #endif // __SIMPLE_FFT__FFT_IMPL_HPP__ 512 | -------------------------------------------------------------------------------- /simple_fft/fft_settings.h: -------------------------------------------------------------------------------- 1 | // In this file you can alter some settings of the library: 2 | // 1) Specify the desired real and complex types by typedef'ing real_type and complex_type. 3 | // By default real_type is double and complex_type is std::complex. 4 | // 2) If the array class uses square brackets for element access operator, define 5 | // the macro __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 6 | 7 | #ifndef __SIMPLE_FFT__FFT_SETTINGS_H__ 8 | #define __SIMPLE_FFT__FFT_SETTINGS_H__ 9 | 10 | #include 11 | 12 | typedef double real_type; 13 | typedef std::complex complex_type; 14 | 15 | //#ifndef __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 16 | //#define __USE_SQUARE_BRACKETS_FOR_ELEMENT_ACCESS_OPERATOR 17 | //#endif 18 | 19 | #endif // __SIMPLE_FFT__FFT_SETTINGS_H__ 20 | -------------------------------------------------------------------------------- /stb/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | stb 4 | === 5 | 6 | single-file public domain (or MIT licensed) libraries for C/C++ 7 | 8 | Most libraries by stb, except: stb_dxt by Fabian "ryg" Giesen, stb_image_resize 9 | by Jorge L. "VinoBS" Rodriguez, and stb_sprintf by Jeff Roberts. 10 | 11 | 12 | library | lastest version | category | LoC | description 13 | --------------------- | ---- | -------- | --- | -------------------------------- 14 | **[stb_vorbis.c](stb_vorbis.c)** | 1.15 | audio | 5479 | decode ogg vorbis files from file/memory to float/16-bit signed output 15 | **[stb_image.h](stb_image.h)** | 2.20 | graphics | 7529 | image loading/decoding from file/memory: JPG, PNG, TGA, BMP, PSD, GIF, HDR, PIC 16 | **[stb_truetype.h](stb_truetype.h)** | 1.20 | graphics | 4894 | parse, decode, and rasterize characters from truetype fonts 17 | **[stb_image_write.h](stb_image_write.h)** | 1.10 | graphics | 1625 | image writing to disk: PNG, TGA, BMP 18 | **[stb_image_resize.h](stb_image_resize.h)** | 0.95 | graphics | 2627 | resize images larger/smaller with good quality 19 | **[stb_rect_pack.h](stb_rect_pack.h)** | 0.99 | graphics | 619 | simple 2D rectangle packer with decent quality 20 | **[stb_sprintf.h](stb_sprintf.h)** | 1.06 | utility | 1860 | fast sprintf, snprintf for C/C++ 21 | **[stretchy_buffer.h](stretchy_buffer.h)** | 1.03 | utility | 262 | typesafe dynamic array for C (i.e. approximation to vector<>), doesn't compile as C++ 22 | **[stb_textedit.h](stb_textedit.h)** | 1.13 | user interface | 1404 | guts of a text editor for games etc implementing them from scratch 23 | **[stb_voxel_render.h](stb_voxel_render.h)** | 0.86 | 3D graphics | 3804 | Minecraft-esque voxel rendering "engine" with many more features 24 | **[stb_dxt.h](stb_dxt.h)** | 1.08b | 3D graphics | 728 | Fabian "ryg" Giesen's real-time DXT compressor 25 | **[stb_perlin.h](stb_perlin.h)** | 0.4 | 3D graphics | 366 | revised Perlin noise (3D input, 1D output) 26 | **[stb_easy_font.h](stb_easy_font.h)** | 1.0 | 3D graphics | 303 | quick-and-dirty easy-to-deploy bitmap font for printing frame rate, etc 27 | **[stb_tilemap_editor.h](stb_tilemap_editor.h)** | 0.39 | game dev | 4174 | embeddable tilemap editor 28 | **[stb_herringbone_wa...](stb_herringbone_wang_tile.h)** | 0.6 | game dev | 1220 | herringbone Wang tile map generator 29 | **[stb_c_lexer.h](stb_c_lexer.h)** | 0.09 | parsing | 962 | simplify writing parsers for C-like languages 30 | **[stb_divide.h](stb_divide.h)** | 0.91 | math | 419 | more useful 32-bit modulus e.g. "euclidean divide" 31 | **[stb_connected_comp...](stb_connected_components.h)** | 0.95 | misc | 1045 | incrementally compute reachability on grids 32 | **[stb.h](stb.h)** | 2.32 | misc | 14469 | helper functions for C, mostly redundant in C++; basically author's personal stuff 33 | **[stb_leakcheck.h](stb_leakcheck.h)** | 0.5 | misc | 190 | quick-and-dirty malloc/free leak-checking 34 | 35 | Total libraries: 20 36 | Total lines of C code: 53979 37 | 38 | 39 | FAQ 40 | --- 41 | 42 | #### What's the license? 43 | 44 | These libraries are in the public domain. You can do anything you 45 | want with them. You have no legal obligation 46 | to do anything else, although I appreciate attribution. 47 | 48 | They are also licensed under the MIT open source license, if you have lawyers 49 | who are unhappy with public domain. Every source file includes an explicit 50 | dual-license for you to choose from. 51 | 52 | #### Are there other single-file public-domain/open source libraries with minimal dependencies out there? 53 | 54 | [Yes.](https://github.com/nothings/single_file_libs) 55 | 56 | #### If I wrap an stb library in a new library, does the new library have to be public domain/MIT? 57 | 58 | No, because it's public domain you can freely relicense it to whatever license your new 59 | library wants to be. 60 | 61 | #### What's the deal with SSE support in GCC-based compilers? 62 | 63 | stb_image will either use SSE2 (if you compile with -msse2) or 64 | will not use any SIMD at all, rather than trying to detect the 65 | processor at runtime and handle it correctly. As I understand it, 66 | the approved path in GCC for runtime-detection require 67 | you to use multiple source files, one for each CPU configuration. 68 | Because stb_image is a header-file library that compiles in only 69 | one source file, there's no approved way to build both an 70 | SSE-enabled and a non-SSE-enabled variation. 71 | 72 | While we've tried to work around it, we've had multiple issues over 73 | the years due to specific versions of gcc breaking what we're doing, 74 | so we've given up on it. See https://github.com/nothings/stb/issues/280 75 | and https://github.com/nothings/stb/issues/410 for examples. 76 | 77 | #### Some of these libraries seem redundant to existing open source libraries. Are they better somehow? 78 | 79 | Generally they're only better in that they're easier to integrate, 80 | easier to use, and easier to release (single file; good API; no 81 | attribution requirement). They may be less featureful, slower, 82 | and/or use more memory. If you're already using an equivalent 83 | library, there's probably no good reason to switch. 84 | 85 | #### Can I link directly to the table of stb libraries? 86 | 87 | You can use [this URL](https://github.com/nothings/stb#stb_libs) to link directly to that list. 88 | 89 | #### Why do you list "lines of code"? It's a terrible metric. 90 | 91 | Just to give you some idea of the internal complexity of the library, 92 | to help you manage your expectations, or to let you know what you're 93 | getting into. While not all the libraries are written in the same 94 | style, they're certainly similar styles, and so comparisons between 95 | the libraries are probably still meaningful. 96 | 97 | Note though that the lines do include both the implementation, the 98 | part that corresponds to a header file, and the documentation. 99 | 100 | #### Why single-file headers? 101 | 102 | Windows doesn't have standard directories where libraries 103 | live. That makes deploying libraries in Windows a lot more 104 | painful than open source developers on Unix-derivates generally 105 | realize. (It also makes library dependencies a lot worse in Windows.) 106 | 107 | There's also a common problem in Windows where a library was built 108 | against a different version of the runtime library, which causes 109 | link conflicts and confusion. Shipping the libs as headers means 110 | you normally just compile them straight into your project without 111 | making libraries, thus sidestepping that problem. 112 | 113 | Making them a single file makes it very easy to just 114 | drop them into a project that needs them. (Of course you can 115 | still put them in a proper shared library tree if you want.) 116 | 117 | Why not two files, one a header and one an implementation? 118 | The difference between 10 files and 9 files is not a big deal, 119 | but the difference between 2 files and 1 file is a big deal. 120 | You don't need to zip or tar the files up, you don't have to 121 | remember to attach *two* files, etc. 122 | 123 | #### Why "stb"? Is this something to do with Set-Top Boxes? 124 | 125 | No, they are just the initials for my name, Sean T. Barrett. 126 | This was not chosen out of egomania, but as a moderately sane 127 | way of namespacing the filenames and source function names. 128 | 129 | #### Will you add more image types to stb_image.h? 130 | 131 | If people submit them, I generally add them, but the goal of stb_image 132 | is less for applications like image viewer apps (which need to support 133 | every type of image under the sun) and more for things like games which 134 | can choose what images to use, so I may decline to add them if they're 135 | too rare or if the size of implementation vs. apparent benefit is too low. 136 | 137 | #### Do you have any advice on how to create my own single-file library? 138 | 139 | Yes. https://github.com/nothings/stb/blob/master/docs/stb_howto.txt 140 | 141 | #### Why public domain? 142 | 143 | I prefer it over GPL, LGPL, BSD, zlib, etc. for many reasons. 144 | Some of them are listed here: 145 | https://github.com/nothings/stb/blob/master/docs/why_public_domain.md 146 | 147 | #### Why C? 148 | 149 | Primarily, because I use C, not C++. But it does also make it easier 150 | for other people to use them from other languages. 151 | 152 | #### Why not C99? stdint.h, declare-anywhere, etc. 153 | 154 | I still use MSVC 6 (1998) as my IDE because it has better human factors 155 | for me than later versions of MSVC. 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /stb/stb_image_write.h: -------------------------------------------------------------------------------- 1 | /* stb_image_write - v1.10 - public domain - http://nothings.org/stb/stb_image_write.h 2 | writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015 3 | no warranty implied; use at your own risk 4 | 5 | Before #including, 6 | 7 | #define STB_IMAGE_WRITE_IMPLEMENTATION 8 | 9 | in the file that you want to have the implementation. 10 | 11 | Will probably not work correctly with strict-aliasing optimizations. 12 | 13 | If using a modern Microsoft Compiler, non-safe versions of CRT calls may cause 14 | compilation warnings or even errors. To avoid this, also before #including, 15 | 16 | #define STBI_MSC_SECURE_CRT 17 | 18 | ABOUT: 19 | 20 | This header file is a library for writing images to C stdio or a callback. 21 | 22 | The PNG output is not optimal; it is 20-50% larger than the file 23 | written by a decent optimizing implementation; though providing a custom 24 | zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that. 25 | This library is designed for source code compactness and simplicity, 26 | not optimal image file size or run-time performance. 27 | 28 | BUILDING: 29 | 30 | You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h. 31 | You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace 32 | malloc,realloc,free. 33 | You can #define STBIW_MEMMOVE() to replace memmove() 34 | You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function 35 | for PNG compression (instead of the builtin one), it must have the following signature: 36 | unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality); 37 | The returned data will be freed with STBIW_FREE() (free() by default), 38 | so it must be heap allocated with STBIW_MALLOC() (malloc() by default), 39 | 40 | UNICODE: 41 | 42 | If compiling for Windows and you wish to use Unicode filenames, compile 43 | with 44 | #define STBIW_WINDOWS_UTF8 45 | and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert 46 | Windows wchar_t filenames to utf8. 47 | 48 | USAGE: 49 | 50 | There are five functions, one for each image file format: 51 | 52 | int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 53 | int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 54 | int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 55 | int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality); 56 | int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 57 | 58 | void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically 59 | 60 | There are also five equivalent functions that use an arbitrary write function. You are 61 | expected to open/close your file-equivalent before and after calling these: 62 | 63 | int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 64 | int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 65 | int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 66 | int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 67 | int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 68 | 69 | where the callback is: 70 | void stbi_write_func(void *context, void *data, int size); 71 | 72 | You can configure it with these global variables: 73 | int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE 74 | int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression 75 | int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode 76 | 77 | 78 | You can define STBI_WRITE_NO_STDIO to disable the file variant of these 79 | functions, so the library will not use stdio.h at all. However, this will 80 | also disable HDR writing, because it requires stdio for formatted output. 81 | 82 | Each function returns 0 on failure and non-0 on success. 83 | 84 | The functions create an image file defined by the parameters. The image 85 | is a rectangle of pixels stored from left-to-right, top-to-bottom. 86 | Each pixel contains 'comp' channels of data stored interleaved with 8-bits 87 | per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is 88 | monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall. 89 | The *data pointer points to the first byte of the top-left-most pixel. 90 | For PNG, "stride_in_bytes" is the distance in bytes from the first byte of 91 | a row of pixels to the first byte of the next row of pixels. 92 | 93 | PNG creates output files with the same number of components as the input. 94 | The BMP format expands Y to RGB in the file format and does not 95 | output alpha. 96 | 97 | PNG supports writing rectangles of data even when the bytes storing rows of 98 | data are not consecutive in memory (e.g. sub-rectangles of a larger image), 99 | by supplying the stride between the beginning of adjacent rows. The other 100 | formats do not. (Thus you cannot write a native-format BMP through the BMP 101 | writer, both because it is in BGR order and because it may have padding 102 | at the end of the line.) 103 | 104 | PNG allows you to set the deflate compression level by setting the global 105 | variable 'stbi_write_png_compression_level' (it defaults to 8). 106 | 107 | HDR expects linear float data. Since the format is always 32-bit rgb(e) 108 | data, alpha (if provided) is discarded, and for monochrome data it is 109 | replicated across all three channels. 110 | 111 | TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed 112 | data, set the global variable 'stbi_write_tga_with_rle' to 0. 113 | 114 | JPEG does ignore alpha channels in input data; quality is between 1 and 100. 115 | Higher quality looks better but results in a bigger image. 116 | JPEG baseline (no JPEG progressive). 117 | 118 | CREDITS: 119 | 120 | 121 | Sean Barrett - PNG/BMP/TGA 122 | Baldur Karlsson - HDR 123 | Jean-Sebastien Guay - TGA monochrome 124 | Tim Kelsey - misc enhancements 125 | Alan Hickman - TGA RLE 126 | Emmanuel Julien - initial file IO callback implementation 127 | Jon Olick - original jo_jpeg.cpp code 128 | Daniel Gibson - integrate JPEG, allow external zlib 129 | Aarni Koskela - allow choosing PNG filter 130 | 131 | bugfixes: 132 | github:Chribba 133 | Guillaume Chereau 134 | github:jry2 135 | github:romigrou 136 | Sergio Gonzalez 137 | Jonas Karlsson 138 | Filip Wasil 139 | Thatcher Ulrich 140 | github:poppolopoppo 141 | Patrick Boettcher 142 | github:xeekworx 143 | Cap Petschulat 144 | Simon Rodriguez 145 | Ivan Tikhonov 146 | github:ignotion 147 | Adam Schackart 148 | 149 | LICENSE 150 | 151 | See end of file for license information. 152 | 153 | */ 154 | 155 | #ifndef INCLUDE_STB_IMAGE_WRITE_H 156 | #define INCLUDE_STB_IMAGE_WRITE_H 157 | 158 | #include 159 | 160 | // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline' 161 | #ifndef STBIWDEF 162 | #ifdef STB_IMAGE_WRITE_STATIC 163 | #define STBIWDEF static 164 | #else 165 | #ifdef __cplusplus 166 | #define STBIWDEF extern "C" 167 | #else 168 | #define STBIWDEF extern 169 | #endif 170 | #endif 171 | #endif 172 | 173 | #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations 174 | extern int stbi_write_tga_with_rle; 175 | extern int stbi_write_png_compression_level; 176 | extern int stbi_write_force_png_filter; 177 | #endif 178 | 179 | #ifndef STBI_WRITE_NO_STDIO 180 | STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 181 | STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 182 | STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 183 | STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 184 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); 185 | 186 | #ifdef STBI_WINDOWS_UTF8 187 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); 188 | #endif 189 | #endif 190 | 191 | typedef void stbi_write_func(void *context, void *data, int size); 192 | 193 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 194 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 195 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 196 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 197 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 198 | 199 | STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean); 200 | 201 | #endif//INCLUDE_STB_IMAGE_WRITE_H 202 | 203 | #ifdef STB_IMAGE_WRITE_IMPLEMENTATION 204 | 205 | #ifdef _WIN32 206 | #ifndef _CRT_SECURE_NO_WARNINGS 207 | #define _CRT_SECURE_NO_WARNINGS 208 | #endif 209 | #ifndef _CRT_NONSTDC_NO_DEPRECATE 210 | #define _CRT_NONSTDC_NO_DEPRECATE 211 | #endif 212 | #endif 213 | 214 | #ifndef STBI_WRITE_NO_STDIO 215 | #include 216 | #endif // STBI_WRITE_NO_STDIO 217 | 218 | #include 219 | #include 220 | #include 221 | #include 222 | 223 | #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED)) 224 | // ok 225 | #elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED) 226 | // ok 227 | #else 228 | #error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)." 229 | #endif 230 | 231 | #ifndef STBIW_MALLOC 232 | #define STBIW_MALLOC(sz) malloc(sz) 233 | #define STBIW_REALLOC(p,newsz) realloc(p,newsz) 234 | #define STBIW_FREE(p) free(p) 235 | #endif 236 | 237 | #ifndef STBIW_REALLOC_SIZED 238 | #define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz) 239 | #endif 240 | 241 | 242 | #ifndef STBIW_MEMMOVE 243 | #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) 244 | #endif 245 | 246 | 247 | #ifndef STBIW_ASSERT 248 | #include 249 | #define STBIW_ASSERT(x) assert(x) 250 | #endif 251 | 252 | #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) 253 | 254 | #ifdef STB_IMAGE_WRITE_STATIC 255 | static int stbi__flip_vertically_on_write=0; 256 | static int stbi_write_png_compression_level = 8; 257 | static int stbi_write_tga_with_rle = 1; 258 | static int stbi_write_force_png_filter = -1; 259 | #else 260 | int stbi_write_png_compression_level = 8; 261 | int stbi__flip_vertically_on_write=0; 262 | int stbi_write_tga_with_rle = 1; 263 | int stbi_write_force_png_filter = -1; 264 | #endif 265 | 266 | STBIWDEF void stbi_flip_vertically_on_write(int flag) 267 | { 268 | stbi__flip_vertically_on_write = flag; 269 | } 270 | 271 | typedef struct 272 | { 273 | stbi_write_func *func; 274 | void *context; 275 | } stbi__write_context; 276 | 277 | // initialize a callback-based context 278 | static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context) 279 | { 280 | s->func = c; 281 | s->context = context; 282 | } 283 | 284 | #ifndef STBI_WRITE_NO_STDIO 285 | 286 | static void stbi__stdio_write(void *context, void *data, int size) 287 | { 288 | fwrite(data,1,size,(FILE*) context); 289 | } 290 | 291 | #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 292 | #ifdef __cplusplus 293 | #define STBIW_EXTERN extern "C" 294 | #else 295 | #define STBIW_EXTERN extern 296 | #endif 297 | STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); 298 | STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); 299 | 300 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) 301 | { 302 | return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, bufferlen, NULL, NULL); 303 | } 304 | #endif 305 | 306 | static FILE *stbiw__fopen(char const *filename, char const *mode) 307 | { 308 | FILE *f; 309 | #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 310 | wchar_t wMode[64]; 311 | wchar_t wFilename[1024]; 312 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) 313 | return 0; 314 | 315 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) 316 | return 0; 317 | 318 | #if _MSC_VER >= 1400 319 | if (0 != _wfopen_s(&f, wFilename, wMode)) 320 | f = 0; 321 | #else 322 | f = _wfopen(wFilename, wMode); 323 | #endif 324 | 325 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 326 | if (0 != fopen_s(&f, filename, mode)) 327 | f=0; 328 | #else 329 | f = fopen(filename, mode); 330 | #endif 331 | return f; 332 | } 333 | 334 | static int stbi__start_write_file(stbi__write_context *s, const char *filename) 335 | { 336 | FILE *f = stbiw__fopen(filename, "wb"); 337 | stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); 338 | return f != NULL; 339 | } 340 | 341 | static void stbi__end_write_file(stbi__write_context *s) 342 | { 343 | fclose((FILE *)s->context); 344 | } 345 | 346 | #endif // !STBI_WRITE_NO_STDIO 347 | 348 | typedef unsigned int stbiw_uint32; 349 | typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; 350 | 351 | static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) 352 | { 353 | while (*fmt) { 354 | switch (*fmt++) { 355 | case ' ': break; 356 | case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int)); 357 | s->func(s->context,&x,1); 358 | break; } 359 | case '2': { int x = va_arg(v,int); 360 | unsigned char b[2]; 361 | b[0] = STBIW_UCHAR(x); 362 | b[1] = STBIW_UCHAR(x>>8); 363 | s->func(s->context,b,2); 364 | break; } 365 | case '4': { stbiw_uint32 x = va_arg(v,int); 366 | unsigned char b[4]; 367 | b[0]=STBIW_UCHAR(x); 368 | b[1]=STBIW_UCHAR(x>>8); 369 | b[2]=STBIW_UCHAR(x>>16); 370 | b[3]=STBIW_UCHAR(x>>24); 371 | s->func(s->context,b,4); 372 | break; } 373 | default: 374 | STBIW_ASSERT(0); 375 | return; 376 | } 377 | } 378 | } 379 | 380 | static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) 381 | { 382 | va_list v; 383 | va_start(v, fmt); 384 | stbiw__writefv(s, fmt, v); 385 | va_end(v); 386 | } 387 | 388 | static void stbiw__putc(stbi__write_context *s, unsigned char c) 389 | { 390 | s->func(s->context, &c, 1); 391 | } 392 | 393 | static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) 394 | { 395 | unsigned char arr[3]; 396 | arr[0] = a, arr[1] = b, arr[2] = c; 397 | s->func(s->context, arr, 3); 398 | } 399 | 400 | static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) 401 | { 402 | unsigned char bg[3] = { 255, 0, 255}, px[3]; 403 | int k; 404 | 405 | if (write_alpha < 0) 406 | s->func(s->context, &d[comp - 1], 1); 407 | 408 | switch (comp) { 409 | case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case 410 | case 1: 411 | if (expand_mono) 412 | stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp 413 | else 414 | s->func(s->context, d, 1); // monochrome TGA 415 | break; 416 | case 4: 417 | if (!write_alpha) { 418 | // composite against pink background 419 | for (k = 0; k < 3; ++k) 420 | px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255; 421 | stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); 422 | break; 423 | } 424 | /* FALLTHROUGH */ 425 | case 3: 426 | stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); 427 | break; 428 | } 429 | if (write_alpha > 0) 430 | s->func(s->context, &d[comp - 1], 1); 431 | } 432 | 433 | static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) 434 | { 435 | stbiw_uint32 zero = 0; 436 | int i,j, j_end; 437 | 438 | if (y <= 0) 439 | return; 440 | 441 | if (stbi__flip_vertically_on_write) 442 | vdir *= -1; 443 | 444 | if (vdir < 0) 445 | j_end = -1, j = y-1; 446 | else 447 | j_end = y, j = 0; 448 | 449 | for (; j != j_end; j += vdir) { 450 | for (i=0; i < x; ++i) { 451 | unsigned char *d = (unsigned char *) data + (j*x+i)*comp; 452 | stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 453 | } 454 | s->func(s->context, &zero, scanline_pad); 455 | } 456 | } 457 | 458 | static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...) 459 | { 460 | if (y < 0 || x < 0) { 461 | return 0; 462 | } else { 463 | va_list v; 464 | va_start(v, fmt); 465 | stbiw__writefv(s, fmt, v); 466 | va_end(v); 467 | stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono); 468 | return 1; 469 | } 470 | } 471 | 472 | static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) 473 | { 474 | int pad = (-x*3) & 3; 475 | return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 476 | "11 4 22 4" "4 44 22 444444", 477 | 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 478 | 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 479 | } 480 | 481 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 482 | { 483 | stbi__write_context s; 484 | stbi__start_write_callbacks(&s, func, context); 485 | return stbi_write_bmp_core(&s, x, y, comp, data); 486 | } 487 | 488 | #ifndef STBI_WRITE_NO_STDIO 489 | STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) 490 | { 491 | stbi__write_context s; 492 | if (stbi__start_write_file(&s,filename)) { 493 | int r = stbi_write_bmp_core(&s, x, y, comp, data); 494 | stbi__end_write_file(&s); 495 | return r; 496 | } else 497 | return 0; 498 | } 499 | #endif //!STBI_WRITE_NO_STDIO 500 | 501 | static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) 502 | { 503 | int has_alpha = (comp == 2 || comp == 4); 504 | int colorbytes = has_alpha ? comp-1 : comp; 505 | int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 506 | 507 | if (y < 0 || x < 0) 508 | return 0; 509 | 510 | if (!stbi_write_tga_with_rle) { 511 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, 512 | "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); 513 | } else { 514 | int i,j,k; 515 | int jend, jdir; 516 | 517 | stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); 518 | 519 | if (stbi__flip_vertically_on_write) { 520 | j = 0; 521 | jend = y; 522 | jdir = 1; 523 | } else { 524 | j = y-1; 525 | jend = -1; 526 | jdir = -1; 527 | } 528 | for (; j != jend; j += jdir) { 529 | unsigned char *row = (unsigned char *) data + j * x * comp; 530 | int len; 531 | 532 | for (i = 0; i < x; i += len) { 533 | unsigned char *begin = row + i * comp; 534 | int diff = 1; 535 | len = 1; 536 | 537 | if (i < x - 1) { 538 | ++len; 539 | diff = memcmp(begin, row + (i + 1) * comp, comp); 540 | if (diff) { 541 | const unsigned char *prev = begin; 542 | for (k = i + 2; k < x && len < 128; ++k) { 543 | if (memcmp(prev, row + k * comp, comp)) { 544 | prev += comp; 545 | ++len; 546 | } else { 547 | --len; 548 | break; 549 | } 550 | } 551 | } else { 552 | for (k = i + 2; k < x && len < 128; ++k) { 553 | if (!memcmp(begin, row + k * comp, comp)) { 554 | ++len; 555 | } else { 556 | break; 557 | } 558 | } 559 | } 560 | } 561 | 562 | if (diff) { 563 | unsigned char header = STBIW_UCHAR(len - 1); 564 | s->func(s->context, &header, 1); 565 | for (k = 0; k < len; ++k) { 566 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 567 | } 568 | } else { 569 | unsigned char header = STBIW_UCHAR(len - 129); 570 | s->func(s->context, &header, 1); 571 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 572 | } 573 | } 574 | } 575 | } 576 | return 1; 577 | } 578 | 579 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 580 | { 581 | stbi__write_context s; 582 | stbi__start_write_callbacks(&s, func, context); 583 | return stbi_write_tga_core(&s, x, y, comp, (void *) data); 584 | } 585 | 586 | #ifndef STBI_WRITE_NO_STDIO 587 | STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) 588 | { 589 | stbi__write_context s; 590 | if (stbi__start_write_file(&s,filename)) { 591 | int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); 592 | stbi__end_write_file(&s); 593 | return r; 594 | } else 595 | return 0; 596 | } 597 | #endif 598 | 599 | // ************************************************************************************************* 600 | // Radiance RGBE HDR writer 601 | // by Baldur Karlsson 602 | 603 | #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) 604 | 605 | static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) 606 | { 607 | int exponent; 608 | float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); 609 | 610 | if (maxcomp < 1e-32f) { 611 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; 612 | } else { 613 | float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; 614 | 615 | rgbe[0] = (unsigned char)(linear[0] * normalize); 616 | rgbe[1] = (unsigned char)(linear[1] * normalize); 617 | rgbe[2] = (unsigned char)(linear[2] * normalize); 618 | rgbe[3] = (unsigned char)(exponent + 128); 619 | } 620 | } 621 | 622 | static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) 623 | { 624 | unsigned char lengthbyte = STBIW_UCHAR(length+128); 625 | STBIW_ASSERT(length+128 <= 255); 626 | s->func(s->context, &lengthbyte, 1); 627 | s->func(s->context, &databyte, 1); 628 | } 629 | 630 | static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) 631 | { 632 | unsigned char lengthbyte = STBIW_UCHAR(length); 633 | STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code 634 | s->func(s->context, &lengthbyte, 1); 635 | s->func(s->context, data, length); 636 | } 637 | 638 | static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) 639 | { 640 | unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; 641 | unsigned char rgbe[4]; 642 | float linear[3]; 643 | int x; 644 | 645 | scanlineheader[2] = (width&0xff00)>>8; 646 | scanlineheader[3] = (width&0x00ff); 647 | 648 | /* skip RLE for images too small or large */ 649 | if (width < 8 || width >= 32768) { 650 | for (x=0; x < width; x++) { 651 | switch (ncomp) { 652 | case 4: /* fallthrough */ 653 | case 3: linear[2] = scanline[x*ncomp + 2]; 654 | linear[1] = scanline[x*ncomp + 1]; 655 | linear[0] = scanline[x*ncomp + 0]; 656 | break; 657 | default: 658 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 659 | break; 660 | } 661 | stbiw__linear_to_rgbe(rgbe, linear); 662 | s->func(s->context, rgbe, 4); 663 | } 664 | } else { 665 | int c,r; 666 | /* encode into scratch buffer */ 667 | for (x=0; x < width; x++) { 668 | switch(ncomp) { 669 | case 4: /* fallthrough */ 670 | case 3: linear[2] = scanline[x*ncomp + 2]; 671 | linear[1] = scanline[x*ncomp + 1]; 672 | linear[0] = scanline[x*ncomp + 0]; 673 | break; 674 | default: 675 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 676 | break; 677 | } 678 | stbiw__linear_to_rgbe(rgbe, linear); 679 | scratch[x + width*0] = rgbe[0]; 680 | scratch[x + width*1] = rgbe[1]; 681 | scratch[x + width*2] = rgbe[2]; 682 | scratch[x + width*3] = rgbe[3]; 683 | } 684 | 685 | s->func(s->context, scanlineheader, 4); 686 | 687 | /* RLE each component separately */ 688 | for (c=0; c < 4; c++) { 689 | unsigned char *comp = &scratch[width*c]; 690 | 691 | x = 0; 692 | while (x < width) { 693 | // find first run 694 | r = x; 695 | while (r+2 < width) { 696 | if (comp[r] == comp[r+1] && comp[r] == comp[r+2]) 697 | break; 698 | ++r; 699 | } 700 | if (r+2 >= width) 701 | r = width; 702 | // dump up to first run 703 | while (x < r) { 704 | int len = r-x; 705 | if (len > 128) len = 128; 706 | stbiw__write_dump_data(s, len, &comp[x]); 707 | x += len; 708 | } 709 | // if there's a run, output it 710 | if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd 711 | // find next byte after run 712 | while (r < width && comp[r] == comp[x]) 713 | ++r; 714 | // output run up to r 715 | while (x < r) { 716 | int len = r-x; 717 | if (len > 127) len = 127; 718 | stbiw__write_run_data(s, len, comp[x]); 719 | x += len; 720 | } 721 | } 722 | } 723 | } 724 | } 725 | } 726 | 727 | static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data) 728 | { 729 | if (y <= 0 || x <= 0 || data == NULL) 730 | return 0; 731 | else { 732 | // Each component is stored separately. Allocate scratch space for full output scanline. 733 | unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4); 734 | int i, len; 735 | char buffer[128]; 736 | char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 737 | s->func(s->context, header, sizeof(header)-1); 738 | 739 | #ifdef STBI_MSC_SECURE_CRT 740 | len = sprintf_s(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 741 | #else 742 | len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 743 | #endif 744 | s->func(s->context, buffer, len); 745 | 746 | for(i=0; i < y; i++) 747 | stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)); 748 | STBIW_FREE(scratch); 749 | return 1; 750 | } 751 | } 752 | 753 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) 754 | { 755 | stbi__write_context s; 756 | stbi__start_write_callbacks(&s, func, context); 757 | return stbi_write_hdr_core(&s, x, y, comp, (float *) data); 758 | } 759 | 760 | #ifndef STBI_WRITE_NO_STDIO 761 | STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) 762 | { 763 | stbi__write_context s; 764 | if (stbi__start_write_file(&s,filename)) { 765 | int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); 766 | stbi__end_write_file(&s); 767 | return r; 768 | } else 769 | return 0; 770 | } 771 | #endif // STBI_WRITE_NO_STDIO 772 | 773 | 774 | ////////////////////////////////////////////////////////////////////////////// 775 | // 776 | // PNG writer 777 | // 778 | 779 | #ifndef STBIW_ZLIB_COMPRESS 780 | // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() 781 | #define stbiw__sbraw(a) ((int *) (a) - 2) 782 | #define stbiw__sbm(a) stbiw__sbraw(a)[0] 783 | #define stbiw__sbn(a) stbiw__sbraw(a)[1] 784 | 785 | #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a)) 786 | #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0) 787 | #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a))) 788 | 789 | #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v)) 790 | #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0) 791 | #define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0) 792 | 793 | static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) 794 | { 795 | int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; 796 | void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2); 797 | STBIW_ASSERT(p); 798 | if (p) { 799 | if (!*arr) ((int *) p)[1] = 0; 800 | *arr = (void *) ((int *) p + 2); 801 | stbiw__sbm(*arr) = m; 802 | } 803 | return *arr; 804 | } 805 | 806 | static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) 807 | { 808 | while (*bitcount >= 8) { 809 | stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer)); 810 | *bitbuffer >>= 8; 811 | *bitcount -= 8; 812 | } 813 | return data; 814 | } 815 | 816 | static int stbiw__zlib_bitrev(int code, int codebits) 817 | { 818 | int res=0; 819 | while (codebits--) { 820 | res = (res << 1) | (code & 1); 821 | code >>= 1; 822 | } 823 | return res; 824 | } 825 | 826 | static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit) 827 | { 828 | int i; 829 | for (i=0; i < limit && i < 258; ++i) 830 | if (a[i] != b[i]) break; 831 | return i; 832 | } 833 | 834 | static unsigned int stbiw__zhash(unsigned char *data) 835 | { 836 | stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16); 837 | hash ^= hash << 3; 838 | hash += hash >> 5; 839 | hash ^= hash << 4; 840 | hash += hash >> 17; 841 | hash ^= hash << 25; 842 | hash += hash >> 6; 843 | return hash; 844 | } 845 | 846 | #define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount)) 847 | #define stbiw__zlib_add(code,codebits) \ 848 | (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush()) 849 | #define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c) 850 | // default huffman tables 851 | #define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8) 852 | #define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9) 853 | #define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7) 854 | #define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8) 855 | #define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n)) 856 | #define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n)) 857 | 858 | #define stbiw__ZHASH 16384 859 | 860 | #endif // STBIW_ZLIB_COMPRESS 861 | 862 | STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) 863 | { 864 | #ifdef STBIW_ZLIB_COMPRESS 865 | // user provided a zlib compress implementation, use that 866 | return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality); 867 | #else // use builtin 868 | static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 }; 869 | static unsigned char lengtheb[]= { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; 870 | static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 }; 871 | static unsigned char disteb[] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 }; 872 | unsigned int bitbuf=0; 873 | int i,j, bitcount=0; 874 | unsigned char *out = NULL; 875 | unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(char**)); 876 | if (hash_table == NULL) 877 | return NULL; 878 | if (quality < 5) quality = 5; 879 | 880 | stbiw__sbpush(out, 0x78); // DEFLATE 32K window 881 | stbiw__sbpush(out, 0x5e); // FLEVEL = 1 882 | stbiw__zlib_add(1,1); // BFINAL = 1 883 | stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman 884 | 885 | for (i=0; i < stbiw__ZHASH; ++i) 886 | hash_table[i] = NULL; 887 | 888 | i=0; 889 | while (i < data_len-3) { 890 | // hash next 3 bytes of data to be compressed 891 | int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3; 892 | unsigned char *bestloc = 0; 893 | unsigned char **hlist = hash_table[h]; 894 | int n = stbiw__sbcount(hlist); 895 | for (j=0; j < n; ++j) { 896 | if (hlist[j]-data > i-32768) { // if entry lies within window 897 | int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i); 898 | if (d >= best) best=d,bestloc=hlist[j]; 899 | } 900 | } 901 | // when hash table entry is too long, delete half the entries 902 | if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) { 903 | STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality); 904 | stbiw__sbn(hash_table[h]) = quality; 905 | } 906 | stbiw__sbpush(hash_table[h],data+i); 907 | 908 | if (bestloc) { 909 | // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal 910 | h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1); 911 | hlist = hash_table[h]; 912 | n = stbiw__sbcount(hlist); 913 | for (j=0; j < n; ++j) { 914 | if (hlist[j]-data > i-32767) { 915 | int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1); 916 | if (e > best) { // if next match is better, bail on current match 917 | bestloc = NULL; 918 | break; 919 | } 920 | } 921 | } 922 | } 923 | 924 | if (bestloc) { 925 | int d = (int) (data+i - bestloc); // distance back 926 | STBIW_ASSERT(d <= 32767 && best <= 258); 927 | for (j=0; best > lengthc[j+1]-1; ++j); 928 | stbiw__zlib_huff(j+257); 929 | if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]); 930 | for (j=0; d > distc[j+1]-1; ++j); 931 | stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5); 932 | if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]); 933 | i += best; 934 | } else { 935 | stbiw__zlib_huffb(data[i]); 936 | ++i; 937 | } 938 | } 939 | // write out final bytes 940 | for (;i < data_len; ++i) 941 | stbiw__zlib_huffb(data[i]); 942 | stbiw__zlib_huff(256); // end of block 943 | // pad with 0 bits to byte boundary 944 | while (bitcount) 945 | stbiw__zlib_add(0,1); 946 | 947 | for (i=0; i < stbiw__ZHASH; ++i) 948 | (void) stbiw__sbfree(hash_table[i]); 949 | STBIW_FREE(hash_table); 950 | 951 | { 952 | // compute adler32 on input 953 | unsigned int s1=1, s2=0; 954 | int blocklen = (int) (data_len % 5552); 955 | j=0; 956 | while (j < data_len) { 957 | for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1; 958 | s1 %= 65521, s2 %= 65521; 959 | j += blocklen; 960 | blocklen = 5552; 961 | } 962 | stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8)); 963 | stbiw__sbpush(out, STBIW_UCHAR(s2)); 964 | stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8)); 965 | stbiw__sbpush(out, STBIW_UCHAR(s1)); 966 | } 967 | *out_len = stbiw__sbn(out); 968 | // make returned pointer freeable 969 | STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len); 970 | return (unsigned char *) stbiw__sbraw(out); 971 | #endif // STBIW_ZLIB_COMPRESS 972 | } 973 | 974 | static unsigned int stbiw__crc32(unsigned char *buffer, int len) 975 | { 976 | #ifdef STBIW_CRC32 977 | return STBIW_CRC32(buffer, len); 978 | #else 979 | static unsigned int crc_table[256] = 980 | { 981 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 982 | 0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 983 | 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 984 | 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 985 | 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 986 | 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 987 | 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 988 | 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 989 | 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 990 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 991 | 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 992 | 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 993 | 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 994 | 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 995 | 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 996 | 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 997 | 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 998 | 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 999 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 1000 | 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 1001 | 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 1002 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 1003 | 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 1004 | 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 1005 | 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 1006 | 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 1007 | 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 1008 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 1009 | 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 1010 | 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 1011 | 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 1012 | 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D 1013 | }; 1014 | 1015 | unsigned int crc = ~0u; 1016 | int i; 1017 | for (i=0; i < len; ++i) 1018 | crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; 1019 | return ~crc; 1020 | #endif 1021 | } 1022 | 1023 | #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4) 1024 | #define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v)); 1025 | #define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3]) 1026 | 1027 | static void stbiw__wpcrc(unsigned char **data, int len) 1028 | { 1029 | unsigned int crc = stbiw__crc32(*data - len - 4, len+4); 1030 | stbiw__wp32(*data, crc); 1031 | } 1032 | 1033 | static unsigned char stbiw__paeth(int a, int b, int c) 1034 | { 1035 | int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c); 1036 | if (pa <= pb && pa <= pc) return STBIW_UCHAR(a); 1037 | if (pb <= pc) return STBIW_UCHAR(b); 1038 | return STBIW_UCHAR(c); 1039 | } 1040 | 1041 | // @OPTIMIZE: provide an option that always forces left-predict or paeth predict 1042 | static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer) 1043 | { 1044 | static int mapping[] = { 0,1,2,3,4 }; 1045 | static int firstmap[] = { 0,1,0,5,6 }; 1046 | int *mymap = (y != 0) ? mapping : firstmap; 1047 | int i; 1048 | int type = mymap[filter_type]; 1049 | unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y); 1050 | int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes; 1051 | 1052 | if (type==0) { 1053 | memcpy(line_buffer, z, width*n); 1054 | return; 1055 | } 1056 | 1057 | // first loop isn't optimized since it's just one pixel 1058 | for (i = 0; i < n; ++i) { 1059 | switch (type) { 1060 | case 1: line_buffer[i] = z[i]; break; 1061 | case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break; 1062 | case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break; 1063 | case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-signed_stride],0)); break; 1064 | case 5: line_buffer[i] = z[i]; break; 1065 | case 6: line_buffer[i] = z[i]; break; 1066 | } 1067 | } 1068 | switch (type) { 1069 | case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break; 1070 | case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break; 1071 | case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break; 1072 | case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break; 1073 | case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break; 1074 | case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; 1075 | } 1076 | } 1077 | 1078 | STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) 1079 | { 1080 | int force_filter = stbi_write_force_png_filter; 1081 | int ctype[5] = { -1, 0, 4, 2, 6 }; 1082 | unsigned char sig[8] = { 137,80,78,71,13,10,26,10 }; 1083 | unsigned char *out,*o, *filt, *zlib; 1084 | signed char *line_buffer; 1085 | int j,zlen; 1086 | 1087 | if (stride_bytes == 0) 1088 | stride_bytes = x * n; 1089 | 1090 | if (force_filter >= 5) { 1091 | force_filter = -1; 1092 | } 1093 | 1094 | filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0; 1095 | line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; } 1096 | for (j=0; j < y; ++j) { 1097 | int filter_type; 1098 | if (force_filter > -1) { 1099 | filter_type = force_filter; 1100 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer); 1101 | } else { // Estimate the best filter by running through all of them: 1102 | int best_filter = 0, best_filter_val = 0x7fffffff, est, i; 1103 | for (filter_type = 0; filter_type < 5; filter_type++) { 1104 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer); 1105 | 1106 | // Estimate the entropy of the line using this filter; the less, the better. 1107 | est = 0; 1108 | for (i = 0; i < x*n; ++i) { 1109 | est += abs((signed char) line_buffer[i]); 1110 | } 1111 | if (est < best_filter_val) { 1112 | best_filter_val = est; 1113 | best_filter = filter_type; 1114 | } 1115 | } 1116 | if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it 1117 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer); 1118 | filter_type = best_filter; 1119 | } 1120 | } 1121 | // when we get here, filter_type contains the filter type, and line_buffer contains the data 1122 | filt[j*(x*n+1)] = (unsigned char) filter_type; 1123 | STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n); 1124 | } 1125 | STBIW_FREE(line_buffer); 1126 | zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level); 1127 | STBIW_FREE(filt); 1128 | if (!zlib) return 0; 1129 | 1130 | // each tag requires 12 bytes of overhead 1131 | out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12); 1132 | if (!out) return 0; 1133 | *out_len = 8 + 12+13 + 12+zlen + 12; 1134 | 1135 | o=out; 1136 | STBIW_MEMMOVE(o,sig,8); o+= 8; 1137 | stbiw__wp32(o, 13); // header length 1138 | stbiw__wptag(o, "IHDR"); 1139 | stbiw__wp32(o, x); 1140 | stbiw__wp32(o, y); 1141 | *o++ = 8; 1142 | *o++ = STBIW_UCHAR(ctype[n]); 1143 | *o++ = 0; 1144 | *o++ = 0; 1145 | *o++ = 0; 1146 | stbiw__wpcrc(&o,13); 1147 | 1148 | stbiw__wp32(o, zlen); 1149 | stbiw__wptag(o, "IDAT"); 1150 | STBIW_MEMMOVE(o, zlib, zlen); 1151 | o += zlen; 1152 | STBIW_FREE(zlib); 1153 | stbiw__wpcrc(&o, zlen); 1154 | 1155 | stbiw__wp32(o,0); 1156 | stbiw__wptag(o, "IEND"); 1157 | stbiw__wpcrc(&o,0); 1158 | 1159 | STBIW_ASSERT(o == out + *out_len); 1160 | 1161 | return out; 1162 | } 1163 | 1164 | #ifndef STBI_WRITE_NO_STDIO 1165 | STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) 1166 | { 1167 | FILE *f; 1168 | int len; 1169 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1170 | if (png == NULL) return 0; 1171 | 1172 | f = stbiw__fopen(filename, "wb"); 1173 | if (!f) { STBIW_FREE(png); return 0; } 1174 | fwrite(png, 1, len, f); 1175 | fclose(f); 1176 | STBIW_FREE(png); 1177 | return 1; 1178 | } 1179 | #endif 1180 | 1181 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) 1182 | { 1183 | int len; 1184 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1185 | if (png == NULL) return 0; 1186 | func(context, png, len); 1187 | STBIW_FREE(png); 1188 | return 1; 1189 | } 1190 | 1191 | 1192 | /* *************************************************************************** 1193 | * 1194 | * JPEG writer 1195 | * 1196 | * This is based on Jon Olick's jo_jpeg.cpp: 1197 | * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html 1198 | */ 1199 | 1200 | static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18, 1201 | 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 }; 1202 | 1203 | static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) { 1204 | int bitBuf = *bitBufP, bitCnt = *bitCntP; 1205 | bitCnt += bs[1]; 1206 | bitBuf |= bs[0] << (24 - bitCnt); 1207 | while(bitCnt >= 8) { 1208 | unsigned char c = (bitBuf >> 16) & 255; 1209 | stbiw__putc(s, c); 1210 | if(c == 255) { 1211 | stbiw__putc(s, 0); 1212 | } 1213 | bitBuf <<= 8; 1214 | bitCnt -= 8; 1215 | } 1216 | *bitBufP = bitBuf; 1217 | *bitCntP = bitCnt; 1218 | } 1219 | 1220 | static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) { 1221 | float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p; 1222 | float z1, z2, z3, z4, z5, z11, z13; 1223 | 1224 | float tmp0 = d0 + d7; 1225 | float tmp7 = d0 - d7; 1226 | float tmp1 = d1 + d6; 1227 | float tmp6 = d1 - d6; 1228 | float tmp2 = d2 + d5; 1229 | float tmp5 = d2 - d5; 1230 | float tmp3 = d3 + d4; 1231 | float tmp4 = d3 - d4; 1232 | 1233 | // Even part 1234 | float tmp10 = tmp0 + tmp3; // phase 2 1235 | float tmp13 = tmp0 - tmp3; 1236 | float tmp11 = tmp1 + tmp2; 1237 | float tmp12 = tmp1 - tmp2; 1238 | 1239 | d0 = tmp10 + tmp11; // phase 3 1240 | d4 = tmp10 - tmp11; 1241 | 1242 | z1 = (tmp12 + tmp13) * 0.707106781f; // c4 1243 | d2 = tmp13 + z1; // phase 5 1244 | d6 = tmp13 - z1; 1245 | 1246 | // Odd part 1247 | tmp10 = tmp4 + tmp5; // phase 2 1248 | tmp11 = tmp5 + tmp6; 1249 | tmp12 = tmp6 + tmp7; 1250 | 1251 | // The rotator is modified from fig 4-8 to avoid extra negations. 1252 | z5 = (tmp10 - tmp12) * 0.382683433f; // c6 1253 | z2 = tmp10 * 0.541196100f + z5; // c2-c6 1254 | z4 = tmp12 * 1.306562965f + z5; // c2+c6 1255 | z3 = tmp11 * 0.707106781f; // c4 1256 | 1257 | z11 = tmp7 + z3; // phase 5 1258 | z13 = tmp7 - z3; 1259 | 1260 | *d5p = z13 + z2; // phase 6 1261 | *d3p = z13 - z2; 1262 | *d1p = z11 + z4; 1263 | *d7p = z11 - z4; 1264 | 1265 | *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6; 1266 | } 1267 | 1268 | static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) { 1269 | int tmp1 = val < 0 ? -val : val; 1270 | val = val < 0 ? val-1 : val; 1271 | bits[1] = 1; 1272 | while(tmp1 >>= 1) { 1273 | ++bits[1]; 1274 | } 1275 | bits[0] = val & ((1<0)&&(DU[end0pos]==0); --end0pos) { 1313 | } 1314 | // end0pos = first element in reverse order !=0 1315 | if(end0pos == 0) { 1316 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1317 | return DU[0]; 1318 | } 1319 | for(i = 1; i <= end0pos; ++i) { 1320 | int startpos = i; 1321 | int nrzeroes; 1322 | unsigned short bits[2]; 1323 | for (; DU[i]==0 && i<=end0pos; ++i) { 1324 | } 1325 | nrzeroes = i-startpos; 1326 | if ( nrzeroes >= 16 ) { 1327 | int lng = nrzeroes>>4; 1328 | int nrmarker; 1329 | for (nrmarker=1; nrmarker <= lng; ++nrmarker) 1330 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes); 1331 | nrzeroes &= 15; 1332 | } 1333 | stbiw__jpg_calcBits(DU[i], bits); 1334 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]); 1335 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); 1336 | } 1337 | if(end0pos != 63) { 1338 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1339 | } 1340 | return DU[0]; 1341 | } 1342 | 1343 | static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) { 1344 | // Constants that don't pollute global namespace 1345 | static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0}; 1346 | static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1347 | static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d}; 1348 | static const unsigned char std_ac_luminance_values[] = { 1349 | 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, 1350 | 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, 1351 | 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, 1352 | 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 1353 | 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, 1354 | 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, 1355 | 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1356 | }; 1357 | static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; 1358 | static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1359 | static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77}; 1360 | static const unsigned char std_ac_chrominance_values[] = { 1361 | 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, 1362 | 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, 1363 | 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, 1364 | 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, 1365 | 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, 1366 | 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, 1367 | 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1368 | }; 1369 | // Huffman tables 1370 | static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}}; 1371 | static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}}; 1372 | static const unsigned short YAC_HT[256][2] = { 1373 | {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1374 | {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1375 | {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1376 | {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1377 | {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1378 | {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1379 | {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1380 | {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1381 | {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1382 | {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1383 | {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1384 | {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1385 | {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1386 | {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1387 | {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1388 | {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1389 | }; 1390 | static const unsigned short UVAC_HT[256][2] = { 1391 | {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1392 | {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1393 | {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1394 | {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1395 | {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1396 | {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1397 | {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1398 | {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1399 | {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1400 | {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1401 | {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1402 | {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1403 | {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1404 | {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1405 | {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1406 | {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1407 | }; 1408 | static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22, 1409 | 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99}; 1410 | static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99, 1411 | 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}; 1412 | static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1413 | 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f }; 1414 | 1415 | int row, col, i, k; 1416 | float fdtbl_Y[64], fdtbl_UV[64]; 1417 | unsigned char YTable[64], UVTable[64]; 1418 | 1419 | if(!data || !width || !height || comp > 4 || comp < 1) { 1420 | return 0; 1421 | } 1422 | 1423 | quality = quality ? quality : 90; 1424 | quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; 1425 | quality = quality < 50 ? 5000 / quality : 200 - quality * 2; 1426 | 1427 | for(i = 0; i < 64; ++i) { 1428 | int uvti, yti = (YQT[i]*quality+50)/100; 1429 | YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti); 1430 | uvti = (UVQT[i]*quality+50)/100; 1431 | UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); 1432 | } 1433 | 1434 | for(row = 0, k = 0; row < 8; ++row) { 1435 | for(col = 0; col < 8; ++col, ++k) { 1436 | fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1437 | fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1438 | } 1439 | } 1440 | 1441 | // Write Headers 1442 | { 1443 | static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 }; 1444 | static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 }; 1445 | const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width), 1446 | 3,1,0x11,0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 }; 1447 | s->func(s->context, (void*)head0, sizeof(head0)); 1448 | s->func(s->context, (void*)YTable, sizeof(YTable)); 1449 | stbiw__putc(s, 1); 1450 | s->func(s->context, UVTable, sizeof(UVTable)); 1451 | s->func(s->context, (void*)head1, sizeof(head1)); 1452 | s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1); 1453 | s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values)); 1454 | stbiw__putc(s, 0x10); // HTYACinfo 1455 | s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1); 1456 | s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values)); 1457 | stbiw__putc(s, 1); // HTUDCinfo 1458 | s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1); 1459 | s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values)); 1460 | stbiw__putc(s, 0x11); // HTUACinfo 1461 | s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1); 1462 | s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values)); 1463 | s->func(s->context, (void*)head2, sizeof(head2)); 1464 | } 1465 | 1466 | // Encode 8x8 macroblocks 1467 | { 1468 | static const unsigned short fillBits[] = {0x7F, 7}; 1469 | const unsigned char *imageData = (const unsigned char *)data; 1470 | int DCY=0, DCU=0, DCV=0; 1471 | int bitBuf=0, bitCnt=0; 1472 | // comp == 2 is grey+alpha (alpha is ignored) 1473 | int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0; 1474 | int x, y, pos; 1475 | for(y = 0; y < height; y += 8) { 1476 | for(x = 0; x < width; x += 8) { 1477 | float YDU[64], UDU[64], VDU[64]; 1478 | for(row = y, pos = 0; row < y+8; ++row) { 1479 | int p; 1480 | if(row < height) { 1481 | p = (stbi__flip_vertically_on_write ? (height-1-row) : row)*width*comp; 1482 | } else { 1483 | // row >= height => use last input row (=> first if flipping) 1484 | p = stbi__flip_vertically_on_write ? 0 : ((height-1)*width*comp); 1485 | } 1486 | for(col = x; col < x+8; ++col, ++pos) { 1487 | float r, g, b; 1488 | // if col >= width => use pixel from last input column 1489 | p += ((col < width) ? col : (width-1))*comp; 1490 | 1491 | r = imageData[p+0]; 1492 | g = imageData[p+ofsG]; 1493 | b = imageData[p+ofsB]; 1494 | YDU[pos]=+0.29900f*r+0.58700f*g+0.11400f*b-128; 1495 | UDU[pos]=-0.16874f*r-0.33126f*g+0.50000f*b; 1496 | VDU[pos]=+0.50000f*r-0.41869f*g-0.08131f*b; 1497 | } 1498 | } 1499 | 1500 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, YDU, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1501 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, UDU, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1502 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, VDU, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1503 | } 1504 | } 1505 | 1506 | // Do the bit alignment of the EOI marker 1507 | stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits); 1508 | } 1509 | 1510 | // EOI 1511 | stbiw__putc(s, 0xFF); 1512 | stbiw__putc(s, 0xD9); 1513 | 1514 | return 1; 1515 | } 1516 | 1517 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) 1518 | { 1519 | stbi__write_context s; 1520 | stbi__start_write_callbacks(&s, func, context); 1521 | return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); 1522 | } 1523 | 1524 | 1525 | #ifndef STBI_WRITE_NO_STDIO 1526 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) 1527 | { 1528 | stbi__write_context s; 1529 | if (stbi__start_write_file(&s,filename)) { 1530 | int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); 1531 | stbi__end_write_file(&s); 1532 | return r; 1533 | } else 1534 | return 0; 1535 | } 1536 | #endif 1537 | 1538 | #endif // STB_IMAGE_WRITE_IMPLEMENTATION 1539 | 1540 | /* Revision history 1541 | 1.10 (2019-02-07) 1542 | support utf8 filenames in Windows; fix warnings and platform ifdefs 1543 | 1.09 (2018-02-11) 1544 | fix typo in zlib quality API, improve STB_I_W_STATIC in C++ 1545 | 1.08 (2018-01-29) 1546 | add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter 1547 | 1.07 (2017-07-24) 1548 | doc fix 1549 | 1.06 (2017-07-23) 1550 | writing JPEG (using Jon Olick's code) 1551 | 1.05 ??? 1552 | 1.04 (2017-03-03) 1553 | monochrome BMP expansion 1554 | 1.03 ??? 1555 | 1.02 (2016-04-02) 1556 | avoid allocating large structures on the stack 1557 | 1.01 (2016-01-16) 1558 | STBIW_REALLOC_SIZED: support allocators with no realloc support 1559 | avoid race-condition in crc initialization 1560 | minor compile issues 1561 | 1.00 (2015-09-14) 1562 | installable file IO function 1563 | 0.99 (2015-09-13) 1564 | warning fixes; TGA rle support 1565 | 0.98 (2015-04-08) 1566 | added STBIW_MALLOC, STBIW_ASSERT etc 1567 | 0.97 (2015-01-18) 1568 | fixed HDR asserts, rewrote HDR rle logic 1569 | 0.96 (2015-01-17) 1570 | add HDR output 1571 | fix monochrome BMP 1572 | 0.95 (2014-08-17) 1573 | add monochrome TGA output 1574 | 0.94 (2014-05-31) 1575 | rename private functions to avoid conflicts with stb_image.h 1576 | 0.93 (2014-05-27) 1577 | warning fixes 1578 | 0.92 (2010-08-01) 1579 | casts to unsigned char to fix warnings 1580 | 0.91 (2010-07-17) 1581 | first public release 1582 | 0.90 first internal release 1583 | */ 1584 | 1585 | /* 1586 | ------------------------------------------------------------------------------ 1587 | This software is available under 2 licenses -- choose whichever you prefer. 1588 | ------------------------------------------------------------------------------ 1589 | ALTERNATIVE A - MIT License 1590 | Copyright (c) 2017 Sean Barrett 1591 | Permission is hereby granted, free of charge, to any person obtaining a copy of 1592 | this software and associated documentation files (the "Software"), to deal in 1593 | the Software without restriction, including without limitation the rights to 1594 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1595 | of the Software, and to permit persons to whom the Software is furnished to do 1596 | so, subject to the following conditions: 1597 | The above copyright notice and this permission notice shall be included in all 1598 | copies or substantial portions of the Software. 1599 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1600 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1601 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1602 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1603 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1604 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1605 | SOFTWARE. 1606 | ------------------------------------------------------------------------------ 1607 | ALTERNATIVE B - Public Domain (www.unlicense.org) 1608 | This is free and unencumbered software released into the public domain. 1609 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1610 | software, either in source code form or as a compiled binary, for any purpose, 1611 | commercial or non-commercial, and by any means. 1612 | In jurisdictions that recognize copyright laws, the author or authors of this 1613 | software dedicate any and all copyright interest in the software to the public 1614 | domain. We make this dedication for the benefit of the public at large and to 1615 | the detriment of our heirs and successors. We intend this dedication to be an 1616 | overt act of relinquishment in perpetuity of all present and future rights to 1617 | this software under copyright law. 1618 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1619 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1620 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1621 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1622 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1623 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1624 | ------------------------------------------------------------------------------ 1625 | */ 1626 | -------------------------------------------------------------------------------- /whitenoise.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "misc.h" 5 | #include "settings.h" 6 | 7 | inline std::seed_seq& GetRNGSeed() 8 | { 9 | #if DETERMINISTIC() 10 | static std::seed_seq fullSeed{ DETERMINISTIC_SEED() }; 11 | #else 12 | static std::random_device rd; 13 | static std::seed_seq fullSeed{ rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd() }; 14 | #endif 15 | return fullSeed; 16 | } 17 | 18 | template 19 | inline T RandomValue(std::mt19937& rng) 20 | { 21 | std::uniform_int_distribution dist(0, std::numeric_limits::max()); 22 | return dist(rng); 23 | } 24 | 25 | template <> 26 | inline uint8_t RandomValue(std::mt19937& rng) 27 | { 28 | std::uniform_int_distribution dist(0, 255); // can't be uint8_t :/ 29 | return uint8_t(dist(rng)); 30 | } 31 | 32 | template 33 | inline void MakeWhiteNoise(std::mt19937& rng, std::vector& pixels, size_t width) 34 | { 35 | pixels.resize(width*width); 36 | 37 | // NOTE: this works, but won't give a balanced histogram 38 | //for (T& pixel : pixels) 39 | //pixel = RandomValue(); 40 | 41 | for (size_t index = 0, count = width * width; index < count; ++index) 42 | { 43 | float percent = float(index) / float(count - 1); 44 | float value = Lerp(0, float(std::numeric_limits::max() + 1), percent); // intentionally not using convert.h conversion. this is subtly different. 45 | value = Clamp(0.0f, float(std::numeric_limits::max()), value); 46 | pixels[index] = T(value); 47 | } 48 | 49 | std::shuffle(pixels.begin(), pixels.end(), rng); 50 | } 51 | 52 | inline void MakeWhiteNoiseFloat(std::mt19937& rng, std::vector& pixels, size_t width) 53 | { 54 | pixels.resize(width*width); 55 | 56 | for (size_t index = 0, count = width * width; index < count; ++index) 57 | pixels[index] = float(index) / float(count - 1); 58 | 59 | std::shuffle(pixels.begin(), pixels.end(), rng); 60 | } --------------------------------------------------------------------------------