Author Topic: 24 bits reading  (Read 3555 times)

gelatine1

  • Member
  • **
  • Posts: 217
24 bits reading
« on: January 18, 2015, 05:59:56 AM »
unfortunately im reading a file (bmp image file) which has its data packed in 3 bytes. So my question is how should I read this ? I have the file mapped into my memory so I have a pointer to it but I can't use
Code: [Select]
dword ptr [esi] or
Code: [Select]
word ptr [esi] or anything similar right ?

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: 24 bytes reading
« Reply #1 on: January 18, 2015, 06:16:49 AM »
you can, if ESI is set to the pointer   :P

gelatine1

  • Member
  • **
  • Posts: 217
Re: 24 bytes reading
« Reply #2 on: January 18, 2015, 06:27:13 AM »
I don't really get what you mean ?  :icon_confused:

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: 24 bits reading
« Reply #3 on: January 18, 2015, 08:10:30 AM »
I have the file mapped into my memory so I have a pointer to it but I can't use...

you say you have the pointer - let's say it's lpView
Code: [Select]
    mov     esi,lpViewnow, you can use ESI   :biggrin:

truthfully, though.....
BMP images are never so large that file mapping is needed
you can probably fit the entire file in memory at once

you could use HeapAlloc to allocate a large block of memory, then read it in
or, you could use LoadImage and set the LR_CREATEDIBSECTION flag

a DIB section is a bitmap with a pointer and direct access to the image data bits

after using LoadImage to create a DIB section,
use GetObject to fill a DIBSECTION structure to get the data pointer

http://msdn.microsoft.com/en-us/library/windows/desktop/ms648045%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/dd144904%28v=vs.85%29.aspx

http://msdn.microsoft.com/en-us/library/dd183567%28v=vs.85%29.aspx

gelatine1

  • Member
  • **
  • Posts: 217
Re: 24 bits reading
« Reply #4 on: January 18, 2015, 08:24:30 AM »
Well wait I think I asked my question in a wrong way. So I guess I'll rephrase my question. I meant to ask if there is some kind of way to subtract 3 bytes from memory and move it to a 4 byte register. Something that would look like
Code: [Select]
movzx eax,word ptr [esi] to subtract 2 bytes from memory and get it in a 4 bytes register.

I guess I'll take a look a look into the functions you gave me there. thank you

KeepingRealBusy

  • Member
  • ***
  • Posts: 426
Re: 24 bits reading
« Reply #5 on: January 18, 2015, 08:31:54 AM »
You just set a pointer to the lowest byte, then move a dword to a register and mask out the most significant byte and advance the pointer by the 3 bytes you already have

Dave.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
Re: 24 bits reading
« Reply #6 on: January 18, 2015, 08:41:54 AM »
right - as Dave mentioned...
Code: [Select]
    mov     eax,[esi]
    and     eax,0FFFFFFh

the address in ESI is calculated, based on the location of the desired pixel
image base address + ( line number * bytes per line) + ( 3 * column number)
notice that most BMP images are upside-down (top image line is last in file)
the sign of the height value in the header tells you if it is right-side up   :P

bytes per line is always some multiple of 4
so, if you have a 24-bit image that is 3 pixels wide, the bytes per line will be 12

Tedd

  • Member
  • ***
  • Posts: 377
  • Procrastinor Extraordinaire
Re: 24 bits reading
« Reply #7 on: January 20, 2015, 04:42:06 AM »
Code: [Select]
@@:
mov eax,[esi]       ;get 4 bytes
and eax,00ffffffh   ;mask out the top one, leaving the 3 bytes you want
...                 ;do stuff
add esi,3           ;move to the next 3 bytes
jmp @B              ;repeat..
Potato2