Yeah, I was able to fix it. It’s faster on larger images with way more color, but definitely much faster with 256 colors regardless of any size. So, my code is a improvement based on David’s code. Now, I"m using geometric growth for resizing array, and to avoid as much resize, just like how David implement his colormap 0 code. Which makes me think about this for other cases where I use a dictionary structure.
Real color JPG photo:
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic 100_1090.jpg v - tic colormap 0 toc v +
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input file '100_1090.jpg' at position 0 (1 image 2855x1955x1x3).
[gmic]./ Elapsed time: 1.357 s.
[gmic]./ Display image [0] = '[colormap of 100_1090]'.
[0] = '[colormap of 100_1090]':
size = (262638,1,1,3) [3077.8 Kio of float32].
data = (0,1,0,1,1,0,0,2,0,1,1,0,2,0,2,0,3,0,1,0,1,2,3,0,2,3,0,2,1,3,0,0,4,1,0,4,4,0,3,3,0,2,4,2,1,2,0,0,3,0,3,4,4,0,5,5,1,0,4,0,3,5,3,4, ... ,232,227,227,226,225,239,239,236,235,234,234,234,241,240,232,231,231,233,229,229,229,227,238,238,238,235,235,233,231,230,229,237,240,237,234,233,243,240,237,240,236,235,235,235,233,232,239,239,239,236,236,241,238,238,234,241,241,238,236,236,240,237,237,239).
min = 0, max = 255, mean = 113.888, std = 69.4318, coords_min = (0,0,0,0), coords_max = (179481,0,0,0).
[gmic]./ End G'MIC interpreter.
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic 100_1090.jpg tic rep_uniq_cols 0 toc
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input file '100_1090.jpg' at position 0 (1 image 2855x1955x1x3).
[gmic]./ Initialize timer.
[gmic]./ Elapsed time: 1.176 s.
[gmic]./ Display image [0] = '[empty]'.
[0] = '[empty]':
size = (262638,1,1,3) [3077.8 Kio of float32].
data = (0,237,101,152,153,83,158,98,147,188,222,173,255,79,188,194,32,104,59,182,12,126,66,11,213,87,124,162,198,94,217,119,186,68,50,227,104,215,172,35,18,214,156,59,171,101,169,164,199,16,43,102,149,160,78,146,164,136,181,26,22,24,73,184, ... ,27,27,19,47,51,11,27,7,35,7,15,39,99,23,27,15,35,47,11,83,67,31,31,31,23,27,23,87,71,79,67,51,55,23,15,31,59,75,27,55,71,91,39,19,67,15,79,55,71,63,19,63,47,43,31,11,31,15,35,27,31,19,27,43).
min = 0, max = 255, mean = 113.888, std = 69.4318, coords_min = (0,0,0,0), coords_max = (12,0,0,0).
[gmic]./ End G'MIC interpreter.
Sample Cat Result (256 colors on 600x550):
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic sp cat tic colormap 0 toc
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input sample image 'cat' (1 image 600x550x1x3).
[gmic]./ Initialize timer.
[gmic]./ Estimate full colormap for image [0], sorted by increasing norm.
[gmic]./ Elapsed time: 0.09 s.
[gmic]./ Display image [0] = '[colormap of cat]'.
[0] = '[colormap of cat]':
size = (256,1,1,3) [3072 b of float32].
data = (4,16,30,24,43,42,56,60,54,72,69,79,74,65,87,87,49,87,100,77,98,97,111,98,112,87,108,122,123,108,121,131,97,119,133,131,116,140,143,134,127,141,149,91,115,148,125,144,153,141,155,152,139,161,135,151,161,156,150,161,134,160,167,131, ... ,102,62,140,154,128,86,109,51,115,99,69,102,128,122,158,79,146,133,92,165,113,67,139,105,85,158,126,169,136,147,115,100,176,140,87,149,161,131,182,117,105,157,151,174,165,189,117,129,143,174,184,165,196,137,181,153,191,202,165,199,207,185,223,214).
min = 4, max = 244, mean = 118.258, std = 66.143, coords_min = (0,0,0,0), coords_max = (245,0,0,0).
[gmic]./ End G'MIC interpreter.
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic sp cat tic rep_uniq_cols 0 toc
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input sample image 'cat' (1 image 600x550x1x3).
[gmic]./ Initialize timer.
[gmic]./ Elapsed time: 0.044 s.
[gmic]./ Display image [0] = '[empty]'.
[0] = '[empty]':
size = (256,1,1,3) [3072 b of float32].
data = (157,231,134,171,197,150,188,212,112,165,131,202,219,98,206,167,205,168,212,174,49,234,87,167,198,189,182,161,184,56,162,211,74,240,192,91,139,243,140,198,24,219,188,210,144,182,238,182,127,203,212,219,196,159,210,195,198,158,188,228,189,217,211,184, ... ,13,115,14,149,25,12,5,110,35,17,4,169,8,52,21,161,174,131,51,55,103,31,140,165,19,7,46,51,75,14,49,112,117,77,15,40,4,105,40,31,80,4,51,21,6,53,133,104,30,9,4,17,35,105,102,90,25,31,15,79,25,41,8,8).
min = 4, max = 244, mean = 118.258, std = 66.143, coords_min = (170,0,0,0), coords_max = (67,0,0,0).
[gmic]./ End G'MIC interpreter.
It’s not much faster, but hey, the base code allows for other things too.
On more extreme case, it is definitely faster because of the use of multi-threading:
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic sp cat,5000 tic colormap 0 toc
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input sample image 'cat' (1 image 5000x4583x1x3).
[gmic]./ Initialize timer.
[gmic]./ Estimate full colormap for image [0], sorted by increasing norm.
[gmic]./ Elapsed time: 3.114 s.
[gmic]./ Display image [0] = '[colormap of cat]'.
[0] = '[colormap of cat]':
size = (256,1,1,3) [3072 b of float32].
data = (4,16,30,24,43,42,56,60,54,72,69,79,74,65,87,87,49,87,100,77,98,97,111,98,112,87,108,122,123,108,121,131,97,119,133,131,116,140,143,134,127,141,149,91,115,148,125,144,153,141,155,152,139,161,135,151,161,156,150,161,134,160,167,131, ... ,102,62,140,154,128,86,109,51,115,99,69,102,128,122,158,79,146,133,92,165,113,67,139,105,85,158,126,169,136,147,115,100,176,140,87,149,161,131,182,117,105,157,151,174,165,189,117,129,143,174,184,165,196,137,181,153,191,202,165,199,207,185,223,214).
min = 4, max = 244, mean = 118.258, std = 66.143, coords_min = (0,0,0,0), coords_max = (245,0,0,0).
[gmic]./ End G'MIC interpreter.
D:\Pictures\A Year Folders Pictures\2007 Folders Pictures\Recovered Photograph>gmic sp cat,5000 tic rep_uniq_cols 0 toc
[gmic]./ Start G'MIC interpreter (v.3.5.0).
[gmic]./ Input sample image 'cat' (1 image 5000x4583x1x3).
[gmic]./ Initialize timer.
[gmic]./ Elapsed time: 0.752 s.
[gmic]./ Display image [0] = '[empty]'.
[0] = '[empty]':
size = (256,1,1,3) [3072 b of float32].
data = (194,125,151,119,194,193,101,169,131,142,87,133,184,167,142,212,65,174,195,177,163,4,223,227,217,171,174,219,141,188,177,219,185,205,74,220,189,229,210,243,116,168,188,214,154,201,206,177,152,42,167,142,183,162,210,244,54,167,150,173,98,212,235,166, ... ,53,223,128,51,60,28,31,14,95,54,37,40,62,109,214,19,39,117,165,24,146,106,54,15,40,207,5,77,19,26,16,161,153,21,61,5,143,99,5,94,56,47,12,140,68,42,11,5,102,128,100,85,7,4,115,32,41,95,61,69,51,174,45,7).
min = 4, max = 244, mean = 118.258, std = 66.143, coords_min = (21,0,0,0), coords_max = (55,0,0,0).
[gmic]./ End G'MIC interpreter.
Note to self: Avoid excessive resize() on single-threading merging of multiple dictionary. Seem to be the reason why the first result doesn’t show difference.
[gmic]./rep_uniq_cols/_rep_colors_freqstat/*foreach#115/ Elapsed time: 0.3 s.
[gmic]./rep_uniq_cols/_rep_colors_freqstat/*foreach#115/*if#229/ Elapsed time: 0.759 s.
The first part is multiple threads utilizing multiple dictionary. Second part is single-threaded merging of multiple dictionaries. So, all I need to do is to avoid excessive resize() and I got a huge boost in speed.
EDIT:
Ok, I did that, and I got the overall execution time to be reduced by more than half for application on 2855x1955 JPG 8-bit image with 262638 colors.
[gmic]./rep_uniq_cols/_rep_colors_freqstat/*foreach#116/ Elapsed time: 0.306 s.
[gmic]./rep_uniq_cols/_rep_colors_freqstat/*foreach#116/*if#231/ Elapsed time: 0.241 s.
So, the total execution time is .547 s. Which is better than 1.147 s provided by colormap 0.
On sample cat, the total execution time is 1/3 of colormap 0. With sp cat,5000
, well, the execution time is 8x faster for my version.