Computer Science Canada

Screen -> File using Hex Output

Author:  Zren [ Sun Aug 02, 2009 3:47 am ]
Post subject:  Screen -> File using Hex Output

Just because I wanted to see if I could prove DemonWasp wrong in the ability to have screen output writing to file in this thread.
http://compsci.ca/v3/viewtopic.php?p=190965#190965

Though I got to admit I've been wondering this for awhile. So I looked into what makes a basic uncompressed BMP file. And after some reading, copying and checking with a hex editor...it kinda works.

Now I'm just wondering what's making this output. Is it because of my RGB conversion? or my horrible attempt at hacking the Hex format. Even so, why it the bloody hell is it slanted... Too much of something on each row?

Anyways, the code is VERY hack and slash and fit to none of my standards other than "it works." Though this is only because Strings don't support ASCII values of 0 and 128.

Turing:

Draw.FillStar (0, 0, 99, 99, red) % for the output

var r, g, b : real % for use later with colour

var fileName : string := "Master.bmp"
var fileNo : int
open : fileNo, fileName, put

procedure hexstr (h : string)
    var v : array 0 .. 1 of int
    var value : int
    for i : 1 .. length (h)
        case (Str.Upper (h (i))) of
            label "A" :
                v (i mod 2) := 10
            label "B" :
                v (i mod 2) := 11
            label "C" :
                v (i mod 2) := 12
            label "D" :
                v (i mod 2) := 13
            label "E" :
                v (i mod 2) := 14
            label "F" :
                v (i mod 2) := 15
            label :
                v (i mod 2) := strint (h (i))
        end case
        if i mod 2 = 0 then
            value := v (0) + v (1) * 16
            put : fileNo, chr (value) ..
        end if
    end for
end hexstr

hexstr ("424D")      %Magic Number "BM"
hexstr ("46000000")  %Size of the BMP file (Doesn't matter it seems)
hexstr ("00000000")  %Application Specific
hexstr ("36000000")  %The offset where the bitmap data (pixels) can be found.
hexstr ("28000000")  %The number of bytes in the header (from this point).
hexstr ("64000000")  %The width of the bitmap in pixels
hexstr ("64000000")  %The height of the bitmap in pixels
hexstr ("0100")      %Number of colour planes being used
hexstr ("1800")      %The number of bits/pixel (24 bit)
hexstr ("00000000")  %BI_RPG, No compression used
hexstr ("10000000")  %The size of the raw BMP data (after this header)
hexstr ("130B0000")  %The horizontal resolution of the image
hexstr ("130B0000")  %The vertical resolution of the image
hexstr ("00000000")  %Number of colors in the palette
hexstr ("00000000")  %Means all colors are important

%Actual Picture
for y : 0 .. 100
    for x : 0 .. 100
        RGB.GetColor (whatdotcolor (x, y), r, g, b)
        put : fileNo, chr (floor (r * 255)), chr (floor (g * 255)), chr (floor (b * 255)) ..
    end for
    hexstr ("0000") %End Row for picture
end for

close : fileNo


PS: Turing help is a massive pain with uploading, previewing and all with this template.

Author:  Dan [ Sun Aug 02, 2009 6:43 am ]
Post subject:  RE:Screen -> File using Hex Output

If what you mean is save the screen to a bmp file turing can allready do this:

Turing:

var myPic : int := Pic.New (0,0,maxx,maxy)
Pic.Save (myPic, "mySavedPic.bmp")

Author:  Brightguy [ Sun Aug 02, 2009 11:28 am ]
Post subject:  Re: Screen -> File using Hex Output

You're drawing an image of width 101 into a bitmap of width 100. Also your alignment is off, you don't need any row padding for width 100.

Author:  Zren [ Sun Aug 02, 2009 11:53 am ]
Post subject:  Re: RE:Screen -> File using Hex Output

Dan @ Sun Aug 02, 2009 6:43 am wrote:
If what you mean is save the screen to a bmp file turing can allready do this:

Turing:

var myPic : int := Pic.New (0,0,maxx,maxy)
Pic.Save (myPic, "mySavedPic.bmp")


Oh that's cool. I started looking through the module and found Pic.ScreenSave( x1, y1, x2, y2, filename ) and skips the first line and the extra Pic.Free() at the end.

I took out the end of line statement and it still works (got rid of that ugly diagonal line dabut). Then I remembered I started the two for loops from 0 thus making 0..100 101 pixels in dimension. Thus causing the overflow onto the next row of pixels. (Hey I first coded this past midnight).

I could go further with this and make it into a module and such but seeing as the code has already been made, this'll do.

EDIT: Thanks brightguy. You musta posted right after I click reply.

End Results:

Turing:

var r, g, b : real % for use later with colour
var fileNo : int

procedure hexstr (h : string)
    if length (h) mod 2 = 0 then %If even in length
        var v : array 0 .. 1 of int
        var value : int
        for i : 1 .. length (h)
            case (Str.Upper (h (i))) of
                label "A" :
                    v (i mod 2) := 10
                label "B" :
                    v (i mod 2) := 11
                label "C" :
                    v (i mod 2) := 12
                label "D" :
                    v (i mod 2) := 13
                label "E" :
                    v (i mod 2) := 14
                label "F" :
                    v (i mod 2) := 15
                label :
                    v (i mod 2) := strint (h (i))
            end case
            if i mod 2 = 0 then
                value := v (0) + v (1) * 16
                put : fileNo, chr (value) ..
            end if
        end for
    else
        put "Error with hex input."
    end if
end hexstr

procedure ScreenToFile (filename : string)
    open : fileNo, filename, put

    hexstr ("424D")         %Magic Number "BM"
    hexstr ("46000000")         %Size of the BMP file (Doesn't matter it seems)
    hexstr ("00000000")         %Application Specific
    hexstr ("36000000")         %The offset where the bitmap data (pixels) can be found.
    hexstr ("28000000")         %The number of bytes in the header (from this point).
    hexstr ("64000000")         %The width of the bitmap in pixels
    hexstr ("64000000")         %The height of the bitmap in pixels
    hexstr ("0100")         %Number of colour planes being used
    hexstr ("1800")         %The number of bits/pixel (24 bit)
    hexstr ("00000000")         %BI_RPG, No compression used
    hexstr ("10000000")         %The size of the raw BMP data (after this header)
    hexstr ("130B0000")         %The horizontal resolution of the image
    hexstr ("130B0000")         %The vertical resolution of the image
    hexstr ("00000000")         %Number of colors in the palette
    hexstr ("00000000")         %Means all colors are important

    %Actual Picture
    for y : 0 .. 99
        for x : 0 .. 99
            RGB.GetColor (whatdotcolor (x, y), r, g, b)
            put : fileNo, chr (floor (r * 255)), chr (floor (g * 255)), chr (floor (b * 255)) ..
        end for
    end for

    close : fileNo
end ScreenToFile

Draw.FillBox (0, 0, 99, 99, black)
for decreasing i : 50 .. 10
    Draw.Oval (50, 50, i, i, i)
end for
ScreenToFile ("Master.bmp")


: