Created
December 27, 2014 22:01
-
-
Save Cyberax/3a7796231be66d0f64cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int generate_indexed_something(size_t BufPixels, xxx ResultArray, xxx pixels, int_t pixlen) | |
{ | |
PyObject * BufString = 0; | |
PyObject * Result = 0; | |
/* extend ResultArray by a bunch of converted pixels at a time */ | |
if (BufPixels % 4 != 0) | |
{ | |
/* fill out unused part of byte with zeroes--actually shouldn't occur */ | |
PixBuf[BufPixels / 4] &= ~(0xff << BufPixels % 4 * 2); | |
} | |
BufString = PyString_FromStringAndSize((const char *)PixBuf, (BufPixels + 3) / 4); | |
if (BufString == 0) | |
goto cleanup; | |
Result = PyObject_CallMethod(ResultArray, "fromstring", "O", BufString); | |
if (Result == 0) | |
goto cleanup; | |
cleanup: | |
Py_XDECREF(Result); | |
Py_XDECREF(BufString); | |
return PyErr_Occurred(); | |
} | |
void do_something() | |
{ | |
/* generate indexed version of image */ | |
const size_t PixBufSize = 128 /* convenient buffer size to avoid heap allocation */; | |
const size_t MaxPixels = PixBufSize * 4; /* 2 bits per pixel */ | |
uint8_t PixBuf[PixBufSize]; | |
size_t BufPixels; | |
int_t pixlen = nrpixels; | |
ResultArray = PyObject_CallMethod(ArrayModule, "array", "s", "B"); | |
if (ResultArray == 0) | |
goto cleanup; | |
pixels = (const uint32_t *)pixaddr; | |
BufPixels = 0; | |
for(pixlen = nrpixels; pixlen>=0 && BufPixels !=0; --pixlen) | |
{ | |
if (generate_indexed_something(BufPixels, pixels, pixlen)) | |
goto cleanup; | |
const uint32_t thispixel = *pixels; | |
for (histindex = 0; histogram[histindex].pixel != thispixel; ++histindex) | |
/* guaranteed to find pixel index */; | |
if (BufPixels % 4 == 0) | |
{ | |
/* starting new byte */ | |
PixBuf[BufPixels / 4] = 0; /* ensure there's no junk in it */ | |
} | |
PixBuf[BufPixels / 4] |= histogram[histindex].index << BufPixels % 4 * 2; | |
++BufPixels; | |
++pixels; | |
} | |
cleanup: | |
Py_XDECREF(ResultArray); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment