API equivalent of 'convert -format %c histogram:info'?
Posted: 2011-12-04T00:03:13-07:00
I thought this would be easy, but I've been wracking my brain over this for about 12 hours now, as it's the last piece of the program I'm working on, and I just cannot get the API to write a histogram in the format that 'convert -format %c histogram:info' does. All I can get MagickWriteImage to do is either write a single line describing the image, or a list of every pixel in the image and its RGBA values.
So, how can I use the API to generate output like this?
69871: ( 0, 0, 0, 0) #00000000 none
1: ( 18, 18, 18,255) #121212 grey7
1: ( 23, 23, 23,255) #171717 grey9
2: ( 26, 26, 26,255) #1A1A1A grey10
1: ( 33, 33, 33,255) #212121 grey13
5: ( 46, 46, 46,255) #2E2E2E grey18
7: ( 51, 51, 51,255) #333333 grey20
4: ( 54, 54, 54,255) #363636 grey21
2: ( 56, 56, 56,255) #383838 grey22
5: ( 61, 61, 61,255) #3D3D3D grey24
Code:
I've also tried:
Then I tried MagickGetImageHistogram, and while I can iterate over the PixelWand array, I haven't figured out how to get valid RGB values out of them, nor does there seem to be any way to get the color names.
I'm sure I'm missing something that should be really obvious, but it's probably just too obvious for me to see it.
So, how can I use the API to generate output like this?
69871: ( 0, 0, 0, 0) #00000000 none
1: ( 18, 18, 18,255) #121212 grey7
1: ( 23, 23, 23,255) #171717 grey9
2: ( 26, 26, 26,255) #1A1A1A grey10
1: ( 33, 33, 33,255) #212121 grey13
5: ( 46, 46, 46,255) #2E2E2E grey18
7: ( 51, 51, 51,255) #333333 grey20
4: ( 54, 54, 54,255) #363636 grey21
2: ( 56, 56, 56,255) #383838 grey22
5: ( 61, 61, 61,255) #3D3D3D grey24
Code:
Code: Select all
// outputFilePath is of the form "/foo/bar/baz/123456789_color_distribution.txt"
outputPathChars = (*env)->GetStringUTFChars(env, outputFilePath, JNI_FALSE);
finalOutputPath = malloc(snprintf(NULL, 0, "%s%s", "histogram:info:", outputPathChars) + 1);
sprintf(finalOutputPath, "%s%s", "histogram:info:", outputPathChars);
MagickSetImageDepth(wand, 8);
printf("Attempting to write histogram text file to %s\n", finalOutputPath);
MagickSetImageFormat(wand, "%c");
status = MagickWriteImage(wand, finalOutputPath);
Code: Select all
outputPathChars = (*env)->GetStringUTFChars(env, outputFilePath, JNI_FALSE);
finalOutputPath = malloc(snprintf(NULL, 0, "%s", outputPathChars) + 1);
sprintf(finalOutputPath, "%s", outputPathChars);
MagickSetImageDepth(wand, 8);
printf("Attempting to write histogram text file to %s\n", finalOutputPath);
MagickSetImageFormat(wand, "%c histogram:info");
status = MagickWriteImage(wand, finalOutputPath);
I'm sure I'm missing something that should be really obvious, but it's probably just too obvious for me to see it.