#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <magick/api.h>

int main(int argc,char **argv){
ExceptionInfo exception;
Image  *image, *images,*resize_image, *thumbnails;
ImageInfo *image_info;
PixelPacket foobar;
double x,y;
int i;
int rgb, lastcolor = -1, colspan = 0;

      /*
        Initialize the image info structure and read an image.
      */
      InitializeMagick(*argv);
      GetExceptionInfo(&exception);
      image_info=CloneImageInfo((ImageInfo *) NULL);
      (void) strcpy(image_info->filename,argv[1]);
      images=ReadImage(image_info,&exception);

      if (exception.severity != UndefinedException)
        CatchException(&exception);

      if (images == (Image *) NULL)
        exit(1);
 

	printf("<table cellspacing = 1 cellpadding=1 border=0 >");

	for(x = 0; x < images->columns; x++){
		printf("<tr>");
		for(y = 0; y < images->rows; y++){
			foobar = AcquireOnePixel(images, y,x, &exception );
			rgb = ((foobar.red << 8) & 0xff0000)  | ((foobar.green << 4) & 0x00ff00) | (foobar.blue & 0xff); 			 
			if(lastcolor != rgb){
				if(colspan > 1& (lastcolor != -1)){
					printf(" colspan=%d><td bgcolor=\"#%06x\" width=%u height=%u",colspan,(rgb & 0xffffff),1,1);
					colspan = 1;
				}else{
					if(lastcolor != -1) printf(">");					
					printf("<td bgcolor=\"#%06x\" width=%u height=%u" ,(rgb & 0xffffff),1,1);
				}
			}else{
				colspan++;
			}
			lastcolor = rgb;
		}
		
		lastcolor = -1;
		colspan = 1;
		printf("></td></tr>");
	}
	printf("</table>");

}
