📄 rgbimgmodule.c
字号:
fseek(inf, starttab[idx],
SEEK_SET);
cur = starttab[idx];
}
fread(rledat, lengthtab[idx], 1, inf);
cur += lengthtab[idx];
expandrow(lptr, rledat, 3-z);
}
if (reverse_order)
lptr -= xsize * sizeof(Py_UInt32);
else
lptr += xsize * sizeof(Py_UInt32);
}
}
if (zsize == 3)
setalpha(base, xsize * ysize);
else if (zsize < 3)
copybw((Py_Int32 *) base, xsize * ysize);
}
else {
rv = PyString_FromStringAndSize((char *) 0,
(xsize*ysize+TAGLEN)*sizeof(Py_Int32));
if (rv == NULL)
goto finally;
base = (unsigned char *) PyString_AsString(rv);
#ifdef ADD_TAGS
addlongimgtag(base, xsize, ysize);
#endif
verdat = (unsigned char *)malloc(xsize);
fseek(inf, 512, SEEK_SET);
for (z = 0; z < zsize; z++) {
lptr = base;
if (reverse_order)
lptr += (ysize - 1) * xsize
* sizeof(Py_UInt32);
for (y = 0; y < ysize; y++) {
fread(verdat, xsize, 1, inf);
interleaverow(lptr, verdat, 3-z, xsize);
if (reverse_order)
lptr -= xsize * sizeof(Py_UInt32);
else
lptr += xsize * sizeof(Py_UInt32);
}
}
if (zsize == 3)
setalpha(base, xsize * ysize);
else if (zsize < 3)
copybw((Py_Int32 *) base, xsize * ysize);
}
finally:
free(starttab);
free(lengthtab);
free(rledat);
free(verdat);
fclose(inf);
return rv;
}
/* static utility functions for longimagedata */
static void
interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
{
lptr += z;
while (n--) {
*lptr = *cptr++;
lptr += 4;
}
}
static void
copybw(Py_Int32 *lptr, int n)
{
while (n >= 8) {
lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff));
lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff));
lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff));
lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff));
lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff));
lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff));
lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff));
lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff));
lptr += 8;
n -= 8;
}
while (n--) {
*lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
lptr++;
}
}
static void
setalpha(unsigned char *lptr, int n)
{
while (n >= 8) {
lptr[0 * 4] = 0xff;
lptr[1 * 4] = 0xff;
lptr[2 * 4] = 0xff;
lptr[3 * 4] = 0xff;
lptr[4 * 4] = 0xff;
lptr[5 * 4] = 0xff;
lptr[6 * 4] = 0xff;
lptr[7 * 4] = 0xff;
lptr += 4 * 8;
n -= 8;
}
while (n--) {
*lptr = 0xff;
lptr += 4;
}
}
static void
expandrow(unsigned char *optr, unsigned char *iptr, int z)
{
unsigned char pixel, count;
optr += z;
while (1) {
pixel = *iptr++;
if (!(count = (pixel & 0x7f)))
return;
if (pixel & 0x80) {
while (count >= 8) {
optr[0 * 4] = iptr[0];
optr[1 * 4] = iptr[1];
optr[2 * 4] = iptr[2];
optr[3 * 4] = iptr[3];
optr[4 * 4] = iptr[4];
optr[5 * 4] = iptr[5];
optr[6 * 4] = iptr[6];
optr[7 * 4] = iptr[7];
optr += 8 * 4;
iptr += 8;
count -= 8;
}
while (count--) {
*optr = *iptr++;
optr += 4;
}
}
else {
pixel = *iptr++;
while (count >= 8) {
optr[0 * 4] = pixel;
optr[1 * 4] = pixel;
optr[2 * 4] = pixel;
optr[3 * 4] = pixel;
optr[4 * 4] = pixel;
optr[5 * 4] = pixel;
optr[6 * 4] = pixel;
optr[7 * 4] = pixel;
optr += 8 * 4;
count -= 8;
}
while (count--) {
*optr = pixel;
optr += 4;
}
}
}
}
/*
* longstoimage -
* copy an array of longs to an iris image file. Each long
* represents one pixel. xsize and ysize specify the dimensions of
* the pixel array. zsize specifies what kind of image file to
* write out. if zsize is 1, the luminance of the pixels are
* calculated, and a single channel black and white image is saved.
* If zsize is 3, an RGB image file is saved. If zsize is 4, an
* RGBA image file is saved.
*
*/
static PyObject *
longstoimage(PyObject *self, PyObject *args)
{
unsigned char *lptr;
char *name;
int xsize, ysize, zsize;
FILE *outf = NULL;
IMAGE image;
int tablen, y, z, pos, len;
Py_Int32 *starttab = NULL, *lengthtab = NULL;
unsigned char *rlebuf = NULL;
unsigned char *lumbuf = NULL;
int rlebuflen, goodwrite;
PyObject *retval = NULL;
if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize,
&name))
return NULL;
goodwrite = 1;
outf = fopen(name, "wb");
if (!outf) {
PyErr_SetString(ImgfileError, "can't open output file");
return NULL;
}
tablen = ysize * zsize * sizeof(Py_Int32);
starttab = (Py_Int32 *)malloc(tablen);
lengthtab = (Py_Int32 *)malloc(tablen);
rlebuflen = (int) (1.05 * xsize + 10);
rlebuf = (unsigned char *)malloc(rlebuflen);
lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
PyErr_NoMemory();
goto finally;
}
memset(&image, 0, sizeof(IMAGE));
image.imagic = IMAGIC;
image.type = RLE(1);
if (zsize>1)
image.dim = 3;
else
image.dim = 2;
image.xsize = xsize;
image.ysize = ysize;
image.zsize = zsize;
image.min = 0;
image.max = 255;
goodwrite *= writeheader(outf, &image);
pos = 512 + 2 * tablen;
fseek(outf, pos, SEEK_SET);
if (reverse_order)
lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
for (y = 0; y < ysize; y++) {
for (z = 0; z < zsize; z++) {
if (zsize == 1) {
lumrow(lptr, lumbuf, xsize);
len = compressrow(lumbuf, rlebuf,
CHANOFFSET(z), xsize);
} else {
len = compressrow(lptr, rlebuf,
CHANOFFSET(z), xsize);
}
if(len > rlebuflen) {
PyErr_SetString(ImgfileError,
"rlebuf is too small");
goto finally;
}
goodwrite *= fwrite(rlebuf, len, 1, outf);
starttab[y + z * ysize] = pos;
lengthtab[y + z * ysize] = len;
pos += len;
}
if (reverse_order)
lptr -= xsize * sizeof(Py_UInt32);
else
lptr += xsize * sizeof(Py_UInt32);
}
fseek(outf, 512, SEEK_SET);
goodwrite *= writetab(outf, starttab, ysize*zsize);
goodwrite *= writetab(outf, lengthtab, ysize*zsize);
if (goodwrite) {
Py_INCREF(Py_None);
retval = Py_None;
} else
PyErr_SetString(ImgfileError, "not enough space for image");
finally:
fclose(outf);
free(starttab);
free(lengthtab);
free(rlebuf);
free(lumbuf);
return retval;
}
/* static utility functions for longstoimage */
static void
lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
{
lumptr += CHANOFFSET(0);
while (n--) {
*lumptr = ILUM(rgbptr[OFFSET_R],
rgbptr[OFFSET_G],
rgbptr[OFFSET_B]);
lumptr += 4;
rgbptr += 4;
}
}
static int
compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
{
unsigned char *iptr, *ibufend, *sptr, *optr;
short todo, cc;
Py_Int32 count;
lbuf += z;
iptr = lbuf;
ibufend = iptr + cnt * 4;
optr = rlebuf;
while(iptr < ibufend) {
sptr = iptr;
iptr += 8;
while ((iptr<ibufend) &&
((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
{
iptr += 4;
}
iptr -= 8;
count = (iptr - sptr) / 4;
while (count) {
todo = count > 126 ? 126 : (short)count;
count -= todo;
*optr++ = 0x80 | todo;
while (todo > 8) {
optr[0] = sptr[0 * 4];
optr[1] = sptr[1 * 4];
optr[2] = sptr[2 * 4];
optr[3] = sptr[3 * 4];
optr[4] = sptr[4 * 4];
optr[5] = sptr[5 * 4];
optr[6] = sptr[6 * 4];
optr[7] = sptr[7 * 4];
optr += 8;
sptr += 8 * 4;
todo -= 8;
}
while (todo--) {
*optr++ = *sptr;
sptr += 4;
}
}
sptr = iptr;
cc = *iptr;
iptr += 4;
while ((iptr < ibufend) && (*iptr == cc))
iptr += 4;
count = (iptr - sptr) / 4;
while (count) {
todo = count > 126 ? 126 : (short)count;
count -= todo;
*optr++ = (unsigned char) todo;
*optr++ = (unsigned char) cc;
}
}
*optr++ = 0;
return optr - (unsigned char *)rlebuf;
}
static PyObject *
ttob(PyObject *self, PyObject *args)
{
int order, oldorder;
if (!PyArg_Parse(args, "i", &order))
return NULL;
oldorder = reverse_order;
reverse_order = order;
return PyInt_FromLong(oldorder);
}
static PyMethodDef
rgbimg_methods[] = {
{"sizeofimage", sizeofimage},
{"longimagedata", longimagedata},
{"longstoimage", longstoimage},
{"ttob", ttob},
{NULL, NULL} /* sentinel */
};
DL_EXPORT(void)
initrgbimg(void)
{
PyObject *m, *d;
m = Py_InitModule("rgbimg", rgbimg_methods);
d = PyModule_GetDict(m);
ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
if (ImgfileError != NULL)
PyDict_SetItemString(d, "error", ImgfileError);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -