Create a Bitmap from RAW Data 
Author Message
 Create a Bitmap from RAW Data

Hi,
  I have on a file a 8-bit image in RAW format (1 byte=RGB color of 1
pixel). the Image has a size of 416*416. I can paint the image on a
PictureBox using the SetPixelV (or SetPixel) but it takes too much
time. I'm really a newbie on GDI API and I don't know who to make the
drawing faster.
  I think the solution will be using the SetDIBits function but I
can't make it work. Can anyone help me on that topic please?.  thanks.

    Jorge


'//+++++++++++++++++++++++++++++++++++++++++++++++++++++
    Dim test As BITMAPINFO
    Dim iDC As Long
    Dim hPointer As Long
    Dim hBitmap As Long
    Dim lfree As Long
    Dim strBuffer As String
    Dim strFileName As String
    Dim lColor As Byte
    Dim i As Long
    Dim j As Long
    Dim table() As Byte
    Dim Buffer() As Byte

    With test.bmiHeader
        .biWidth = 416
        .biHeight = 416
        .biPlanes = 1
        .biBitCount = 32
        .biCompression = BI_RGB
        .biSize = Len(test.bmiHeader)
    End With

    iDC = CreateCompatibleDC(0)

    hBitmap = CreateDIBSection(iDC, test, DIB_RGB_COLORS, hPointer,
ByVal 0&, ByVal 0&)
    SelectObject iDC, hBitmap

    ReDim table(1 To 4, 1 To test.bmiHeader.biHeight, 1 To
test.bmiHeader.biWidth)
    strFileName = "c:\test.raw"

    lfree = FreeFile
    Open strFileName For Binary Access Read As #lfree
        ReDim Buffer(1 To LOF(lfree))
        Get #lfree, , Buffer
    Close #lfree

    For j = 1 To 416
        For i = 1 To 416
            lColor = Buffer(i * j)
            table(1, i, j) = lColor
            table(2, i, j) = lColor
            table(3, i, j) = lColor
            table(4, i, j) = 0
         Next
    Next

    SetDIBits picLarge(1).HDC, picLarge(1).Image, 1,
test.bmiHeader.biHeight, table(1, 1, 1), test, DIB_RGB_COLORS

    DeleteDC iDC
    DeleteObject hBitmap
'//+++++++++++++++++++++++++++++++++++++++++++++++++++++



Mon, 07 Feb 2005 05:07:40 GMT  
 Create a Bitmap from RAW Data
Hi

I have made a RAW displayer OCX control before which uses this function. Do
you want me to send the source code to you?

Max Bolingbroke


Quote:
> Hi,
>   I have on a file a 8-bit image in RAW format (1 byte=RGB color of 1
> pixel). the Image has a size of 416*416. I can paint the image on a
> PictureBox using the SetPixelV (or SetPixel) but it takes too much
> time. I'm really a newbie on GDI API and I don't know who to make the
> drawing faster.
>   I think the solution will be using the SetDIBits function but I
> can't make it work. Can anyone help me on that topic please?.  thanks.

>     Jorge


> '//+++++++++++++++++++++++++++++++++++++++++++++++++++++
>     Dim test As BITMAPINFO
>     Dim iDC As Long
>     Dim hPointer As Long
>     Dim hBitmap As Long
>     Dim lfree As Long
>     Dim strBuffer As String
>     Dim strFileName As String
>     Dim lColor As Byte
>     Dim i As Long
>     Dim j As Long
>     Dim table() As Byte
>     Dim Buffer() As Byte

>     With test.bmiHeader
>         .biWidth = 416
>         .biHeight = 416
>         .biPlanes = 1
>         .biBitCount = 32
>         .biCompression = BI_RGB
>         .biSize = Len(test.bmiHeader)
>     End With

>     iDC = CreateCompatibleDC(0)

>     hBitmap = CreateDIBSection(iDC, test, DIB_RGB_COLORS, hPointer,
> ByVal 0&, ByVal 0&)
>     SelectObject iDC, hBitmap

>     ReDim table(1 To 4, 1 To test.bmiHeader.biHeight, 1 To
> test.bmiHeader.biWidth)
>     strFileName = "c:\test.raw"

>     lfree = FreeFile
>     Open strFileName For Binary Access Read As #lfree
>         ReDim Buffer(1 To LOF(lfree))
>         Get #lfree, , Buffer
>     Close #lfree

>     For j = 1 To 416
>         For i = 1 To 416
>             lColor = Buffer(i * j)
>             table(1, i, j) = lColor
>             table(2, i, j) = lColor
>             table(3, i, j) = lColor
>             table(4, i, j) = 0
>          Next
>     Next

>     SetDIBits picLarge(1).HDC, picLarge(1).Image, 1,
> test.bmiHeader.biHeight, table(1, 1, 1), test, DIB_RGB_COLORS

>     DeleteDC iDC
>     DeleteObject hBitmap
> '//+++++++++++++++++++++++++++++++++++++++++++++++++++++



Mon, 07 Feb 2005 20:02:19 GMT  
 Create a Bitmap from RAW Data


Quote:
>Hi,
>  I have on a file a 8-bit image in RAW format (1 byte=RGB color of 1
>pixel). the Image has a size of 416*416. I can paint the image on a
>PictureBox using the SetPixelV (or SetPixel) but it takes too much
>time. I'm really a newbie on GDI API and I don't know who to make the
>drawing faster.
>  I think the solution will be using the SetDIBits function but I
>can't make it work. Can anyone help me on that topic please?.  thanks.

>    Jorge

If I correctly understand your format the byte sequence is:
  B,G,R,B,G,R,B,G,R,...........

Quote:

>    iDC = CreateCompatibleDC(0)

>    hBitmap = CreateDIBSection(iDC, test, DIB_RGB_COLORS, hPointer,
>ByVal 0&, ByVal 0&)
>    SelectObject iDC, hBitmap

Why the above two calls?

Quote:
>    ReDim table(1 To 4, 1 To test.bmiHeader.biHeight, 1 To
>test.bmiHeader.biWidth)
>    For j = 1 To 416
>        For i = 1 To 416
>            lColor = Buffer(i * j)
>            table(1, i, j) = lColor
>            table(2, i, j) = lColor
>            table(3, i, j) = lColor
>            table(4, i, j) = 0
>         Next
>    Next

I would have said:

wid = test.bmiHeader.biWidth
hgt = test.bmiHeader.biHeight
ReDim table(1 To 4, 1 To wid, 1 To hgt)

For j = 1 To hgt
  For i = 1 To wid
    For k = 1 to 4
      lcolor = Buffer(k + ((i -1) * 4) + ((j - 1) * 8))
      table(k, i, j) = lcolor
    Next k
  Next i
Next j

Quote:
>    SetDIBits picLarge(1).HDC, picLarge(1).Image, 1,
>test.bmiHeader.biHeight, table(1, 1, 1), test, DIB_RGB_COLORS

The size of the image given by the byte data and the size of
picLarge(1).Image have to be the same.

--
Richard Mason



Mon, 07 Feb 2005 20:48:22 GMT  
 Create a Bitmap from RAW Data


Quote:
>I have made a RAW displayer OCX control before which uses this function. Do
>you want me to send the source code to you?

Presumably you are working with one specific format. The only info on
image RAW Format at Wotsit says:

 "A raw file could be as simple as a stream of RGB values or as complex
as random numbers as a header file with planar CMYK settings appended
afterwards."

--
Richard Mason



Mon, 07 Feb 2005 21:10:53 GMT  
 Create a Bitmap from RAW Data
The Load function looks like this:

Public Function LoadImage(Path As String, BitOrder As enmBitOrder, Width As
Long, Height As Long, Optional Flipped As Boolean = False, Optional
HeaderSize As Long = 0) As Boolean

So it supports multiple bit orders BUT it only supports simple files
(streams of colours)

Max Bolingbroke


Quote:


> >I have made a RAW displayer OCX control before which uses this function.
Do
> >you want me to send the source code to you?

> Presumably you are working with one specific format. The only info on
> image RAW Format at Wotsit says:

>  "A raw file could be as simple as a stream of RGB values or as complex
> as random numbers as a header file with planar CMYK settings appended
> afterwards."

> --
> Richard Mason



Tue, 08 Feb 2005 04:10:38 GMT  
 Create a Bitmap from RAW Data
Hi,

   Thank you for the answers.

   Max, I'm interested in the OCX you told before. Can you send it to
me, please? Is there any restriction on the use (license)?

   In the LoadImage function where is the buffer with the data? all
the parameters are for input use (path, width, height, orientation)
and it returns a boolean (success or not?). Aparently y could use it
for loading but my problem is displaying the data on a picturebox.

   Richard, about the RAW format I'm working is exactly the same as
it's said in Wotsit site. The file is a sequence of bytes where 1 byte
is a color in a 256-grayscale palette. So this color is the same for
Red, Green and Blue.

   I begin to use the GDI library two days ago. The calls to
CreateCompatibleDC and CreateDIBSection are there because I understood
after reading some tutorials on the Net and the MSDN that it was
necessary to make those calls.

Quote:
> I would have said:

> wid = test.bmiHeader.biWidth
> hgt = test.bmiHeader.biHeight
> ReDim table(1 To 4, 1 To wid, 1 To hgt)

> For j = 1 To hgt
>   For i = 1 To wid
>     For k = 1 to 4
>       lcolor = Buffer(k + ((i -1) * 4) + ((j - 1) * 8))
>       table(k, i, j) = lcolor
>     Next k
>   Next i
> Next j

The forth element in the table is not reserved and so must be set to
zero? Since the data in the file is not with the format you mention
earlier, the asignement      lcolor = Buffer(k + ((i -1) * 4) + ((j -
1) * 8))   won't work. thank you anyway. If you need more information
about the RAW format and you have the Adobe Photoshop, you can save
any 8-bit image into this format and see precisely what are the bytes
in the file.

I read somewhere in the past message of the group that I have to tell
the DC what is the palette I must use (since I have a 8-bit image), is
that right? How can I do that?

   Thank you all again,

       Ethiel



Tue, 08 Feb 2005 21:45:01 GMT  
 Create a Bitmap from RAW Data


Quote:
> The file is a sequence of bytes where 1 byte
>is a color in a 256-grayscale palette. So this color is the same for
>Red, Green and Blue.

>I read somewhere in the past message of the group that I have to tell
>the DC what is the palette I must use (since I have a 8-bit image), is
>that right? How can I do that?

For a paletted image the image data are indices into a color table.
With a full grayscale color table the color table values go from 0 to
255 and the indices also range from 0 to 255 so the index is the gray
color, i.e. there's no point in looking up index 27 to return the color
27,27,27

  Dim ff As Integer
  Dim filename As String
  Dim filesize As Long
  Dim wid As Long, hgt As Long
  Dim icount As Long, jcount As Long, kcount As Long
  Dim index As Long
  Dim lret As Long
  Dim bytearray() As Byte
  Dim imagearray() As Byte
  Dim bmapinfo As BITMAPINFO

  'Have to know beforehand the image dimensions.
  wid = ...
  hgt = ...

  filename = "........ .raw"
  filesize = FileLen(filename)

  ReDim bytearray(1 To filesize)

  ff = FreeFile
  Open filename For Binary Access Read As ff
  Get ff, , bytearray()
  Close ff

  ReDim imagearray(1 To 4, 1 To wid, 1 To hgt)

  'The 4th byte of R G B X is "don't care", so can be anything.
  index = 1
  For icount = 1 To hgt
    For jcount = 1 To wid
      For kcount = 1 To 3
        imagearray(kcount, jcount, icount) = bytearray(index)
      Next kcount
      index = index + 1
    Next jcount
  Next icount

  With bmapinfo.bmiHeader
    .biSize = 40
    .biWidth = wid
    .biHeight = -hgt
    .biPlanes = 1
    .biBitCount = 32
    .biCompression = BI_RGB
  End With

  lret = SetDIBits(picDest.hdc, picDest.Image, 0, hgt, _
                     imagearray(1, 1, 1), _
                     bmapinfo, DIB_RGB_COLORS)

As I said before, for the SetDiBits to work here the .Image size of
picDest _must_ be the same size as the image you are working from. You
said your image size is 416 * 416 so the pixel size of the PictureBox
client area must also be 416 x 416 i.e. with ScaleMode = Pixels then
ScalWidth = ScaleHeight = 416

--
Richard Mason



Wed, 09 Feb 2005 17:54:38 GMT  
 Create a Bitmap from RAW Data
Thank you Richard,

   that worked just fine!

   Ethiel



Tue, 15 Feb 2005 22:28:31 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Creating Bitmap files from data.

2. create a bitmap from an intptr to scanned data

3. Reading bitmap files into memory and creating bitmap

4. Binary Data in SQL 2000/Conver to Bitmap Data For Data Grid View

5. Append raw data to Access table

6. Printing Raw Data - Please help

7. Trncating Data to update in Raw Field in oracle from VB.net

8. Spooling raw printer data

9. Problem with long raw data

10. ORACLE RAW LONG data type

11. How do I open a table named SPINDLE RAW DATA usiing ADO

12. Raw data to Printer device.

 

 
Powered by phpBB® Forum Software