Jump to content

Make Image Pixel by Pixel

wangyuji431

Is there any way in C or java to make an image pixel by pixel?

Any help would be appreciated.

Link to comment
Share on other sites

Link to post
Share on other sites

I know java has a way, look up the iron-puzzle.png and go from there, python does as well with the PIL module

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[i7-7700k@5Ghz | MSI Z270 M7 | 16GB 3000 GEIL EVOX | STRIX ROG 1060 OC 6G | EVGA G2 650W | ROSEWILL B2 SPIRIT | SANDISK 256GB M2 | 4x 1TB Seagate Barracudas RAID 10 ]

[i3-4360 | mini-itx potato | 4gb DDR3-1600 | 8tb wd red | 250gb seagate| Debian 9 ]

[Dell Inspiron 15 5567] 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly is the problem?

 

For a 24 bit image, you have 3 bytes for each pixel , red green and blue ... you just define an array of bytes in memory , three for each pixel.

When you're done... just save those bytes into a particular file format.

BMP is the simplest, you just have some headers and then basically you dump the bytes in the file.. just have to keep in mind with BMP files the last line in the image is saved first (you go from bottom to top)

See http://www.fileformat.info/format/bmp/egff.htm or  http://www.digicamsoft.com/bmp/bmp.html  or https://en.wikipedia.org/wiki/BMP_file_format for very detailed information about bmp file format.

 

PNG files are also super simple, but you'd have to add crc32 and zlib libraries to compress the bytes when saving to file.

 

Windows API also has some image handling functions but you don't have to use them. See for example https://msdn.microsoft.com/en-us/library/windows/desktop/dd183388(v=vs.85).aspx

 

Link to comment
Share on other sites

Link to post
Share on other sites

It is super easy in Java

 

BufferedImage img = ImageIO.read(new File("image-orginal"));

// change pixels in here with
img.setRGB(x, y, rgb)

ImageIO.write(img, "png", new File("image-final.png"));

 

Link to comment
Share on other sites

Link to post
Share on other sites

You could do it yourself If you used a really simple format. Or you could use a library like libpng.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

14 hours ago, wangyuji431 said:

Is there any way in C or java to make an image pixel by pixel?

Any help would be appreciated.

You can totally do that in any Touring Complete language! Uncompressed image formats are actually super simple and usually only have 3 bytes per pixel, plus an image header. 

A bitmap is super simple, as it is really just an array of arrays where each sub array is 3 bytes and represents one pixel. The jagged array is then preceded by a a header which describes image parameters necessary for decoding and displaying the image.

Here's some light reading as a start: BMP Image Format

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Maybe you want to look into the Opencv library for C/C++ which offers some easy way to manipulate pictures pixel by pixel. 

Link to comment
Share on other sites

Link to post
Share on other sites

Qt is the defacto C++ GUI library, and it allows you to directly manipulate pixels.

Link to comment
Share on other sites

Link to post
Share on other sites

On 7/29/2016 at 0:39 AM, prolemur said:

It is super easy in Java

 


BufferedImage img = ImageIO.read(new File("image-orginal"));

// change pixels in here with
img.setRGB(x, y, rgb)

ImageIO.write(img, "png", new File("image-final.png"));

 

Thanks!

I don't know much about picture formats, but I'm assuming png files are uncompressed?

Link to comment
Share on other sites

Link to post
Share on other sites

No, PNG images are compressed using the DEFLATE algorithm  (available in the zlib library , similar to zip compression, very common and popular, used to compress website pages on the fly while sending to users' browsers, also used in 7z and other file compressors): http://www.libpng.org/pub/png/spec/1.2/PNG-Compression.html

 

Besides BMP, the only easier file format I could think of that's also somewhat well known and decodable by image viewers is TGA  (Truevision Targa). It's basically a header and then a dump of bytes with the image contents (uncompressed or compressed, as configured by programmer in the header)

Here's the format information : http://www.paulbourke.net/dataformats/tga/

 

BMP has some quirks which makes is slightly annoying. For example, besides the fact that you have to save to file the last horizontal line and then move up towards the first line, for each horizontal line your program must write to disk a quantity of bytes that's multiple of 4. So for example, if your image is 24 bit rgb and it's 301 pixels wide, that means for each line you'd write 301 x 3 bytes per pixel = 903 bytes but this is not a multiple of 4, so you'd write an extra byte to have a multiple of 4 (226 x 4 = 904 bytes). It's not rocket science, but it's yet another quirk which has to be kept track of, and naturally these extra bytes have to be added to the image size specified in the bmp header and so on.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×