Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TIFF reader bug when reading 32 bit unsigned monochrome image #18

Open
BenBradnick opened this issue Nov 29, 2022 · 0 comments
Open

TIFF reader bug when reading 32 bit unsigned monochrome image #18

BenBradnick opened this issue Nov 29, 2022 · 0 comments

Comments

@BenBradnick
Copy link

I have had success using this library for signed data. However I have a problem with unsigned data. I am using version v3.0.0.0 of the library on Windows 10.

I used the TIFF writer to write a 256x256 image with 32 bits per sample and using the TinyTIFFWriter_UInt format. I set the values of the pixels to x + y*256 where x is the column number and y is the row number.

The written file looks correct and I can check the values with a quick Python script using PIL and numpy (note that PIL uses column-major ordering):

>>> from PIL import Image
>>> import numpy
>>> def check_pixels(array):
...   for y in range(arr.shape[0]):
...     for x in range(arr.shape[1]):
...       expected_value = x + y*arr.shape[1]
...       if expected_value != arr[y,x]:
...         print(f"[{x},{y}] expected: {expected_value}, got: {arr[y][x]}")
...
>>> check_pixels(arr)
>>> image = Image.open(filepath)
>>> arr = numpy.array(image)
>>> check_pixels(arr)
>>> arr.shape
(256, 256)

However when using the TIFF reader I get some peculiar data at specific repeating points in the image.

I read the image using the following code:

    // Open the file using the file reader
    TinyTIFFReaderFile *inputFile = TinyTIFFReader_open(fullFileName.c_str());
    EXPECT_NE(inputFile, nullptr);

    // Check the properties
    EXPECT_EQ(1, TinyTIFFReader_countFrames(inputFile));
    EXPECT_EQ(xSize, TinyTIFFReader_getWidth(inputFile));
    EXPECT_EQ(ySize, TinyTIFFReader_getHeight(inputFile));
    EXPECT_EQ(1, TinyTIFFReader_getSamplesPerPixel(inputFile));
    EXPECT_EQ(32, TinyTIFFReader_getBitsPerSample(inputFile, 0));

    // Read the data and check
    unsigned int *inputData = new unsigned int[xSize*ySize];
    unsigned int counter = 0;
    for (unsigned int y=0; y<ySize; y++)
    {
        for (unsigned int x=0; x<xSize; x++)
        {
            TinyTIFFReader_getSampleData(inputFile, inputData, x + y*xSize);
            if (TinyTIFFReader_wasError(inputFile)) 
            {
                printf("Error reading sample: %s\n", TinyTIFFReader_getLastError(inputFile));
                return;
            } 
            if ((x+y*xSize) != inputData[x+y*xSize])
            {
                printf(
                    "[%d][%d] does not match. Expected: %u, got: %u\n",
                    x, y, x+y*xSize, inputData[x+y*xSize]
                );
                counter++;
                if (counter > 10) return;
            }
            EXPECT_EQ(x+y*xSize, inputData[x+y*xSize]);
        }
    }

    // Close the file
    TinyTIFFReader_close(inputFile);

And I get strange values at these [x][y] coordinates:

[255][63] does not match. Expected: 16383, got: 4107264
[255][127] does not match. Expected: 32767, got: 8301568
[255][191] does not match. Expected: 49151, got: 12495872
[255][255] does not match. Expected: 65535, got: 16690176

I don't see anything obviously wrong with my logic, and it's just 4 values in the entire 256x256 image so I'm wondering if there is a bug in the library itself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant