1. pixmap to bitmap

Another Win32 programming question:  How would I go about saving a bitmap
file from a pixmap (when all I've got is a handle to the pixmap)?

I'm hoping this is trivial...  Thanks.

-- Brian

new topic     » topic index » view message » categorize

2. Re: pixmap to bitmap

Brian K. Broker wondered:


> How would I go about saving a bitmap
> file from a pixmap?

There is no savePixmapToFile, if that's the question.

A pixmap is a plain old color bitmap, and there is no routine supplied with
Windows that will automatically save the bitmap to a file. The Win32 help
file suggests that it can be done by writing the bitmap data structures out
to disk.

If you don't mind pathetically slow code, you can use the code at the end of
this message. Paintbrush won't load the resulting file if it's 4 colors, so
I've added this hack to force the palette to at least 16 colors:

   if length( pal ) < 16 do
      pal &= repeat( {0,0,0}, 16-length(pal) )
   end if


-- David Cuny

-- hacked savePixmapToFile function
--

include win32lib.ew
include image.e

function getRGB( atom a )
   -- convert rgb atom to {rgb}
    integer r, g, b
    r = and_bits( a, #FF )
    g = and_bits( a, #FF00 ) / #100
    b = and_bits( a, #FF0000 ) / #10000
    return {r,g,b}
end function

procedure savePixmapToFile( atom pixmap, sequence fName )

   --save pixmap to bitmap file

    integer at, cx, cy
    sequence size, pal, bmp, pix

    -- get extent
    size = getExtent( pixmap )
    cx = size[1]
    cy = size[2]

    -- empty bitmap and pal
    bmp = repeat( repeat( 0, cx ), cy )
    pal = {}

    for y = 1 to cy do
        for x = 1 to cx do

            -- get a pixel
            pix = getRGB( getPixel( pixmap, x, y ) )

            -- in the pal?
            at = find( pix, pal )
            if at = 0 then
                -- add to pal
                pal = append( pal, pix )
                -- get index
                at = length( pal )
            end if

            -- add to bitmap
            bmp[y][x] = at - 1

        end for
    end for

    -- make at least 16 colors
    if length( pal ) < 16 then
        pal &= repeat( {0,0,0}, 16-length(pal) )
    end if

    if save_bitmap( {pal,bmp}, fName ) then
        warnErr( "Unable to save bitmap " & fName )
    end if

end procedure

-- example of use
savePixmapToFile( loadBitmapFromFile( "icon1.bmp" ), "test.bmp" )

new topic     » goto parent     » topic index » view message » categorize

3. Re: pixmap to bitmap

> Another Win32 programming question:  How would I go about saving a bitmap
> file from a pixmap (when all I've got is a handle to the pixmap)?
>
> I'm hoping this is trivial...  Thanks.
>
> -- Brian


I actually have an old version of it still on my HD. Runs like a dream,
X-Windows and all that works just fine, except for the occasional core dump with
Netscape :)
Don't know if it's any better or worse than the UMSDOS system (included with
most standard distros?), but hey, it works.

--Tom
----
Get your free email at http://www.gnwmail.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu