Exemple #1
0
int FileReadTIF(Tcl_Interp *interp,
		Tcl_Channel chan,
		CONST char *fileName,
		Tcl_Obj *format,
		Tk_PhotoHandle imageHandle,
		int destX, int destY,
		int width, int height,
		int srcX, int srcY)
{
    int nBytes, h;
    int nLines;                   /* actual number of scan lines read
                                   * for each image block pointed at
                                   * by pixelPtr */
    int nDefBlockLines;           /* default number of scan lines to be read
                                   * for each image block pointed at
                                   * by pixelPtr */
    unsigned char *pixelPtr;
    Tk_PhotoImageBlock block;
    unsigned long fileImgHeight, fileImgWidth;
    unsigned short bitspersample;
    unsigned short photometrictype;

    short n;
    unsigned long row;
    unsigned long startrow;
    unsigned long pixelPtrIndex;
    TIFF *tif;
    TIFFErrorHandler prev_handler; /* Current TIFF error handler  */
    static TIFFErrorHandler temp_handler = tkTIFFErrorHandler;
                                   /* Temporary TIFF error handler
                                      (is used to avoid output of
                                       libtiff error messages to stderr) */

    prev_handler = TIFFSetErrorHandler(temp_handler);

    tif = TIFFOpen(fileName, "r");
    if (tif == NULL) {
        printf("FileReadTIF: Problem reading TIF file !");
	Tcl_AppendResult(interp, "couldn't read TIF header from file \"",
		fileName, "\"", NULL);
        TIFFSetErrorHandler(prev_handler);
        return TCL_ERROR;
    }

    /* test entry !!! */
    /*
    printf("\nFileReadTIF: file name     = %s", fileName);
    printf("\nFileReadTIF: height        = %d", height);
    printf("\nFileReadTIF: width         = %d", width);
    */
    /* obtain dimensions of (first) image in TIFF file */
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &fileImgHeight);
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &fileImgWidth);
    /* test entry !!! */
    /*
     printf("\nFileReadTIF: image height  = %d", fileImgHeight);
     printf("\nFileReadTIF: image width   = %d\n", fileImgWidth);
    */


    if ((fileImgWidth <= 0) || (fileImgHeight <= 0)) {
	Tcl_AppendResult(interp, "TIF image file \"", fileName,
		"\" has dimension(s) <= 0", (char *) NULL);
	return TCL_ERROR;
    }

    if ((srcX + width) > (int)fileImgWidth) {
	width = fileImgWidth - srcX;
    }
    if ((srcY + height) > (int)fileImgHeight) {
	height = fileImgHeight - srcY;
    }
    if ((width <= 0) || (height <= 0)
	|| (srcX >= (int)fileImgWidth) || (srcY >= (int)fileImgHeight)) {
	return TCL_OK;
    }


    /*
       determine type of TIFF image: PhotometricInterpretation
       WhiteIsZero       => 0 (bw or grayscale image)
       BlackIsZero       => 1 (bw or grayscale image)
       RGB Model         => 2 (RGB full color model)
       Color Palette     => 3
       Transparency Mask => 4
    */
    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometrictype);
    /* test entry !!! */
    if ((photometrictype == 0) || (photometrictype == 1) || (photometrictype == 3)) {
        /*
           bw or grayscale image
        */
        TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
        /* test entry !!! */
	/*
        printf("\nFileReadTIF: bits / sample = %d", bitspersample);
	*/
        block.pixelSize = 1;
        block.offset[0] = 0;
        block.offset[1] = 0;
        block.offset[2] = 0;
    } else if (photometrictype == 2) {
        /*
           RGB full color image

           note: - bitspersample is always = 8
                 - there is no color map
        */
        block.pixelSize = 3;
        block.offset[0] = 0;
        block.offset[1] = 1;
        block.offset[2] = 2;
    } else {
        Tcl_AppendResult(interp, "photometric interpretation >2 ",
            "(palette color and transparency mask) ",
            "not yet supported", NULL);
        TIFFSetErrorHandler(prev_handler);
        return TCL_ERROR;
    }

    /* block.width = width; */
    height = fileImgHeight;
    width = fileImgWidth;
    block.width = fileImgWidth;
    block.pitch = block.pixelSize * fileImgWidth;
    /* test entry !!! */
    /*
    printf("\nFileReadTIF: height        = %d", height);
    printf("\nFileReadTIF: width         = %d", width);
    printf("\nFileReadTIF: fileImgWidth         = %d", fileImgWidth);
    */
    /* the following two values should be identical */
    /*
    printf("\nFileReadTIF: block.pitch     = %d", block.pitch);
    printf("\nFileReadTIF: block.pixelSize = %d", block.pixelSize);
    printf("\nFileReadTIF: scanline size   = %d", TIFFScanlineSize(tif));
    */
    Tk_PhotoExpand(imageHandle, destX + width, destY + height);

    /*
    if (srcY > 0) {
	fseek(f, (long) (srcY * block.pitch), SEEK_CUR);
    }
    */

    nDefBlockLines = (MAX_MEMORY + block.pitch - 1) / block.pitch;
    nLines = nDefBlockLines;
    nLines = height;
    if (nLines > height) {
        nLines = height;
    }
    if (nLines <= 0) {
        nLines = 1;
    }

    /* determine the required buffer size by calling TIFFScanlineSize */
    nBytes = nLines * block.pitch;
    /*
    printf("\nFileReadTIF: nLines        = %d", nLines);
    printf("\nFileReadTIF: nBytes        = %d", nBytes);
    */
    pixelPtr = (unsigned char *) ckalloc((unsigned)nBytes);
    block.pixelPtr = pixelPtr + srcX * block.pixelSize;

    n = 0;
    for (h = height; h > 0; h -= nLines) {
        n++;
        startrow = (n-1) * nLines;
        if (nLines > h) {
            nLines = h;
            nBytes = nLines * block.pitch;
        }
        for (row = startrow; row < (startrow + nLines); row++) {
            /* read the data for the spezified row into the buffer */
            pixelPtrIndex = row*block.pitch - (n-1)*nDefBlockLines*block.pitch;
            /* test entry !!! */
            /* printf("\nFileReadTIF: pixelPtr_Indx = %d", pixelPtrIndex); */
            TIFFReadScanline(tif, &(pixelPtr[pixelPtrIndex]), row, 1);
        }
	block.height = nLines;

	Tk_PhotoPutBlock(imageHandle, &block, destX, destY, width, nLines, TK_PHOTO_COMPOSITE_SET);
	destY += nLines;
    }


    ckfree((char *) pixelPtr);

    TIFFClose(tif);
    /* test entry !!! */
    /*
    printf("\nFileReadTIF: TIF file closed !");
    */
    TIFFSetErrorHandler(prev_handler);

    return TCL_OK;



}
Exemple #2
0
static int
RasterizeSVG(
    Tcl_Interp *interp,
    Tk_PhotoHandle imageHandle,
    NSVGimage *nsvgImage,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY,
    RastOpts *ropts)
{
    int w, h, c;
    NSVGrasterizer *rast;
    unsigned char *imgData;
    Tk_PhotoImageBlock svgblock;

    w = (int) ceil(nsvgImage->width * ropts->scale);
    h = (int) ceil(nsvgImage->height * ropts->scale);
    rast = nsvgCreateRasterizer();
    if (rast == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot initialize rasterizer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "RASTERIZER_ERROR",
		NULL);
	goto cleanAST;
    }
    imgData = attemptckalloc(w * h *4);
    if (imgData == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot alloc image buffer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "OUT_OF_MEMORY", NULL);
	goto cleanRAST;
    }
    nsvgRasterize(rast, nsvgImage, (float) ropts->x, (float) ropts->y,
	    (float) ropts->scale, imgData, w, h, w * 4);
    /* transfer the data to a photo block */
    svgblock.pixelPtr = imgData;
    svgblock.width = w;
    svgblock.height = h;
    svgblock.pitch = w * 4;
    svgblock.pixelSize = 4;
    for (c = 0; c <= 3; c++) {
	svgblock.offset[c] = c;
    }
    if (Tk_PhotoExpand(interp, imageHandle,
		destX + width, destY + height) != TCL_OK) {
	goto cleanRAST;
    }
    if (Tk_PhotoPutBlock(interp, imageHandle, &svgblock, destX, destY,
		width, height, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	goto cleanimg;
    }
    ckfree(imgData);
    nsvgDeleteRasterizer(rast);
    nsvgDelete(nsvgImage);
    return TCL_OK;

cleanimg:
    ckfree(imgData);

cleanRAST:
    nsvgDeleteRasterizer(rast);

cleanAST:
    nsvgDelete(nsvgImage);
    return TCL_ERROR;
}
Exemple #3
0
static int
StringReadPPM(
    Tcl_Interp *interp,		/* Interpreter to use for reporting errors. */
    Tcl_Obj *dataObj,		/* The image data. */
    Tcl_Obj *format,		/* User-specified format string, or NULL. */
    Tk_PhotoHandle imageHandle,	/* The photo image to write into. */
    int destX, int destY,	/* Coordinates of top-left pixel in photo
				 * image to be written to. */
    int width, int height,	/* Dimensions of block of photo image to be
				 * written to. */
    int srcX, int srcY)		/* Coordinates of top-left pixel to be used in
				 * image being read. */
{
    int fileWidth, fileHeight, maxIntensity;
    int nLines, nBytes, h, type, count, dataSize;
    unsigned char *pixelPtr, *dataBuffer;
    Tk_PhotoImageBlock block;

    type = ReadPPMStringHeader(dataObj, &fileWidth, &fileHeight,
	    &maxIntensity, &dataBuffer, &dataSize);
    if (type == 0) {
	Tcl_AppendResult(interp, "couldn't read raw PPM header from string",
		NULL);
	return TCL_ERROR;
    }
    if ((fileWidth <= 0) || (fileHeight <= 0)) {
	Tcl_AppendResult(interp, "PPM image data has dimension(s) <= 0",
		NULL);
	return TCL_ERROR;
    }
    if ((maxIntensity <= 0) || (maxIntensity >= 256)) {
	char buffer[TCL_INTEGER_SPACE];

	sprintf(buffer, "%d", maxIntensity);
	Tcl_AppendResult(interp,
		"PPM image data has bad maximum intensity value ", buffer,
		NULL);
	return TCL_ERROR;
    }

    if ((srcX + width) > fileWidth) {
	width = fileWidth - srcX;
    }
    if ((srcY + height) > fileHeight) {
	height = fileHeight - srcY;
    }
    if ((width <= 0) || (height <= 0)
	|| (srcX >= fileWidth) || (srcY >= fileHeight)) {
	return TCL_OK;
    }

    if (type == PGM) {
	block.pixelSize = 1;
	block.offset[0] = 0;
	block.offset[1] = 0;
	block.offset[2] = 0;
    } else {
	block.pixelSize = 3;
	block.offset[0] = 0;
	block.offset[1] = 1;
	block.offset[2] = 2;
    }
    block.offset[3] = 0;
    block.width = width;
    block.pitch = block.pixelSize * fileWidth;

    if (srcY > 0) {
	dataBuffer += srcY * block.pitch;
	dataSize -= srcY * block.pitch;
    }

    if (maxIntensity == 255) {
	/*
	 * We have all the data in memory, so write everything in one go.
	 */

	if (block.pitch*height > dataSize) {
	    Tcl_AppendResult(interp, "truncated PPM data", NULL);
	    return TCL_ERROR;
	}
	block.pixelPtr = dataBuffer + srcX * block.pixelSize;
	block.height = height;
	return Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY,
		width, height, TK_PHOTO_COMPOSITE_SET);
    }

    if (Tk_PhotoExpand(interp, imageHandle,
	    destX + width, destY + height) != TCL_OK) {
	return TCL_ERROR;
    }

    nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch;
    if (nLines > height) {
	nLines = height;
    }
    if (nLines <= 0) {
	nLines = 1;
    }
    nBytes = nLines * block.pitch;
    pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes);
    block.pixelPtr = pixelPtr + srcX * block.pixelSize;

    for (h = height; h > 0; h -= nLines) {
	unsigned char *p;

	if (nLines > h) {
	    nLines = h;
	    nBytes = nLines * block.pitch;
	}
	if (dataSize < nBytes) {
	    ckfree((char *) pixelPtr);
	    Tcl_AppendResult(interp, "truncated PPM data", NULL);
	    return TCL_ERROR;
	}
	for (p=pixelPtr,count=nBytes ; count>0 ; count--,p++,dataBuffer++) {
	    *p = (((int) *dataBuffer) * 255)/maxIntensity;
	}
	dataSize -= nBytes;
	block.height = nLines;
	if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY,
		width, nLines, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	    ckfree((char *) pixelPtr);
	    return TCL_ERROR;
	}
	destY += nLines;
    }

    ckfree((char *) pixelPtr);
    return TCL_OK;
}
Exemple #4
0
static int
StringReadNAO(
    Tcl_Interp		*interp,
    Tcl_Obj		*nap_expr,	/* string containing NAP expression */
    Tcl_Obj		*format,	/* format string not used */
    Tk_PhotoHandle	imageHandle,
    int			destX,
    int			destY,
    int			width,
    int			height,
    int			srcX,
    int			srcY)
{
    int			naoWidth;
    int			naoHeight;
    int			naoDepth;
    Nap_NAO		*naoPtr;
    Nap_NAO		*naoPtr0;
    int			rank;
    Tk_PhotoImageBlock	block;
    NapClientData	*nap_cd = Nap_GetClientData(interp);

    naoPtr0 = Nap_GetNaoFromObj(nap_cd, nap_expr);
    CHECK2(naoPtr0, "StringReadNAO: error calling Nap_GetNaoFromObj");
    Nap_IncrRefCount(nap_cd, naoPtr0);
    naoPtr = Nap_CastNAO(nap_cd, naoPtr0, NAP_U8);
    Nap_IncrRefCount(nap_cd, naoPtr);
    Nap_DecrRefCount(nap_cd, naoPtr0);
    rank = naoPtr->rank;
    switch (rank) {
    case 2:
        naoWidth = naoPtr->shape[1]; 
        naoHeight = naoPtr->shape[0]; 
	break;
    case 3:
        naoWidth = naoPtr->shape[2]; 
        naoHeight = naoPtr->shape[1]; 
        naoDepth = naoPtr->shape[0];
	break;
    default:
	CHECK2(0, "StringReadNAO: Rank is not 1 or 2");
    }

/*
 * Setup the dimensions for the display
 */

    if ((srcX + width) > naoWidth) {
        width = naoWidth - srcX;
    }  
    if ((srcY + height) > naoHeight) {
        height = naoHeight - srcY;
    }
    if ((width <= 0) || (height <= 0)
            || (srcX >= naoWidth) || (srcY >= naoHeight)) {
        return TCL_OK;
    }

/*
 * Get the output window open to the correct size
 */

    Tk_PhotoExpand(imageHandle, destX + width, destY + height);


/*
 * setup the supporting image data structure
 */

    block.width = naoWidth;
    block.height = naoHeight;
    block.pitch = naoWidth;     /* Address difference between corresponding
                                 * pixels in successive lines */
    block.pixelSize = 1;	/* Address difference successive pixels
                                 * in the same line */
      /* Address differences between the red, green
                                 * and blue components of the pixel and the
                                 * pixel as a whole. */

    block.offset[0] = 0;  
    block.offset[1] = 0;
    block.offset[2] = 0;
    if (rank == 3) {
        if(naoDepth >= 3) {
            block.offset[1] = naoHeight*naoWidth;
            block.offset[2] = 2 * naoWidth*naoHeight;
        } else if(naoDepth == 2) {
            block.offset[2] =  naoWidth*naoHeight;
        }
    }   

    /* Pointer to the first part of the first pixel */
    block.pixelPtr = (unsigned char *) naoPtr->data.U8;

    Tk_PhotoPutBlock(imageHandle, &block, destX, destY, naoWidth, naoHeight,
	    TK_PHOTO_COMPOSITE_OVERLAY);
    Nap_DecrRefCount(nap_cd, naoPtr);
    return TCL_OK;
}
Exemple #5
0
static int
FileReadPPM(
    Tcl_Interp *interp,		/* Interpreter to use for reporting errors. */
    Tcl_Channel chan,		/* The image file, open for reading. */
    CONST char *fileName,	/* The name of the image file. */
    Tcl_Obj *format,		/* User-specified format string, or NULL. */
    Tk_PhotoHandle imageHandle,	/* The photo image to write into. */
    int destX, int destY,	/* Coordinates of top-left pixel in photo
				 * image to be written to. */
    int width, int height,	/* Dimensions of block of photo image to be
				 * written to. */
    int srcX, int srcY)		/* Coordinates of top-left pixel to be used in
				 * image being read. */
{
    int fileWidth, fileHeight, maxIntensity;
    int nLines, nBytes, h, type, count;
    unsigned char *pixelPtr;
    Tk_PhotoImageBlock block;

    type = ReadPPMFileHeader(chan, &fileWidth, &fileHeight, &maxIntensity);
    if (type == 0) {
	Tcl_AppendResult(interp, "couldn't read raw PPM header from file \"",
		fileName, "\"", NULL);
	return TCL_ERROR;
    }
    if ((fileWidth <= 0) || (fileHeight <= 0)) {
	Tcl_AppendResult(interp, "PPM image file \"", fileName,
		"\" has dimension(s) <= 0", NULL);
	return TCL_ERROR;
    }
    if ((maxIntensity <= 0) || (maxIntensity >= 256)) {
	char buffer[TCL_INTEGER_SPACE];

	sprintf(buffer, "%d", maxIntensity);
	Tcl_AppendResult(interp, "PPM image file \"", fileName,
		"\" has bad maximum intensity value ", buffer, NULL);
	return TCL_ERROR;
    }

    if ((srcX + width) > fileWidth) {
	width = fileWidth - srcX;
    }
    if ((srcY + height) > fileHeight) {
	height = fileHeight - srcY;
    }
    if ((width <= 0) || (height <= 0)
	|| (srcX >= fileWidth) || (srcY >= fileHeight)) {
	return TCL_OK;
    }

    if (type == PGM) {
	block.pixelSize = 1;
	block.offset[0] = 0;
	block.offset[1] = 0;
	block.offset[2] = 0;
    } else {
	block.pixelSize = 3;
	block.offset[0] = 0;
	block.offset[1] = 1;
	block.offset[2] = 2;
    }
    block.offset[3] = 0;
    block.width = width;
    block.pitch = block.pixelSize * fileWidth;

    if (Tk_PhotoExpand(interp, imageHandle,
	    destX + width, destY + height) != TCL_OK) {
	return TCL_ERROR;
    }

    if (srcY > 0) {
	Tcl_Seek(chan, (Tcl_WideInt)(srcY * block.pitch), SEEK_CUR);
    }

    nLines = (MAX_MEMORY + block.pitch - 1) / block.pitch;
    if (nLines > height) {
	nLines = height;
    }
    if (nLines <= 0) {
	nLines = 1;
    }
    nBytes = nLines * block.pitch;
    pixelPtr = (unsigned char *) ckalloc((unsigned) nBytes);
    block.pixelPtr = pixelPtr + srcX * block.pixelSize;

    for (h = height; h > 0; h -= nLines) {
	if (nLines > h) {
	    nLines = h;
	    nBytes = nLines * block.pitch;
	}
	count = Tcl_Read(chan, (char *) pixelPtr, nBytes);
	if (count != nBytes) {
	    Tcl_AppendResult(interp, "error reading PPM image file \"",
		    fileName, "\": ",
		    Tcl_Eof(chan) ? "not enough data" : Tcl_PosixError(interp),
		    NULL);
	    ckfree((char *) pixelPtr);
	    return TCL_ERROR;
	}
	if (maxIntensity != 255) {
	    unsigned char *p;

	    for (p = pixelPtr; count > 0; count--, p++) {
		*p = (((int) *p) * 255)/maxIntensity;
	    }
	}
	block.height = nLines;
	if (Tk_PhotoPutBlock(interp, imageHandle, &block, destX, destY,
		width, nLines, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	    ckfree((char *) pixelPtr);
	    return TCL_ERROR;
	}
	destY += nLines;
    }

    ckfree((char *) pixelPtr);
    return TCL_OK;
}