News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

GdipLoadImageFromFile example

Started by Vortex, February 19, 2021, 03:32:20 AM

Previous topic - Next topic

Vortex

Hello,

Here is an example using the GdipLoadImageFromFile GDI+ function :

https://docs.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-image-flat

LoadImgFromFile PROC filename:QWORD

LOCAL BmpImage  :QWORD
LOCAL hBitmap   :QWORD

        invoke  GdipLoadImageFromFile,filename,\
                ADDR BmpImage

        invoke  GdipCreateHBITMAPFromBitmap,BmpImage,\
                ADDR hBitmap,0

        invoke  GdipDisposeImage,BmpImage

        mov     rax,hBitmap
        ret

LoadImgFromFile ENDP


The first paremeter of the GdipLoadImageFromFile function expects a UNICODE filename.

hutch--


Gunther

Erol,

Your program is running fine.  :thumbsup: Do they always have to be *.bmp or *.jpg? I would like to try it once with *.tga.

Gunther
You have to know the facts before you can distort them.

Gunther

It doesn't seem to work with TGA images. That's a pity.

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: Gunther on February 19, 2021, 10:11:57 AMDo they always have to be *.bmp or *.jpg? I would like to try it once with *.tga

Gdi+ handles jpg, jpeg, png, gif, tif and ico files, see attachment (work in progress, use at your own risk etc). Unfortunately it doesn't work with tga :sad:

Vortex

Hi Gunther,

I guess you will need an external library to load .tga files.

Gunther

Erol,

Quote from: Vortex on February 20, 2021, 02:20:35 AM
I guess you will need an external library to load .tga files.

loading TGA files isn't so hard. Displaying TGA files isn't easy. But it should be possible. IrfanView can do it (read, display, and write).

Gunther
You have to know the facts before you can distort them.

TimoVJL

If WIC don't support targa (.tga), it's a black sheep in image formats :undecided:
May the source be with you

jj2007

Quote from: Gunther on February 20, 2021, 03:48:20 AMloading TGA files isn't so hard. Displaying TGA files isn't easy. But it should be possible

Have fun :thumbsup:

Whether it's worthwhile is another question:
1. a medium-sized tga image is 20x the size of its jpg representation
2. converters tga->gif or jpg exist; even the old 1996 Paintshop 4.1 can do it, in batch mode

Gunther

Timo,

Quote from: TimoVJL on February 20, 2021, 04:07:43 AM
If WIC don't support targa (.tga), it's a black sheep in image formats :undecided:

no it isn't. TGA was the first image format that could properly handle true color images, by the way.

Gunther
You have to know the facts before you can distort them.

HSE

Hi Gunther,

Quote from: Gunther on February 20, 2021, 03:48:20 AM
loading TGA files isn't so hard. Displaying TGA files isn't easy. But it should be possible.

I think only is possible to diplay the format that you device support. Any other format must be translated by software or hardware. 

Here there is an example of translator to BMP (that for sure can be displayed but some Vortex's function  :biggrin:):
https://github.com/dbrant/imageformats/blob/master/ImageFormats/TgaReader.cs
Equations in Assembly: SmplMath

Gunther

Jochen,

Quote from: jj2007 on February 20, 2021, 04:48:18 AM
Have fun :thumbsup:
my reference is that.

Quote from: jj2007 on February 20, 2021, 04:48:18 AM
Whether it's worthwhile is another question:
1. a medium-sized tga image is 20x the size of its jpg representation
2. converters tga->gif or jpg exist; even the old 1996 Paintshop 4.1 can do it, in batch mode
Things are not so easy. Our fractal image compression software uses the TGA format for historical reasons. The encoder reads a TGA file and compresses it. I left out a lot of steps, but it's just the principle.
The decoder reads the compressed file, decodes it and writes back a TGA file. This can be viewed with various image viewers or image processing programs: Photoshop, Gimp, IrfanView... you name it.
We need lossless data, of course, which TGA provides; JPG is already lossy due to quantization.

In some cases it would be quite comfortable to see the result of the decoder directly without having to use additional software. But before I change everything in the encoder, decoder, filters (low pass, high pass, median),
and edge detectors, I have to try if my idea is feasible. I plan to proceed like this:

  • Determine the size of the TGA file.
  • Request enough memory for double buffer.
  • Read the TGA header.
  • Write a corresponding BITMAPFILEHEADER and BITMAPINFOHEADER in the memory.
  • Copy the image data behind the new headers.
This should work, because the image data is the same for BMP and TGA: First comes the last line, then the second to last line, ... at the end then the first line.
Since we work with 24 bit color depth, the color order is also the same: Blue, Green, Red one byte each. I think MS kept a sharp eye on the TGA format when developing the BMP format.

The question now is: Is it possible to tell via GDI or GDI+ that there is a BMP file to display at the memory address?

Gunther
You have to know the facts before you can distort them.

Gunther

HSE,

Quote from: HSE on February 20, 2021, 06:41:40 AM
Here there is an example of translator to BMP (that for sure can be displayed but some Vortex's function  :biggrin:):
https://github.com/dbrant/imageformats/blob/master/ImageFormats/TgaReader.cs

Yes, Dimitry Brant. The buddy handles all sorts of cases, including palette images with DAC table. However, I only need a specific case and do not want to shoot with cannons at sparrows.
Thank you for your effort.

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: Gunther on February 20, 2021, 06:47:23 AMThe question now is: Is it possible to tell via GDI or GDI+ that there is a BMP file to display at the memory address?

GdipCreateBitmapFromScan0 ?

hutch--

24 bit BMP format is lossless but TGA is not supported in GDI+ so I would be looking for a library or even an app that will convert TGA to 24 bit BMP.

I found a couple of links that may be useful to you.

http://tgalib.sourceforge.net/

https://github.com/nothings/stb