frame->leftX = hotx; frame->leftY = hoty;
frame->leftX = hotx - offsetX; frame->leftY = hoty - offsetY;
static gint32 save_image (gchar  *filename,	    gint32  image_ID,	    gint32  drawable_ID,	    gint32  orig_image_ID){ 	FILE *fp;	gint32 shape_size;	gint32 hdr_size;	gint32 max_width;	gint32 max_height;	gint offsetX;	gint offsetY;	gchar *name_buf;	GimpDrawable *drawable;	GimpPixelRgn pixel_rgn;	guchar *pixptr;	guchar *pix;	guchar *out;	guchar *outptr;	int i;	int j;	int k;	int newx;	int x;	int y;	int hotx;	int hoty;	gint32 guide_ID;	gint32 *layers;   	int nlayers;	int pos;	struct u7shape shape;	struct u7frame *frame;  	if (run_mode != GIMP_RUN_NONINTERACTIVE) {		name_buf = g_strdup_printf ("Saving %s:", filename);		gimp_progress_init (name_buf);		g_free (name_buf);	}		/* Find the guides... */	guide_ID = gimp_image_find_next_guide(image_ID, 0);	hotx = -1;	hoty = -1;	while(guide_ID>0) {		int orientation;		printf("Found guide %d:", guide_ID);		orientation = gimp_image_get_guide_orientation(image_ID, guide_ID);		switch(orientation) {		case GIMP_HORIZONTAL:			if(hoty<0) {				hoty = gimp_image_get_guide_position(image_ID, guide_ID);				printf(", horizontal=%d\n", hoty);			}			break;		case GIMP_VERTICAL:			if(hotx<0) {				hotx = gimp_image_get_guide_position(image_ID, guide_ID);				printf(", vertical=%d\n", hotx);			}			break;		default:			break;		}		guide_ID = gimp_image_find_next_guide(image_ID, guide_ID);	}		/* get a list of layers for this image_ID */	layers = gimp_image_get_layers (image_ID, &nlayers);  	max_width = gimp_image_width (image_ID);	max_height = gimp_image_height (image_ID);	shape.num_frames = nlayers;	hdr_size = (nlayers+1)*4;	shape.frames = (struct u7frame *)malloc(sizeof(struct u7frame)*shape.num_frames);	for (k=0;k<nlayers;k++) {		frame = &shape.frames[k];		drawable = gimp_drawable_get (layers[k]);		//gimp_drawable_offsets (layers[k], &offsetX, &offsetY);		frame->width = drawable->width;		frame->height = drawable->height;		frame->leftX = hotx;		frame->leftY = hoty;		frame->rightX = frame->width-frame->leftX-1;		frame->rightY = frame->height-frame->leftY-1;		pix = (gchar *)malloc(frame->width*frame->height*2);		pixptr = pix;		out = (gchar *)malloc(frame->width*frame->height*8);		outptr = out;		gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,				     drawable->width, drawable->height, FALSE, FALSE);		gimp_pixel_rgn_get_rect (&pixel_rgn, pixptr, 0, 0,					 drawable->width, drawable->height);		newx = 0;		for (y = 0; y < frame->height; y++) {		    for (x = 0; (x = skip_transparent(&pixptr, x, frame->width)) < frame->width; x = newx) {			short runs[100];			newx = find_runs(runs, pixptr, x, frame->width);			if (!runs[1] && !(runs[0]&1)) {			    int len = runs[0] >> 1;			    outptr = out2(outptr, runs[0]);			    outptr = out2(outptr, x - frame->leftX);			    outptr = out2(outptr, y - frame->leftY);			    for(i=0; i<len; i++) {				*outptr++ = *pixptr;				pixptr +=2;			    }			} else {			    outptr = out2(outptr, ((newx - x)<<1)|1);			    outptr = out2(outptr, x - frame->leftX);			    outptr = out2(outptr, y - frame->leftY);			    for (i = 0; runs[i]; i++) {				int len = runs[i]>>1;				if (runs[i]&1) {				    while (len) {					int c = len > 127 ? 127 : len;					*outptr++ = (c<<1)|1;					*outptr++ = *pixptr;					pixptr += (c*2);					len -= c;				    }				} else while (len > 0) {				    int c;				    c = len > 127 ? 127 : len;				    *outptr++ = c<<1;				    for(j=0; j<c; j++) {					*outptr++ = *pixptr;					pixptr +=2;				    }				    len -= c;				}			    }			}		    }		}		outptr = out2(outptr, 0);			// End with 0 length.		frame->datalen = outptr - out;		frame->pixels = (gchar *) malloc(frame->datalen);		memcpy(frame->pixels, out, frame->datalen);		free(out);		free(pix);		gimp_drawable_detach(drawable);	}	fp = fopen (filename, "wb");	if (!fp) {		g_message ("SHP: can't create \"%s\"\n", filename);		return -1;	}	shape_size = 0;	write4(fp, shape_size);	// Fill in later	for(i=0; i<shape.num_frames; i++) 	    write4(fp, 0);	// Fill in later	for(i=0; i<shape.num_frames; i++) {	    frame = &shape.frames[shape.num_frames-i-1];	    pos = ftell(fp);	// Get the frame offset	    fseek(fp, (i+1)*4, SEEK_SET);	    write4(fp, pos);	// Write it in the right place	    fseek(fp, pos, SEEK_SET);	    write2(fp, frame->rightX);	    write2(fp, frame->leftX);	    write2(fp, frame->leftY);	    write2(fp, frame->rightY);	    fwrite(frame->pixels, 1, frame->datalen, fp);	}	pos = ftell(fp);	// Get the file size	fseek(fp, 0, SEEK_SET);	write4(fp, pos);	// Write it in the right place	fclose(fp);		for(i=0; i<shape.num_frames; i++) {	    free(shape.frames[i].pixels);	}	free(shape.frames);	return 0;}