1

Is it possible to manipulate pixels of image ( e.g creating sepia , grayscale , resizing etc ) trought binary code ?

Lets say we open an image with

bool readFile(string filename) {

  ifstream f(filename, ios::binary | ios:: in );
  ostringstream ob;
  int offset = 0;
  char c;

  if (f.fail()) {

    return false;

  }

  while (f.get(c)) {

    if (offset % 16 == 0)
      cout << hex << setw(8) << setfill('0') << offset << "adress ";

    cout << ' ' << hex << setw(2) << (int)(unsigned char) c;

    offset++;

  }

  return true;
}

int main() {
  if (!readFile("boss.png")) {
    cout << "Cant read" << endl;
  }
  return 0;
}

Can we access images pixels using its binary representation? If so , how ? I fail to find good resource for this.

4
  • You surely can. PNG is a public format. Mar 18, 2016 at 9:45
  • I do it to JPEGs all the time.
    – nicomp
    Mar 18, 2016 at 9:49
  • What exactly do you want to do with an image? Hide data in it? Create your own image codec? Mar 18, 2016 at 10:03
  • I would like to ,as i said , create grayscale / sepia effect , compress it , or even hide data in it. I have only done all of that using pixels directly not trought binary Mar 18, 2016 at 10:11

1 Answer 1

0

PNG is a well documented file format, and thus, theoretically it's possible for you to manipulate the image data directly. However, this is rather unusual. Generally, you would convert it to an uncompressed format, manipulate it, and then recompress it into a PNG, because reading/writing uncompressed image data is much more straightforward (and, unless you're only reading/writing a few pixels, likely much faster).

A search turns up many examples of how to read/write PNG files, for example here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.