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 4558668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:40:51+00:00 2026-05-21T17:40:51+00:00

Because I’m masochistic I’m trying to write something in C to decode an 8-bit

  • 0

Because I’m masochistic I’m trying to write something in C to decode an 8-bit PNG file (it’s a learning thing, I’m not trying to reinvent libpng…)

I’ve got to the point when the stuff in my deflated, unfiltered data buffer unmistakably resembles the source image (see below), but it’s still quite, erm, wrong, and I’m pretty sure there’s something askew with my implementation of the filtering algorithms. Most of them are quite simple, but there’s one major thing I don’t understand in the docs, not being good at maths or ever having taken a comp-sci course:

Unsigned arithmetic modulo 256 is used, so that both the inputs and outputs fit into bytes.

What does that mean?

If someone can tell me that I’d be very grateful!

For reference, (and I apologise for the crappy C) my noddy implementation of the filtering algorithms described in the docs look like:

unsigned char paeth_predictor (unsigned char a, unsigned char b, unsigned char c) {
    // a = left, b = above, c = upper left
    char p = a + b - c; //  initial estimate
    char pa = abs(p - a);    //  distances to a, b, c
    char pb = abs(p - b);
    char pc = abs(p - c);
    // return nearest of a,b,c,
    // breaking ties in order a,b,c.
    if (pa <= pb && pa <= pc) return a;
    else if (pb <= pc) return b;
    else return c;
}

void unfilter_sub(char* out, char* in, int bpp, int row, int rowlen) {
    for (int i = 0; i < rowlen; i++)
        out[i] = in[i] + (i < bpp ? 0 : out[i-bpp]);
}

void unfilter_up(char* out, char* in, int bpp, int row, int rowlen) {
    for (int i = 0; i < rowlen; i++)
        out[i] = in[i] + (row == 0 ? 0 : out[i-rowlen]);
}

void unfilter_paeth(char* out, char* in, int bpp, int row, int rowlen) {
    char a, b, c;
    for (int i = 0; i < rowlen; i++) {
        a = i < bpp ? 0 : out[i - bpp];
        b = row < 1 ? 0 : out[i - rowlen];
        c = i < bpp ? 0 : (row == 0 ? 0 : out[i - rowlen - bpp]);
        out[i] = in[i] + paeth_predictor(a, b, c);
    }
}              

And the images I’m seeing:

Source

Source http://img220.imageshack.us/img220/8111/testdn.png

Output

Output http://img862.imageshack.us/img862/2963/helloworld.png

  • 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-05-21T17:40:51+00:00Added an answer on May 21, 2026 at 5:40 pm

    It means that, in the algorithm, whenever an arithmetic operation is performed, it is performed modulo 256, i.e. if the result is greater than 256 then it “wraps” around. The result is that all values will always fit into 8 bits and not overflow.

    Unsigned types already behave this way by mandate, and if you use unsigned char (and a byte on your system is 8 bits, which it probably is), then your calculation results will naturally just never overflow beyond 8 bits.

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

Sidebar

Related Questions

Because regular expressions scare me, I'm trying to find a way to remove all
Because I am a newbie I am trying to log out any errors that
Because of strange cross domain AJAX problems, causing AJAX requests to fail when not
because it will not show up as » but will show up as &raquo
Because the the UISwitch is not so easy to customize i decide i wanna
Because I didn't create textmate link when installing, so it can not find 'mate'
Because SO is a bit slow lately, I'm posting an easy question. I would
Because my mainActivity does not run my Tab2Activity at startup until the user press
Because I need to save a web page as a mht file format for
Because of the more tedious way of adding hosts to be monitored in Nagios

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.