AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

2004.10.21 18:37 "[Tiff] Read and modify an image", by Elzumba Sefue

Hello, I have a little problem (little?)... I get an exception of "unknow software exception"... The code is below, I try to read an compressed tiff and simply copy them in other place (for test)... but I have problems with some tags, and I fix it in a "bestial" mode (puting some tags manually). The error is when I try to "TIFFWriteEncodedStrip". I try (before) with scanlines, but I'm received the notice that isn't util for compressed images. If somebody know to fix the problem or other form to do this, I will very happy, thanks. I need exactly read an image until a black point has founded, and from this place, create a new image (this is that want the code below), but I'm happy if somebody give me an example to read and write an image using encoded strips.

PD: Sorry for the strings of the code are in spanish, but isn't important.

int FuncReadWrite(char *strFileName) {
    TIFF *tif;
    TIFF *tifout;
    unsigned long nbps,compresion,photo,pc,ru;
    char flag=0;
    tsize_t stripSize;
    unsigned long result;
    int stripMax, stripCount, cStrip, desdeStrip=0, desdeByte=0, i, NewByte=0;
    char **buffer;
    char **NewImg;
    unsigned long bufferSize, count, count2;
    uint32 width, bloque;

    printf("\nBP1");
    if ((tif = TIFFOpen(strFileName, "r")) == NULL) {
        fprintf(stderr, "No se pudo abrir %s como un archivo TIFF\n", strFileName);
        exit(0);
    }
    printf("\nBP2");
    if ((tifout = TIFFOpen("C:\\2.tif", "w")) == NULL)
{
        fprintf(stderr, "No se pudo abrir ni crear %s como un archivo TIFF\n", strFileName);
        exit(0);
    }
    printf("\nBP3");
    
    TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &nbps);
    TIFFGetField(tif, TIFFTAG_COMPRESSION, &compresion);
    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo);
    TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &pc);
    TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &ru);
    
    if(TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width) == 0){
        fprintf(stderr, "Image does not define its width\n");
        exit(42);
    }
    
    /*TIFFSetField(tifout, TIFFTAG_BITSPERSAMPLE, nbps);
    TIFFSetField(tifout, TIFFTAG_COMPRESSION, compresion);
    TIFFSetField(tifout, TIFFTAG_PHOTOMETRIC, photo);
    TIFFSetField(tifout, TIFFTAG_RESOLUTIONUNIT, ru);*/

    stripSize = TIFFStripSize (tif);
    stripMax = TIFFNumberOfStrips (tif);
    
    buffer = (char **) malloc(stripMax);
    for(i=0;i<stripMax;i++) {
        if((buffer[i]= (char *) malloc(stripSize)) == NULL) {
          printf("\nNo alcanza la memoria (malloc).");
          exit(42);
        }
    }
    for (stripCount = 0; stripCount < stripMax; stripCount++){
        if((result = TIFFReadEncodedStrip (tif, stripCount, buffer[stripCount], stripSize)) == -1){
          printf("\nError leyendo el strip (tira) Nº %d\nIMPORTANTE: LA IMAGEN PUDO HABERSE DEFORMADO.", stripCount);
          exit(42);
        }
        //I search the black point (I'm not very sure that is correct)
        if(!flag) {
            for(cStrip=0;cStrip<stripSize;cStrip++) {
              if(buffer[stripCount][cStrip]==0x000000) {
                desdeStrip=stripMax - stripCount;
                desdeByte=stripSize - cStrip;
                bloque=width-desdeByte;
                flag=1;
              }
            }
        }
    }
    
    //free space for the new image
    NewImg = (char **) malloc(stripMax);
    for(i=0;i<stripMax;i++) {
        if((NewImg[i]= (char *) malloc(stripSize)) == NULL) {
          printf("\nNo alcanza la memoria (malloc).");
          exit(42);
        }
    }

    //Create new image in memory
    for (stripCount = 0; stripCount < stripMax; stripCount++){
        //Si todavia no se encontro, busco el primer punto negro
        for(cStrip=0;cStrip<stripSize;cStrip++) {
          if(cStrip>desdeByte)
              NewImg[stripCount][NewByte]=buffer[stripCount][cStrip];
          if(cStrip!=0 && (cStrip%width==0)) {
              cStrip+=desdeByte;
          }
          NewByte++;
        }
    }
    
    //width and PLANNAR CONF
    TIFFSetField(tifout, TIFFTAG_IMAGEWIDTH, stripSize);
    TIFFSetField(tifout, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

    for (stripCount=0;stripCount<stripMax;stripCount++) {
        if(TIFFWriteEncodedStrip(tifout,1,buffer[stripCount],stripSize)==-1)
        printf("\nError en escritura del Strip Nº %d",bloque);
    }
    for(i=0;i<stripMax;i++) {
        free(buffer[i]);
    }
    for(i=0;i<stripMax;i++) {
        free(NewImg[i]);
    }
    free(buffer);
    free(NewImg);
    TIFFClose(tif);
    TIFFClose(tifout);
}