Results 1 to 16 of 16

Thread: BMP to Matrix

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9
    Hi buddies:

    I wanna write a program in Visual Basic which can read a BMP file and convert it into a Matrix... can anybody have any idea or experience about this.. Help me.. i will really appreciate it..

    Thank you,
    Farshid

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Like a matrix of RGB values?

    (make sure to define GetPixel as in API Viewer...)

    (hehe, he called me a buddy ) - thanks man, means the world to me *sob*

    j/k

    Code:
    Dim hMat() as RGBT
    
    Type RGBT
       R as Single
       G as Single
       B as Single
    End Type
    
    Function GetR(ByVal pCol As Long) As Byte
       
       GetR = pCol Mod 256
    
    End Function
    
    Function GetG(ByVal pCol As Long) As Byte
        
       pCol = pCol \ 256
       GetG = pCol Mod 256
        
    End Function
    
    Function GetB(ByVal pCol As Long) As Byte
    
        pCol = (pCol \ 256) \ 256
        GetB = pCol Mod 256
    
    End Function
    
    Sub ConvertToMatix(hDC as Long, W, H, hMatrix as RGBT)
    dim TEMP as Long
       Redim hMatrix(0 to W, 0 to H) as RGBT
       Dim I, J
       For I = 0 to W
          For J = 0 to H
             TEMP = GetPixel(hDC, I, J)
             hMatrix(I, J).R = GetR(TEMP)
             hMatrix(I, J).G = GetG(TEMP)
             hMatrix(I, J).B = GetB(TEMP)
          Next J
       Next I
    End Sub
    
    'usage:
    
    ConvertToMatrix Pic1.hDC, Pic1.ScaleWidth, Pic1.ScaleHeight, hMat
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    Further explanation

    Hi budy Sastraxi:

    Thank you very much for the help... I am a beginner in Visual Basic Programming for Graphics.. The assignment that i have been given is that i should develop a program or rather a function which should open a bmp file, read it and convert save the image data in a matrix. So this is all what i want. can u help me in this regard. I will be really thankful to you for it..

    bye..
    Farshid

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    hmmm.. i thought i posted what you wanted....

    Ah well, please explain what you want to DO with this "matrix" after you have gathered the data (if not in the way i showed then what?)
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    if you cant explain it well here drop me an email:
    [email protected]

    And check out my soon-to-be website:
    http://vbden.com-1.net/
    -or-
    http://vbden.tripod.com/
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    Here is my problem

    Hi budy...

    Here is my problem.

    I want to write a program which can can input bmp image (Black and white colors only) and then read the bmp file... save the image information in a matrix (array) and then store the matrix in a file...

    this is the whole problem.. i am really working hard on it.. ur cooperation will be highly appreciated buddy...

    thanks for everything...
    farshid

  7. #7
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Allright! Use the above code and the usage line to convert to an array. The above code will work for 24-bit colour bitmaps. Anyways, then, once you have the array:

    Code:
    Function StoreMatrix(hMatrix as RGBT, FileName as String)
    
    Dim bI, bJ, I, J
    
    bI = Ubound(hMatrix, 1)
    bJ = Ubound(hMatrix, 2)
    
    Open FileName for Input as #1
    
    For I = 0 to bI: For J = 0 to bJ
    Put #1, CSTR(SplitColours(hMatrix(I, J))
    Next I: Next J
    
    Close #1
    
    End Function
    
    Function OpenMatrix(hMatrix as RGBT, FileName as String)
    
    Dim bI, bJ, I, J
    Dim tMatrix() as Long
    
    bI = Ubound(hMatrix, 1)
    bJ = Ubound(hMatrix, 2)
    
    Redim tMatrix(0 to bI, 0 to bJ) as Long
    
    Open FileName for Output as #1
    
    For I = 0 to bI: For J = 0 to bJ
    Get #1, tMatrix(I, J)
    hMatrix(I, J).R = GetR(tMatrix(I, J)
    hMatrix(I, J).G = GetG(tMatrix(I, J)
    hMatrix(I, J).B = GetB(tMatrix(I, J)
    Next I: Next J
    
    Close #1
    
    End Function
    
    Function SplitColours(Ins as RGBT) as Long
       SplitColours = RGB(Ins.R, Ins.G, Ins.B)
    End Function
    Just make sure to dimension the array properly when loading the file. (OpenMatrix)

    USAGE:

    [code]
    Dim hMat() as RGBT

    ConvertToMatrix hMat, 20, 20, Picture1.hDC
    StoreMatrix hMat, "C:\MyMat.XXX"
    ...
    Dim myMat(20, 20) as RGBT
    OpenMatrix myMat, "C:\MyMat.XXX"
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Afghanistan
    Posts
    9

    Wink Thanks

    Hi friend:

    Thank you very much for your help. I will study the code and see whether i will be able to solve my problem with it or not.. but hopefully i will... if anything i will just mail u..

    ok..thanks alot once again for ur cooperation...

    farshid

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I don't get it, the bmp file format contains this "matrix" already.

  10. #10
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    I think he wants to write it in plain text, so you could open it by just line inputting and saving that value in a pixel, until the end of file.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    eh, i thought he refered to an 2darray with "matrix"

  12. #12
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Well If you look at the code I posted then you will see that it is both.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  13. #13
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164

    I am not sure if I get this

    By taking a quick look on the first part of the code (posted by Sastraxi), I understand you split the three components of the BMP image.

    However, this is done using a handle to a picture control, isn't it ?? ( ConvertToMatrix Pic1.hDC, Pic1.ScaleWidth, Pic1.ScaleHeight, hMat )

    Can this be done by reading the bitmap as a BINARY FILE directly ? Do you know the binary format of the BMP files ?

  14. #14
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    You could load it, make a compatible DC with the DIB data, and then call the DC in along with the array and the height and width in the DIB Type structure.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  15. #15
    Addicted Member
    Join Date
    Mar 2001
    Location
    Greece
    Posts
    164

    Please explain more....

    Please explain more on the BMP binary file format. (type, length of records, headers etc.)

    Is that what you were trying to make me understand in your last reply ?

  16. #16
    Fanatic Member PsychoMark's Avatar
    Join Date
    Feb 2001
    Location
    Netherlands
    Posts
    540
    http://rookscape.com/vbgaming/tutAX.php

    This tutorials shows you two things, first of all how to load the bitmap data into UDTs and an array, and secondly how to use that data directly to blit the bitmap to a DC...

    You can easily pick out the first part though, which shows how the bitmap format is put together...
    Teaudirenopossum.Musasapientumfixaestinaure.
    (I can't hear you. There's a banana in my ear)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width