The MASM Forum

Microsoft 64 bit MASM => Examples => Topic started by: Vortex on February 19, 2021, 03:32:20 AM

Title: GdipLoadImageFromFile example
Post by: Vortex 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.
Title: Re: GdipLoadImageFromFile example
Post by: hutch-- on February 19, 2021, 09:40:45 AM
 :thumbsup:
Title: Re: GdipLoadImageFromFile example
Post by: Gunther 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
Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 19, 2021, 10:43:03 AM
It doesn't seem to work with TGA images. That's a pity.

Gunther
Title: Re: GdipLoadImageFromFile example
Post by: jj2007 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:
Title: Re: GdipLoadImageFromFile example
Post by: Vortex on February 20, 2021, 02:20:35 AM
Hi Gunther,

I guess you will need an external library to load .tga files.
Title: Re: GdipLoadImageFromFile example
Post by: Gunther 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
Title: Re: GdipLoadImageFromFile example
Post by: 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:
Title: Re: GdipLoadImageFromFile example
Post by: jj2007 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 (http://www.paulbourke.net/dataformats/tga/) :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
Title: Re: GdipLoadImageFromFile example
Post by: Gunther 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
Title: Re: GdipLoadImageFromFile example
Post by: HSE 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
Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 20, 2021, 06:47:23 AM
Jochen,

Have fun (http://www.paulbourke.net/dataformats/tga/) :thumbsup:
my reference is that (https://www.fileformat.info/format/tga/egff.htm).

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:
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
Title: Re: GdipLoadImageFromFile example
Post by: Gunther 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
Title: Re: GdipLoadImageFromFile example
Post by: jj2007 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 ?
Title: Re: GdipLoadImageFromFile example
Post by: hutch-- 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
Title: Re: GdipLoadImageFromFile example
Post by: LiaoMi on February 20, 2021, 09:49:15 AM
Hi,

FASM mandeldb + MASM Example + TGALib
http://www.dca.fee.unicamp.br/~martino/disciplinas/ea978/tgaffs.pdf

Code: [Select]

; DON'T _RUN_ this file, just COMPILE it and view it with any
; image program that supports 8-bit paletted TGA-images (many does).

; usage: "fasm mandeldb.asm"

format  binary as ".tga"

; Some basic image properties. Feel free to modify
Width  equ 120
Height       equ 120
IterLim equ 256
Prec      equ 29; don't set above 29 nor to low, else image may be scrambled

; TGA-header: 8-bit paletted image
dw 256,1
db 0,0,1,24
dw 0,0,Width,Height
db 8,8

; palette: 256*3bytes (RGB)
repeat 256
 db (%+54h) and 0FFh
 db (%+0A9h) and 0FFh
 db % - 1
end repeat

; actual image (Mandelbrot fractal)
PrecScale = 1 shl (Prec-1)

StepR = (5 shl (Prec-1) +Width       shr 1) / Width; = 2.5/Width
StepI = (5 shl (Prec-1) +Height shr 1) / Height; = 3/Height

PosI = -5 shl (Prec-2); = -1.25
repeat Height
 PosR = -2 shl Prec; = -2
 repeat Width
  R = PosR
  I = PosI
  RR = (PosR*PosR) shr Prec
  II = (PosI*PosI) shr Prec

  Color = IterLim - 1; Color is also the current iteration number
  repeat IterLim
   if (RR + II) > 4 shl Prec
    break
   end if
   Tmp = (RR - II) + PosR
   I = (R*I) / PrecScale + PosI
   R = Tmp
   RR = (Tmp*Tmp) shr Prec
   II = (I*I) shr Prec
   Color = Color - 1; discounting Color
  end repeat
  if Color < 0
   Color = 0
  end if
  db Color

  PosR = PosR + StepR
 end repeat
 PosI = PosI + StepI
end repeat

Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 20, 2021, 12:24:01 PM
LiaoMi,

thank you for your distribution. I know the C library from Mr. Brueckner.

Is it your assembly language code? The program did crash. First I've loaded a 24 Bit True Color Image and it was displayed as greyscale image but upside down and torn quite obliquely.
By loading a large True Color Image (20 MB) the application crashed.

Gunther
 
Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 20, 2021, 12:26:46 PM
Steve,

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.

that doesn't help. I think that I'm on the right way, but it'll take some time. If I am successful, I will open a new thread.

Gunther
Title: Re: GdipLoadImageFromFile example
Post by: LiaoMi on February 20, 2021, 09:05:52 PM
Hi Gunther,

no, this is an example from the fasm forum.. https://board.flatassembler.net/topic.php?t=4381

Quote
but upside down

Code: [Select]
mov     [bmi.bmiHeader.biHeight], ecx
must be

Code: [Select]
push ecx
neg ecx
mov     [bmi.bmiHeader.biHeight], ecx
pop ecx

For testing I took the file https://www.fileformat.info/format/tga/sample/8fcef46a9229460cae03d41f89ef5287/MARBLES.TGA, it was displayed, but the picture has a diagonal separator. The color of the picture is displayed very well.

Another interesting example is "Decoding OpenGL Textures in Targa Format from an SQLite Database" - https://www.codeproject.com/Articles/21084/Decoding-OpenGL-Textures-in-Targa-Format-from-an-S
Framework for loading TGA image data from an SQLite table, decoding this data and binding it to OpenGL textures.
Title: Re: GdipLoadImageFromFile example
Post by: hutch-- on February 21, 2021, 12:46:04 AM
There has to code around to do the conversion, my ancient Micrografx Picture Publisher converts both ways, I guess its finding some code that has been posted that will do the conversion.
Title: Re: GdipLoadImageFromFile example
Post by: HSE on February 21, 2021, 01:27:13 AM
no, this is an example from the fasm forum.. https://board.flatassembler.net/topic.php?t=4381
Apparently only read correctly 32bit format

You can check file format inserting in line 358 (LoadTGA):
Code: [Select]
movsx eax, byte  ptr [edx+8*2]  ; <--- bitsPerPixel
        pushad
        print str$(eax),13,10
       popad
Title: Re: GdipLoadImageFromFile example
Post by: Vortex on February 21, 2021, 01:42:05 AM
New attachment at the top. Corrected some minor bugs.
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 21, 2021, 04:55:43 AM

Library to read TGA files and other things
https://archive.codeplex.com/?p=directxtex (https://archive.codeplex.com/?p=directxtex)
Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 21, 2021, 10:31:56 AM
Good work Erol.  :thumbsup:

Yves,

Library to read TGA files and other things
https://archive.codeplex.com/?p=directxtex (https://archive.codeplex.com/?p=directxtex)

thank you for the link.

Gunther
Title: Re: GdipLoadImageFromFile example
Post by: Mikl__ on February 21, 2021, 01:19:03 PM
Hallo, Gunther!
Lesson #33: Loading Compressed and Uncompressed TGA's (http://masm32.com/board/index.php?topic=8722.msg95636#msg95636)
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 21, 2021, 07:51:19 PM
The "DirectXTex texture processing library"  https://github.com/Microsoft/DirectXTex (https://github.com/Microsoft/DirectXTex)
Seem to be done by microsoft,so I have translated the DirectXTex.h  with the LoadFromTGAFile function.
doc of the library can be found here https://github.com/microsoft/DirectXTex/wiki/DirectXTex (https://github.com/microsoft/DirectXTex/wiki/DirectXTex)
Sample of use for the function
Quote
auto image = std::make_unique<ScratchImage>();
HRESULT hr = LoadFromTGAFile( L"ROCKS.TGA", TGA_FLAGS_NONE, nullptr, *image );
if ( FAILED(hr) )
    // error
image is a structure defined in directxtex.sdk
Quote
I have try to add the 32 bit lib,too large


Image   STRUCT DEFALIGNMASM
   awidth XMASM ?
   height XMASM ?
   format DWORD ?
   rowPitch XMASM ?
   slicePitch XMASM ?
pixels XMASM ?
Image      ENDS

.data
Oneimage image <>
.code
invoke LoadFromTGAFile,TXT( "ROCKS.TGA"), TGA_FLAGS_NONE,NULL, addr Oneimage           ;perhaps name must be in unicode
Title: Re: GdipLoadImageFromFile example
Post by: jj2007 on February 21, 2021, 09:48:59 PM
The "DirectXTex texture processing library"  https://github.com/Microsoft/DirectXTex (https://github.com/Microsoft/DirectXTex)

I've tried the ...\DirectXTex-master\DirectXTex_Desktop_2017.sln with VS Express 2010, but obviously it throws loads of errors (so much to portability etc). Could you build the DLL, Yves, can you post it?
Title: Re: GdipLoadImageFromFile example
Post by: TimoVJL on February 22, 2021, 12:13:47 AM
http://www.dhpoware.com/source/bitmap.html
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 22, 2021, 03:58:09 AM
JJ,
library 32 on my site http://luce.yves.pagesperso-orange.fr/directxtex.zip (http://luce.yves.pagesperso-orange.fr/directxtex.zip) + hader

Title: Re: GdipLoadImageFromFile example
Post by: jj2007 on February 22, 2021, 05:14:31 AM
Thanks, Yves :thumbsup:
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 23, 2021, 03:23:41 AM

find a tga dumper who can be of great help (enum...)
Quote
TGA ---------------------------------------------- XING_T32.TGA --------------------------
ColorMap Type = 0
ImageType = 2   TGA_TRUECOLOR
ColorMap First = 0
ColorMap Length = 0
ColorMap Entry Size = 0
Origin 0, 0
Size: 240 by 164
Bits Per Pixel 32
Image descriptor 20
        TGA_FLAGS_INVERTY
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 24, 2021, 08:32:58 PM
reusing the dumpfile and the image given by LiaoMi (XING_T32.TGA) in C++.
Follow use of the Directx::ScratchImage class
Quote
int __cdecl wmain(int argc, wchar_t* argv[], wchar_t* envp[])
{
    const DirectX::Image* ImageStruct = 0 //  To show the content of Image struct

auto image = std::make_unique<ScratchImage>();
HRESULT hr = LoadFromTGAFile( L"XING_T32.TGA",nullptr, *image );
if ( FAILED(hr) )
    printf("LoadFromTGAFile failed");
else printf("LoadFromTGAFile REUSSITE \n");
//        size_t __cdecl GetPixelsSize() const { return m_size; }

ImageStruct = image->GetImages();
// image->m_image
printf("image width %d", ImageStruct->width );

Next step is to create a masm proto to call that.

Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 25, 2021, 04:02:37 AM
Yves,

thank you for your efforts.

find a tga dumper who can be of great help (enum...)

The TGA header contains 18 bytes in which all information about the image and its display is stored. Reading these out and interpreting them correctly is really not witchcraft.
What MS has written in code for this simple task is very cumbersome. On the PowerBASIC page, you can find a TGA Viewer, written by me with PB 3.5 (DOS compiler).
The program can display True Color images and TGA images with DAC (256 colors) correctly. To be able to display large images, I used the XMS memory and worked with
VESA bank switching. The credits of the program still show my old mail address at CompuServe. So the program was written in the mid 90s.

Of course, my program has weaknesses. I am in the process of changing that. At that time VESA 2.0 was up to date and my program hard coded e.g. some VESA modes. 
The right thing to do, of course, is to get the list of available VESA modes and scan them etc.

Gunther   
Title: Re: GdipLoadImageFromFile example
Post by: TouEnMasm on February 25, 2021, 04:13:55 AM

I have just a little problem with the upper function.
I can do manything to get informations and modify the file but ... I don't know how to view it ??
any idea ??
There is no DC given.
Title: Re: GdipLoadImageFromFile example
Post by: Gunther on February 25, 2021, 05:28:47 AM
Yves,

I have just a little problem with the upper function.
I can do manything to get informations and modify the file but ... I don't know how to view it ??
any idea ??
There is no DC given.

not at the moment. Let me think about that.

Gunther