CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 51
  1. #1
    Join Date
    Feb 2006
    Posts
    153

    From an array of HEX Codes to a collection of JPG files?

    Hi all,

    I am a array of hex codes, a portion of which is shown below.

    Code:
    	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x04, 0x00, 0x04, 0x7F, 0xE0, 
    	0x40, 0x04, 0x00, 0x48, 0x3F, 0xC0, 0x40, 0x04, 0x00, 0x40, 0x04, 0x00, 0x42, 0xFF, 0xF0, 0x00, 
    	0x00, 0x01, 0x10, 0xFF, 0xE1, 0x10, 0x07, 0x87, 0x80, 0x48, 0xC2, 0x50, 0x12, 0x4F, 0xFE, 0x04, 
    	0x03, 0x58, 0x44, 0x48, 0xC2, 0x04, 0x00, 0x00, 0x00, 0x01, 0x18, 0x0A, 0x07, 0xFC, 0x44, 0x44, 
    	0x44, 0x7F, 0xC4, 0x44, 0x44, 0x47, 0xFC, 0x04, 0x0F, 0xFF, 0x04, 0x00, 0x40, 0x04, 0x00, 0x00, 
    	0x00, 0x00, 0x04, 0x7F, 0xE0, 0x08, 0x3F, 0xCA, 0x4A, 0xA4, 0xAB, 0xFA, 0xA4, 0xAA, 0x4A, 0xBF,
    each "0x00" represents one byte and every 24 bytes represent a character. My question is how can i take this array n generate its equivalent character into seperate JPG files? i.e take 1st 24bytes, convert, then save as a jpeg file, then take the 2nd 24bytes, convert n save as 2nd jpeg file and etc..

    Can anyone help me pls?

    Thks
    FT

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: From an array of HEX Codes to a collection of JPG files?

    When you say "character", what does that mean exactly? Since Ascii characters and even Unicode characters aren't 24 bytes, I'm sure you must mean something else. Is the data a graphic? If so, what are the dimensions? There are a number of ways such data might be used, depending on things like color for example.

    I'd guess this is character data for a game, yes? Any details you have will be helpful.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    Quote Originally Posted by WizBang
    When you say "character", what does that mean exactly? Since Ascii characters and even Unicode characters aren't 24 bytes, I'm sure you must mean something else. Is the data a graphic? If so, what are the dimensions? There are a number of ways such data might be used, depending on things like color for example.

    I'd guess this is character data for a game, yes? Any details you have will be helpful.
    sorry for giving such an unclear description. When i mean character its actually a 12*16 image... Yes, the data is a graphic. Its plain black and white image.

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: From an array of HEX Codes to a collection of JPG files?

    OK. Then in that case, each pixel is represented by 1 bit. Now the thing to figure out is which way the image is "drawn". In other words, is the first bit the upper left, lower right, or whereever, and is the second bit to the right of it, or above/below that.

    I don't know if the data could be used directly by a monochrome bitmap, but it might be interesting to try and see what image you get, if anything. If that works, then you'd not need to draw it out bit by bit.

    It would probably be best if you attached an image, and the corresponding data. Then we can all have a look at it.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    Quote Originally Posted by WizBang
    OK. Then in that case, each pixel is represented by 1 bit. Now the thing to figure out is which way the image is "drawn". In other words, is the first bit the upper left, lower right, or whereever, and is the second bit to the right of it, or above/below that.

    I don't know if the data could be used directly by a monochrome bitmap, but it might be interesting to try and see what image you get, if anything. If that works, then you'd not need to draw it out bit by bit.

    It would probably be best if you attached an image, and the corresponding data. Then we can all have a look at it.
    the image is drawn from top left to top right, then all the way back to left, etc.. in (ROW, COLUMN) notation, it will be from (1,1) to (1,n) then (2,1) to (2,n) till (m,1) to (m,n) where m is the max row, n is max col.

    okies, .. the data for the attached image is as below , all in HEX codes...

    Code:
     0x00	 0x00	 0xC0	 0x04	 0x00	 0x04	 0x7F	 0xE0
    0x40	 0x04	 0x00	 0x48	 0x3F	 0xC0	 0x40	 0x04         0x00	 0x40	 0x04	 0x00	 0x42	 0xFF	 0xF0	 0x00
    its actually a black n white picture.. so can u pls elaborate what do u mean by "I don't know if the data could be used directly by a monochrome bitmap, "?? how to directly use monochrome bitmap?

    *NOTE: the attached image has equally divided grids. of 12 * 16 pixels.. for every black color in the pixel, its bit representation will be "1"

    thks

    FT
    Attached Images Attached Images  
    Last edited by fulltime; April 26th, 2006 at 03:14 AM.

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: From an array of HEX Codes to a collection of JPG files?

    Ah, I see. It seems you already have these being rendered within your program, yes? I'd point out that the equivalent jpeg image will be substantially larger than the original 24 bytes of data. A gif image can be about 61 bytes, and a bitmap about 126. The jpeg would be several hundred bytes, and even with a LOT of compression (and looking very bad) it would still be over three hundred bytes.

    Anyway, it seems the bitmap data would be somewhat different, though there are similarities. I think the differences are due to padding, and of course the fact that your black pixels are the bits which are "on".

    A simple loop could be used to test the bits and render the image to a DC or a picturebox. Then save as you please from there. Is this a one-time conversion, or something your program needs to do all the time?
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  7. #7
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    actually, the output of HEX codes is in the code of an array. it is generated by my company's program. The image that u see is onli 1 out of the total of 256 images.. (i.e, there are actually 256 small images on 1 screen)

    For the entire image, an array is generated, part of which is shown on my 1st post.. so wat i aim to do is to take the entire array n generate back 256 INDIVIDUAL jpg, or bmp files..

    are u able to guide me?

  8. #8
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    attached is part of the screen shot of the entire image...
    Attached Images Attached Images  

  9. #9
    Join Date
    Dec 2001
    Posts
    6,332

    Re: From an array of HEX Codes to a collection of JPG files?

    OK. This is making more sense now.

    But wow, the way these characters have been designed...I can guess why it was done the way it was, but it's a little awkward IMHO. Looking at the image and the data, you'll notice that the lowest bit is the right-most pixel of any given group. So it's sorta right-to-left, if you get what I mean.

    Anyway, here's a little demo to help you out. Plop a command button on a form, paste in the following code and click to see your character drawn. I wasn't gonna do the work for you, but I just HAD to figure what the deal was with those bits!
    Code:
    Option Explicit
    Private Declare Function SetPixelV Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal crColor As Long) As Long
    
    Private Sub Command1_Click()
    Dim I As Long, Bit As Long, A() As String, B() As Byte
    
    'build an array just for demo purposes
    A = Split("00 00 C0 04 00 04 7F E0 40 04 00 48 3F C0 40 04 00 40 04 00 42 FF F0 00", " ")
    ReDim B(UBound(A))
    For I = 0 To UBound(B)
      B(I) = Val("&h" & A(I))
    Next
    
    'draw that character!
    For I = 0 To UBound(B)
      For Bit = 0 To 7
        If B(I) And 2 ^ Bit Then
          SetPixelV Me.hDC, (7 - Bit + I * 8) Mod 12, (7 - Bit + I * 8) \ 12, &H0
        End If
      Next
    Next
    End Sub
    OK, now I can get back to work...
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  10. #10
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: From an array of HEX Codes to a collection of JPG files?

    Basically, all you need to do is convert the data and then use it do whatever you want to create the actual image. The bytes are laid out from upper left, to lower right, with each bit indicating black or white. Just read out each individual bit and into a 12 x 16 array, and you're fine (pic below). Some sample code:

    Code:
    Private Function ConvertArray(bInput() As Byte) As Byte()
    
        Dim lIndex As Long, bOut() As Byte, lRow As Long, lCol As Long, iBit As Integer
        
        If UBound(bInput) = 23 Then             'Bound check.
            ReDim bOut(15, 11)                  'Output is a 16 row by 12 col array.
            For lIndex = 0 To 23                'Loop through each byte.
                For iBit = 7 To 0 Step -1       'And each bit from high to low.
                    bOut(lRow, lCol) = BitSet(bInput(lIndex), iBit)
                    lCol = lCol + 1             'Increment the column.
                    If lCol = 12 Then           'See if we're on the next row.
                        lCol = 0                'Reset col to the left.
                        lRow = lRow + 1         'Increment the row.
                    End If
                Next iBit
            Next lIndex
            ConvertArray = bOut
        Else
            Debug.Print "Invalid input."
        End If
    
    End Function
    
    Private Function BitSet(ByVal bByte As Byte, iBitPos As Integer) As Byte
    
        bByte = bByte \ (2 ^ iBitPos)       'Shift the requested bit all the way to the right.
        BitSet = bByte Mod 2                'Return 1 or 0 to indicate whether it is set or not.
    
    End Function
    I'd made a little sample sub to test it with (I'm at work, so it's Excel VBA). Just drop this in a Workbook module and run it:
    Code:
    Private Sub TryIt()
    
        Dim bArray() As Byte, bFixed() As Byte, lRow As Long, lCol As Long
        
        ReDim bArray(23)
        
        bArray(0) = &H40: bArray(1) = &H4: bArray(2) = &H0: bArray(3) = &H48
        bArray(4) = &H3F: bArray(5) = &HC0: bArray(6) = &H40: bArray(7) = &H4
        bArray(8) = &H40: bArray(9) = &H4: bArray(10) = &H0: bArray(11) = &H48
        bArray(12) = &H3F: bArray(13) = &HC0: bArray(14) = &H40: bArray(15) = &H4
        bArray(16) = &H0: bArray(17) = &H40: bArray(18) = &H4: bArray(19) = &H0
        bArray(20) = &H42: bArray(21) = &HFF: bArray(22) = &HF0: bArray(23) = &H0
        
        bFixed = ConvertArray(bArray)
    
        For lRow = 0 To 15
            For lCol = 0 To 11
                Application.ActiveSheet.Cells(lRow + 2, lCol + 2).Interior.ColorIndex = bFixed(lRow, lCol)
            Next lCol
        Next lRow
    
    End Sub
    Attached Images Attached Images  
    Last edited by Comintern; April 26th, 2006 at 09:38 AM. Reason: typo

  11. #11
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    516

    Re: From an array of HEX Codes to a collection of JPG files?

    Ack!! Looks like Wiz beat me to it.
    I guess I can get back to work too.

  12. #12
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    thks to the both of u... i am at home now and i will try it when i get back to office.. will post here if i require any further help..

    i have joined this forum for onli 2 mths and the help i received has been tremendous... i realli appreciate all the effort and time spent on helping me solve my problem..

    cheers!

    FT

  13. #13
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    hi Wizbang,

    i have problems executing ur code..

    it gives me an error "method or data member not found" referring to this statement SetPixelV Me.hDC, (7 - Bit + I * 8) Mod 12, (7 - Bit + I * 8) \ 12, &H0 with the error on Me.hDC..


    btw, can u kindly explain to me whats the purpose of this funciton, SetPixelV?? and how do u come up with this formula "(7 - Bit + I * 8) Mod 12, (7 - Bit + I * 8) \ 12, &H0"?
    thks..
    Last edited by fulltime; April 26th, 2006 at 09:21 PM.

  14. #14
    Join Date
    Feb 2006
    Posts
    153

    Re: From an array of HEX Codes to a collection of JPG files?

    thks for the sample codes guys.. but if i wan to save the output to a BMP or JPEG file, how can i get it done? I been sourcing the net, but it seems like theres no way i can read in the array n construct the BMP/JPG file from it...

    any ideas?

    btw, Wizbang, i still cant figure out whats missing for ur sample code...did i forget to include any libraries?

  15. #15
    Join Date
    Dec 2001
    Posts
    6,332

    Re: From an array of HEX Codes to a collection of JPG files?

    Quote Originally Posted by fulltime
    thks for the sample codes guys.. but if i wan to save the output to a BMP or JPEG file, how can i get it done? I been sourcing the net, but it seems like theres no way i can read in the array n construct the BMP/JPG file from it...

    any ideas?

    btw, Wizbang, i still cant figure out whats missing for ur sample code...did i forget to include any libraries?
    You can use the SavePicture function to save to a BMP. I don't know why you are having trouble with the hDC property of a form. We are talking about VB 6.0, right?

    If you want, you can use a picturebox instead, which also has the hDC property. Then after the pixels have been set, use Set Picture1.Picture = Picture1.Image. Then you can save the BMP, clear the picture by setting it to Nothing, and do the next character.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Page 1 of 4 1234 LastLast

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

Featured