How to Turn Array of integers into image file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roflmfao
    New Member
    • Jun 2010
    • 6

    How to Turn Array of integers into image file

    For a project (homework), I need to manipulate an Array of integers (2D) so that only certain positions take the value 1, and all others take the value 0.

    The Array is 800x600 in size, but may vary depending on the instance.

    This works fine, the problem lies in the output:

    To make output more readable (800x600 Array, dumped into a text file, is annoying to check for errors), I would like to turn this into an Image (.bmp preferred, but it doesn't really matter)

    A value of 1 should colour the corresponding pixel black, 0 white. Is there a way to do this without manually building the entire image?

    eg
    0100001000
    0110001100
    1111001110
    1100010010
    1111100000
    should turn into a 10x5 pixel image.


    If not, does anyone how the .bmp file would have to be created? I know the basics of it's structure, and would have to write it in binary mode, but what values does the header need?

    I found some stuff for this, but it was all C++, and I only want to use C.


    To make it clear: The assignment is NOT to create the image, technically, the .txt file is ENOUGH. I merely want to make it look nicer. This will not affect my mark in any way (solving the problem is the question, not nice output)
    Just to make sure this doesn't get deleted as "Homework Assignment". It isn't.

    Hope I can be helped :)
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Originally posted by roflmfao
    For a project (homework), I need to manipulate an Array of integers (2D) so that only certain positions take the value 1, and all others take the value 0.

    The Array is 800x600 in size, but may vary depending on the instance.

    This works fine, the problem lies in the output:

    To make output more readable (800x600 Array, dumped into a text file, is annoying to check for errors), I would like to turn this into an Image (.bmp preferred, but it doesn't really matter)

    A value of 1 should colour the corresponding pixel black, 0 white. Is there a way to do this without manually building the entire image?

    eg
    0100001000
    0110001100
    1111001110
    1100010010
    1111100000
    should turn into a 10x5 pixel image.


    If not, does anyone how the .bmp file would have to be created? I know the basics of it's structure, and would have to write it in binary mode, but what values does the header need?

    I found some stuff for this, but it was all C++, and I only want to use C.


    To make it clear: The assignment is NOT to create the image, technically, the .txt file is ENOUGH. I merely want to make it look nicer. This will not affect my mark in any way (solving the problem is the question, not nice output)
    Just to make sure this doesn't get deleted as "Homework Assignment". It isn't.

    Hope I can be helped :)
    From wikipedia here is the information about the bitmap file format. To create a bitmap you just need to change your file extension from .txt to .bmp and follow all the rules for the header and scanline padding.

    Comment

    • roflmfao
      New Member
      • Jun 2010
      • 6

      #3
      :)

      Thanks for that. I then take it there is no easier way to do this?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Than just writing out the file in the correct format? No

        Comment

        • roflmfao
          New Member
          • Jun 2010
          • 6

          #5
          Originally posted by Banfa
          Than just writing out the file in the correct format? No
          I thought there might be a header file I am unaware of, or some pre-written .c file including what I need. Anyways, it seems there isn't...

          Code:
          FILE* output = fopen("output.bmp", "wb");
          This correctly opens "output.bmp " in binary mode?

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            lol this is C, since the vast majority of platforms have non-monitor let alone any graphics handling capability there is no cause to provide standard implementations for this sort of operation.

            Also writing binary data like that would be/is considered a rather simple job. All you have to do is write a header structure and the data for each pixel. I suspect if you choose the header correctly you could make each pixel a byte which should simply things.

            And yes that opens the file correctly.

            Or to keep the file size small you could just have 1 bit per pixel.

            Anyway here is a good place to start

            Comment

            • roflmfao
              New Member
              • Jun 2010
              • 6

              #7
              Thanks

              Ok, will do, thanks. I found some .c file while googling, but wasn't sure what to make of it. Obviously, it wasn't what I needed. The -bmp should be quite simple, as I only need two colors, and no greytones :) So filesize should be small (but it isn't really an issue, there will never be more than one output file :)

              This can be closed/archived/whatever now.

              Comment

              Working...