Author Topic: GdipLoadImageFromFile example  (Read 3953 times)

Vortex

  • Member
  • *****
  • Posts: 2583
GdipLoadImageFromFile example
« on: February 19, 2021, 03:32:20 AM »
Hello,

Here is an example using the GdipLoadImageFromFile GDI+ function :

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

Code: [Select]
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.
« Last Edit: February 21, 2021, 01:41:21 AM by Vortex »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8492
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: GdipLoadImageFromFile example
« Reply #1 on: February 19, 2021, 09:40:45 AM »
 :thumbsup:
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #2 on: February 19, 2021, 10:11:57 AM »
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
Get your facts first, and then you can distort them.

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #3 on: February 19, 2021, 10:43:03 AM »
It doesn't seem to work with TGA images. That's a pity.

Gunther
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Re: GdipLoadImageFromFile example
« Reply #4 on: February 19, 2021, 12:03:57 PM »
Do 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

  • Member
  • *****
  • Posts: 2583
Re: GdipLoadImageFromFile example
« Reply #5 on: February 20, 2021, 02:20:35 AM »
Hi Gunther,

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

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #6 on: February 20, 2021, 03:48:20 AM »
Erol,

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
Get your facts first, and then you can distort them.

TimoVJL

  • Member
  • ****
  • Posts: 723
Re: GdipLoadImageFromFile example
« Reply #7 on: February 20, 2021, 04:07:43 AM »
If WIC don't support targa (.tga), it's a black sheep in image formats :undecided:
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Re: GdipLoadImageFromFile example
« Reply #8 on: February 20, 2021, 04:48:18 AM »
loading 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

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #9 on: February 20, 2021, 06:09:55 AM »
Timo,

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
Get your facts first, and then you can distort them.

HSE

  • Member
  • *****
  • Posts: 1741
  • <AMD>< 7-32>
Re: GdipLoadImageFromFile example
« Reply #10 on: February 20, 2021, 06:41:40 AM »
Hi Gunther,

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

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #11 on: February 20, 2021, 06:47:23 AM »
Jochen,

Have fun :thumbsup:
my reference is that.

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
Get your facts first, and then you can distort them.

Gunther

  • Member
  • *****
  • Posts: 3723
  • Forgive your enemies, but never forget their names
Re: GdipLoadImageFromFile example
« Reply #12 on: February 20, 2021, 07:06:22 AM »
HSE,

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
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 11550
  • Assembler is fun ;-)
    • MasmBasic
Re: GdipLoadImageFromFile example
« Reply #13 on: February 20, 2021, 09:13:47 AM »
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?

GdipCreateBitmapFromScan0 ?

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8492
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: GdipLoadImageFromFile example
« Reply #14 on: February 20, 2021, 09:48:46 AM »
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
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy: