Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8179443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:00:00+00:00 2026-06-07T00:00:00+00:00

I have no clue why this is so hard to do but I can

  • 0

I have no clue why this is so hard to do but I can not get LibTiff.Net 2.3 to set a rational value correctly… Over the years I have always used values like “200/1” in tiff tag number 282(XRESOLUTION) and 283(YRESOLUTION). But when using the LibTiff.Net library it seems impossible to get that results. I always get things like “419430400/2097152” instead. Anyone know how I can resolve this issue?

Notes About My Question:

This is the libtiff library (pre .Net) and it looks like the first else if does account for something like 200/1.

TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
{
    static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
    uint32* m;
    float* na;
    uint32* nb;
    uint32 nc;
    int o;
    assert(sizeof(uint32)==4);
    m=_TIFFmalloc(count*2*sizeof(uint32));
    if (m==NULL)
    {
        TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
        return(0);
    }
    for (na=value, nb=m, nc=0; nc<count; na++, nb+=2, nc++)
    {
        if (*na<=0.0)
        {
            nb[0]=0;
            nb[1]=1;
        }
        else if (*na==(float)(uint32)(*na))
        {
            nb[0]=(uint32)(*na);
            nb[1]=1;
        }
        else if (*na<1.0)
        {
            nb[0]=(uint32)((*na)*0xFFFFFFFF);
            nb[1]=0xFFFFFFFF;
        }
        else
        {
            nb[0]=0xFFFFFFFF;
            nb[1]=(uint32)(0xFFFFFFFF/(*na));
        }
    }
    if (tif->tif_flags&TIFF_SWAB)
        TIFFSwabArrayOfLong(m,count*2);
    o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
    _TIFFfree(m);
    return(o);
}

This is the new .Net version…

private bool writeRationalArray(ref TiffDirEntry dir, float[] v)
{
    int[] t = new int [2 * dir.tdir_count];
    for (int i = 0; i < dir.tdir_count; i++)
    {
        int sign = 1;
        float fv = v[i];
        if (fv < 0)
        {
            if (dir.tdir_type == TiffType.RATIONAL)
            {
                WarningExt(this, m_clientdata, m_name,
                "\"{0}\": Information lost writing value ({1:G}) as (unsigned) RATIONAL",
                FieldWithTag(dir.tdir_tag).Name, fv);
                fv = 0;
            }
            else
            {
                fv = -fv;
                sign = -1;
            }
        }

        int den = 1;
        if (fv > 0)
        {
            while (fv < (1L << (31 - 3)) && den < (1L << (31 - 3)))
            {
                fv *= 1 << 3;
                den *= 1 << 3;
            }
        }

        t[2 * i + 0] = (int)(sign * (fv + 0.5));
        t[2 * i + 1] = den;
    }

    return writeData(ref dir, t, 2 * dir.tdir_count);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:00:01+00:00Added an answer on June 7, 2026 at 12:00 am

    Be sure to give me a 1UP if this helped you please. Thanks!

    OK so I decided to take on editing the library to account for the reduced fraction. Below is the code I changed in the .Net library to make it work. And work it does!

    I hope Bobrovsky includes this in his next release. I’m sure someone else will be thankful other then me! 😉

    In case you a little unsure about how to edit the library here are the steps I used as detailed as possible…

    1) Download the library source located here.

    2) Open the project file. I used the LibTiff.NET_NoSilverlight.sln solution file to open the project.

    3) Expand the LibTiff project node.

    4) Expand the Internals node.

    5) Find the Tiff_DirWrite.cs class file and open it.

    6) Inside that class file I found the function called writeRationalArray and this is what it looked like…

    /// <summary>
    /// Setup a directory entry of an array of RATIONAL or SRATIONAL and
    /// write the associated indirect values.
    /// </summary>
    private bool writeRationalArray(ref TiffDirEntry dir, float[] v)
    {
        int[] t = new int [2 * dir.tdir_count];
        for (int i = 0; i < dir.tdir_count; i++)
        {
            int sign = 1;
            float fv = v[i];
            if (fv < 0)
            {
                if (dir.tdir_type == TiffType.RATIONAL)
                {
                    WarningExt(this, m_clientdata, m_name,
                        "\"{0}\": Information lost writing value ({1:G}) as (unsigned) RATIONAL",
                        FieldWithTag(dir.tdir_tag).Name, fv);
                    fv = 0;
                }
                else
                {
                    fv = -fv;
                    sign = -1;
                }
            }
    
            int den = 1;
            if (fv > 0)
            {
                while (fv < (1L << (31 - 3)) && den < (1L << (31 - 3)))
                {
                    fv *= 1 << 3;
                    den *= 1 << 3;
                }
            }
    
            t[2 * i + 0] = (int)(sign * (fv + 0.5));
            t[2 * i + 1] = den;
        }
    
        return writeData(ref dir, t, 2 * dir.tdir_count);
    }
    

    7) Edit the writeRationalArray function to look like the following…

    /// <summary>
    /// Setup a directory entry of an array of RATIONAL or SRATIONAL and
    /// write the associated indirect values.
    /// </summary>
    private bool writeRationalArray(ref TiffDirEntry dir, float[] v)
    {
        int[] t = new int [2 * dir.tdir_count];
        for (int i = 0; i < dir.tdir_count; i++)
        {
            int sign = 1;
            float fv = v[i];
            if (fv < 0)
            {
                if (dir.tdir_type == TiffType.RATIONAL)
                {
                    WarningExt(this, m_clientdata, m_name,
                        "\"{0}\": Information lost writing value ({1:G}) as (unsigned) RATIONAL",
                        FieldWithTag(dir.tdir_tag).Name, fv);
                    fv = 0;
                }
                else
                {
                    fv = -fv;
                    sign = -1;
                }
            }
    
            int den = 1;
            if (fv > 0)
            {
                while (fv < (1L << (31 - 3)) && den < (1L << (31 - 3)))
                {
                    fv *= 1 << 3;
                    den *= 1 << 3;
                }
            }
    
            t[2 * i + 0] = (int)(sign * (fv + 0.5));
            t[2 * i + 1] = den;
    
            //Reduce the fraction
            int a = t[2 * i + 0];
            int b = t[2 * i + 1];
            while (b > 0) { int rem = a % b; a = b; b = rem; }
            for (int ind = 0; ind < 2; ind++) { t[2 * i + ind] /= a; }
        }
    
        return writeData(ref dir, t, 2 * dir.tdir_count);
    }
    

    All I did to it was add three lines of code to reduce the fraction at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should work right? I have not a clue as to why it's not.
I have no clue how i can get an existing object structure based on
I have no clue why this is happening but maybe someone here knows. I
I have no clue if this is possible or not as I'm new to
I don't have a clue why this does not work for me. I'm calling
I am new to Objective-C and I have no clue why this code is
I have no clue of where to start on this. I've never done any
I have no clue how to validate this string. I am simply supplying an
I am receiving this error. I have no clue as to why it would
I have searched through the net and still have no clue how to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.