static Boln readHeaderLine (Tcl_Interp *interp, tkimg_MFile *handle, char *buf) { char c, *bufPtr, *bufEndPtr; Boln failure; bufPtr = buf; bufEndPtr = buf + HEADLEN; failure = TRUE; #ifdef DEBUG_LOCAL printf ("readHeaderLine\n"); fflush (stdout); #endif while (tkimg_Read (handle, &c, 1) == 1 && bufPtr < bufEndPtr) { if (c == '\n') { *bufPtr = '\0'; failure = FALSE; break; } *bufPtr = c; bufPtr++; } if (failure) { Tcl_AppendResult (interp, "RAW handler: Error reading header line (", buf, ")\n", NULL); return FALSE; } return TRUE; }
static Boln readRlePixel (Tcl_Interp *interp, tkimg_MFile *handle, UByte **pixBufPtr, Int *countPtr, Int stop, Int n, TGAFILE *tf) { Int i, count, nchan; UByte localBuf[4]; nchan = NCHAN(tf->th.pixsize); if (nchan != tkimg_Read (handle, (char *)localBuf, nchan)) return readError (interp); count = *countPtr; for (i=0; i<n; i++) { #if defined (DEBUG_LOCAL) tf->total++; #endif (*pixBufPtr)[0] = localBuf[2]; (*pixBufPtr)[1] = localBuf[1]; (*pixBufPtr)[2] = localBuf[0]; if (nchan == 4) (*pixBufPtr)[3] = localBuf[3]; (*pixBufPtr) += nchan; count++; if (count == stop) { /* Scanline is filled with pixel values. Determine the number of pixels to keep for next scanline. */ tf->scanrest = n - i - 1; *countPtr = count; return TRUE; } } *countPtr = count; return TRUE; }
static Boln readUShortRow (tkimg_MFile *handle, UShort *pixels, Int nShorts, char *buf, Boln swapBytes) { int i; UShort *mPtr = pixels; char *bufPtr = buf; #ifdef DEBUG_LOCAL printf ("Reading %d UShorts\n", nShorts); fflush (stdout); #endif if (2 * nShorts != tkimg_Read (handle, buf, 2 * nShorts)) return FALSE; if (swapBytes) { for (i=0; i<nShorts; i++) { ((char *)mPtr)[0] = bufPtr[1]; ((char *)mPtr)[1] = bufPtr[0]; mPtr++; bufPtr += 2; } } else { for (i=0; i<nShorts; i++) { ((char *)mPtr)[0] = bufPtr[0]; ((char *)mPtr)[1] = bufPtr[1]; mPtr++; bufPtr += 2; } } return TRUE; }
static Boln readFloatRow (tkimg_MFile *handle, Float *pixels, Int nFloats, char *buf, Boln swapBytes) { int i; Float *mPtr = pixels; char *bufPtr = buf; #ifdef DEBUG_LOCAL printf ("Reading %d floats\n", nFloats); fflush (stdout); #endif if (4 * nFloats != tkimg_Read (handle, buf, 4 * nFloats)) return FALSE; if (swapBytes) { for (i=0; i<nFloats; i++) { ((char *)mPtr)[0] = bufPtr[3]; ((char *)mPtr)[1] = bufPtr[2]; ((char *)mPtr)[2] = bufPtr[1]; ((char *)mPtr)[3] = bufPtr[0]; mPtr++; bufPtr += 4; } } else { for (i=0; i<nFloats; i++) { ((char *)mPtr)[0] = bufPtr[0]; ((char *)mPtr)[1] = bufPtr[1]; ((char *)mPtr)[2] = bufPtr[2]; ((char *)mPtr)[3] = bufPtr[3]; mPtr++; bufPtr += 4; } } return TRUE; }
static Boln readShort (tkimg_MFile *handle, Short *s) { char buf[2]; if (2 != tkimg_Read (handle, buf, 2)) return FALSE; *s = (buf[0] & 0xFF) | (buf[1] << 8); return TRUE; }
static Boln readUByte (tkimg_MFile *handle, UByte *b) { char buf[1]; if (1 != tkimg_Read (handle, buf, 1)) return FALSE; *b = buf[0]; return TRUE; }
static Boln readUByteRow (tkimg_MFile *handle, UByte *pixels, Int nBytes, char *buf, Boln swapBytes) { int i; UByte *mPtr = pixels; char *bufPtr = buf; #ifdef DEBUG_LOCAL printf ("Reading %d UBytes\n", nBytes); fflush (stdout); #endif if (nBytes != tkimg_Read (handle, buf, nBytes)) return FALSE; for (i=0; i<nBytes; i++) { ((char *)mPtr)[0] = bufPtr[0]; mPtr++; bufPtr += 1; } return TRUE; }
static Boln tgaReadScan (Tcl_Interp *interp, tkimg_MFile *handle, TGAFILE *tf, Int y) { Int nchan; Int count, stop; UByte localBuf[4]; UByte *pixBufPtr; count = 0; stop = tf->th.xsize; nchan = NCHAN(tf->th.pixsize); pixBufPtr = tf->pixbuf; #if defined (DEBUG_LOCAL) tf->total = 0; #endif if (IS_COMPRESSED (tf->th.imgtyp)) { Byte cbuf[1]; Int pix, numpix; /* While there are pixels left from the previous scanline, either fill the current scanline with the pixel value still stored in "pixbuf" (TGA_MODE_SAME) or read in the appropriate number of pixel values (TGA_MODE_DIFF). */ while (tf->scanrest) { if (tf->scanmode == TGA_MODE_DIFF) { if (nchan != tkimg_Read (handle, (char *)localBuf, nchan)) return readError (interp); } #if defined (DEBUG_LOCAL) tf->total++; #endif *pixBufPtr++ = localBuf[2]; *pixBufPtr++ = localBuf[1]; *pixBufPtr++ = localBuf[0]; if (nchan == 4) *pixBufPtr++ = localBuf[3]; count++; tf->scanrest--; /* If the image is small, the compression might go over several scanlines. */ if (count == stop) return TRUE; } /* Read the byte telling us the compression mode and the compression count. Then read the pixel values till a scanline is filled. */ do { if (1 != tkimg_Read (handle, cbuf, 1)) return readError (interp); numpix = (cbuf[0] & 0x7F) + 1; if ((cbuf[0] & 0x80) != 0x80) { tf->scanmode = TGA_MODE_DIFF; for (pix=0; pix<numpix; pix++) { if (!readRlePixel (interp, handle, &pixBufPtr, &count, stop, 1, tf)) return FALSE; if (count == stop) { tf->scanrest = numpix - pix - 1; break; } } } else { tf->scanmode = TGA_MODE_SAME; if (!readRlePixel (interp, handle, &pixBufPtr, &count, stop, numpix, tf)) return FALSE; } } while (count < stop); #if defined (DEBUG_LOCAL) printf ("\tScanline %d: Pixels: %d Rest: %d\n", y, tf->total, tf->scanrest); #endif } else { /* Read uncompressed pixel data. */ Int i, bytesPerLine; UByte curPix; bytesPerLine = nchan * tf->th.xsize; if (bytesPerLine != tkimg_Read (handle, (char *)tf->pixbuf, bytesPerLine)) return readError (interp); for (i=0; i<stop; i++) { curPix = pixBufPtr[2]; pixBufPtr[2] = pixBufPtr[0]; pixBufPtr[0] = curPix; pixBufPtr += nchan; } } return TRUE; }