rem: int channels = image->format == RRGBAFormat ? 4 : 3;
add: int channels = (image->format == RRGBAFormat ? 4 : 3);
context: image2GrayScale(RContext *ctx, RImage *image){    RXImage *ximg;    register int x, y, g;    unsigned char *ptr;    const int cpc=ctx->attribs->colors_per_channel;    unsigned short gmask;    unsigned short *table;    unsigned char *data;    int channels = image->format == RRGBAFormat ? 4 : 3;    /*register unsigned char maxrgb = 0xff;*/    ximg = RCreateXImage(ctx, ctx->depth, image->width, image->height);    if (!ximg) {	return NULL;    }    ptr = image->data;    data = (unsigned char *)ximg->image->data;    if (ctx->vclass == StaticGray)	gmask = (1<<ctx->depth) - 1; /* use all grays */    else	gmask  = cpc*cpc*cpc-1;    table = computeTable(gmask);    if (table==NULL) {	RErrorCode = RERR_NOMEMORY;        RDestroyXImage(ctx, ximg);        return NULL;    }    if (ctx->attribs->render_mode == RBestMatchRendering) {        /* fake match */#ifdef DEBUG        printf("grayscale match with %d colors per channel\n", cpc);#endif	for (y=0; y<image->height; y++) {	    for (x=0; x<image->width; x++) {                /* reduce pixel */                g = table[(*ptr * 30 + *(ptr+1) * 59 + *(ptr+2) * 11)/100];                ptr += 3;                /*data[ofs] = ctx->colors[g].pixel;*/                XPutPixel(ximg->image, x, y, ctx->colors[g].pixel);	    }	}    } else {	/* dither */	short *gerr;	short *ngerr;	short *terr;	int ger;	const int dg=0xff/gmask;#ifdef DEBUG        printf("grayscale dither with %d colors per channel\n", cpc);#endif	gerr = (short*)malloc((image->width+2)*sizeof(short));	ngerr = (short*)malloc((image->width+2)*sizeof(short));	if (!gerr || !ngerr) {	    if (ngerr)		free(ngerr);	    RErrorCode = RERR_NOMEMORY;	    RDestroyXImage(ctx, ximg);	    return NULL;	}	for (x=0; x<image->width; x++) {	    gerr[x] = (ptr[x*3]*30 + ptr[x*3+1]*59 + ptr[x*3+2]*11)/100;	}	gerr[x] = 0;	/* convert and dither the image to XImage */	for (y=0; y<image->height; y++) {	    if (y<image->height-1) {		int x1;		for (x=0, x1=(y+1)*image->width*3; x<image->width; x1+=channels) {		    ngerr[x] = (ptr[x1]*30 + ptr[x1+1]*59 + ptr[x1+2]*11)/100;		}		/* last column */		x1-=channels;		ngerr[x] = (ptr[x1]*30 + ptr[x1+1]*59 + ptr[x1+2]*11)/100;	    }	    for (x=0; x<image->width; x++) {		/* reduce pixel */                if (gerr[x]>0xff) gerr[x]=0xff; else if (gerr[x]<0) gerr[x]=0;                g = table[gerr[x]];                /*data[ofs] = ctx->colors[g].pixel;*/                XPutPixel(ximg->image, x, y, ctx->colors[g].pixel);		/* calc error */		ger = gerr[x] - g*dg;		/* distribute error */		g = (ger*3)/8;		/* x+1, y */		gerr[x+1]+=g;		/* x, y+1 */		ngerr[x]+=g;		/* x+1, y+1 */		ngerr[x+1]+=ger-2*g;	    }	    /* skip to next line */	    terr = gerr;	    gerr = ngerr;	    ngerr = terr;	}	free(gerr);	free(ngerr);    }    ximg->image->data = (char*)data;    return ximg;}