Download code
From LiteratePrograms
Back to Median_cut_algorithm_(C_Plus_Plus)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
median_cut.cpp
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?action=history&offset=20080309133934 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?oldid=12754 25 */ 26 27 #include <limits> 28 29 #include <queue> 30 #include <algorithm> 31 32 #include "median_cut.h" 33 34 Block::Block(Point* points, int pointsLength) 35 { 36 this->points = points; 37 this->pointsLength = pointsLength; 38 for(int i=0; i < NUM_DIMENSIONS; i++) 39 { 40 minCorner.x[i] = std::numeric_limits<unsigned char>::min(); 41 maxCorner.x[i] = std::numeric_limits<unsigned char>::max(); 42 } 43 } 44 45 Point * Block::getPoints() 46 { 47 return points; 48 } 49 50 int Block::numPoints() const 51 { 52 return pointsLength; 53 } 54 55 int Block::longestSideIndex() const 56 { 57 int m = maxCorner.x[0] - minCorner.x[0]; 58 int maxIndex = 0; 59 for(int i=1; i < NUM_DIMENSIONS; i++) 60 { 61 int diff = maxCorner.x[i] - minCorner.x[i]; 62 if (diff > m) 63 { 64 m = diff; 65 maxIndex = i; 66 } 67 } 68 return maxIndex; 69 } 70 71 int Block::longestSideLength() const 72 { 73 int i = longestSideIndex(); 74 return maxCorner.x[i] - minCorner.x[i]; 75 } 76 77 bool Block::operator<(const Block& rhs) const 78 { 79 return this->longestSideLength() < rhs.longestSideLength(); 80 } 81 82 void Block::shrink() 83 { 84 int i,j; 85 for(j=0; j<NUM_DIMENSIONS; j++) 86 { 87 minCorner.x[j] = maxCorner.x[j] = points[0].x[j]; 88 } 89 for(i=1; i < pointsLength; i++) 90 { 91 for(j=0; j<NUM_DIMENSIONS; j++) 92 { 93 minCorner.x[j] = min(minCorner.x[j], points[i].x[j]); 94 maxCorner.x[j] = max(maxCorner.x[j], points[i].x[j]); 95 } 96 } 97 } 98 99 std::list<Point> medianCut(Point* image, int numPoints, unsigned int desiredSize) 100 { 101 std::priority_queue<Block> blockQueue; 102 103 Block initialBlock(image, numPoints); 104 initialBlock.shrink(); 105 106 blockQueue.push(initialBlock); 107 while (blockQueue.size() < desiredSize && blockQueue.top().numPoints() > 1) 108 { 109 Block longestBlock = blockQueue.top(); 110 111 blockQueue.pop(); 112 Point * begin = longestBlock.getPoints(); 113 Point * median = longestBlock.getPoints() + (longestBlock.numPoints()+1)/2; 114 Point * end = longestBlock.getPoints() + longestBlock.numPoints(); 115 switch(longestBlock.longestSideIndex()) 116 { 117 case 0: std::nth_element(begin, median, end, CoordinatePointComparator<0>()); break; 118 case 1: std::nth_element(begin, median, end, CoordinatePointComparator<1>()); break; 119 case 2: std::nth_element(begin, median, end, CoordinatePointComparator<2>()); break; 120 } 121 122 Block block1(begin, median-begin), block2(median, end-median); 123 block1.shrink(); 124 block2.shrink(); 125 126 blockQueue.push(block1); 127 blockQueue.push(block2); 128 } 129 130 std::list<Point> result; 131 while(!blockQueue.empty()) 132 { 133 Block block = blockQueue.top(); 134 blockQueue.pop(); 135 Point * points = block.getPoints(); 136 137 int sum[NUM_DIMENSIONS] = {0}; 138 for(int i=0; i < block.numPoints(); i++) 139 { 140 for(int j=0; j < NUM_DIMENSIONS; j++) 141 { 142 sum[j] += points[i].x[j]; 143 } 144 } 145 146 Point averagePoint; 147 for(int j=0; j < NUM_DIMENSIONS; j++) 148 { 149 averagePoint.x[j] = sum[j] / block.numPoints(); 150 } 151 152 result.push_back(averagePoint); 153 } 154 155 return result; 156 } 157 158
median_cut.h
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?action=history&offset=20080309133934 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?oldid=12754 25 */ 26 27 #ifndef MEDIAN_CUT_H_ 28 #define MEDIAN_CUT_H_ 29 #include <list> 30 31 const int NUM_DIMENSIONS = 3; 32 33 struct Point 34 { 35 unsigned char x[NUM_DIMENSIONS]; 36 }; 37 38 class Block 39 { 40 Point minCorner, maxCorner; 41 Point* points; 42 int pointsLength; 43 public: 44 Block(Point* points, int pointsLength); 45 Point * getPoints(); 46 int numPoints() const; 47 int longestSideIndex() const; 48 int longestSideLength() const; 49 bool operator<(const Block& rhs) const; 50 void shrink(); 51 private: 52 template <typename T> 53 static T min(const T a, const T b) 54 { 55 if (a < b) 56 return a; 57 else 58 return b; 59 } 60 61 template <typename T> 62 static T max(const T a, const T b) 63 { 64 if (a > b) 65 return a; 66 else 67 return b; 68 } 69 70 }; 71 72 template <int index> 73 class CoordinatePointComparator 74 { 75 public: 76 bool operator()(Point left, Point right) 77 { 78 return left.x[index] < right.x[index]; 79 } 80 }; 81 82 std::list<Point> medianCut(Point* image, int numPoints, unsigned int desiredSize); 83 #endif /* #ifndef MEDIAN_CUT_H_ */ 84
median_cut_sample.cpp
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?action=history&offset=20080309133934 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Median_cut_algorithm_(C_Plus_Plus)?oldid=12754 25 */ 26 27 #include <iostream> 28 #include "median_cut.h" 29 30 int main(int argc, char* argv[]) { 31 FILE * raw_in; 32 int numPoints = atoi(argv[2]) * atoi(argv[3]); 33 Point* points = (Point*)malloc(sizeof(Point) * numPoints); 34 35 raw_in = fopen(argv[1], "rb"); 36 for(int i = 0; i < numPoints; i++) 37 { 38 fread(&points[i], 3, 1, raw_in); 39 } 40 fclose(raw_in); 41 42 std::list<Point> palette = 43 medianCut(points, numPoints, atoi(argv[4])); 44 45 std::list<Point>::iterator iter; 46 for (iter = palette.begin() ; iter != palette.end(); iter++) 47 { 48 std::cout << (int)iter->x[0] << " " 49 << (int)iter->x[1] << " " 50 << (int)iter->x[2] << std::endl; 51 } 52 53 return 0; 54 } 55
