Results 1 to 15 of 15

Thread: [RESOLVED] Save 3 different PNG files into 1 PNG ARGB

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Resolved [RESOLVED] Save 3 different PNG files into 1 PNG ARGB

    Hi

    i am definitely out of my comfort zone here, i am trying to save 3 different PNG images into 1 image,

    image 1 into R channel
    image 2 into G channel
    image 3 into B channel

    i suppose you could call it channel packing, does anyone have any pointers or documentation on how i may achieve this?

    thank you in advance

    Kev

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: Save 3 different PNG files into 1 PNG ARGB

    I didn't try this but it isn't that much code.

    https://www.codeproject.com/Articles...-Images-in-NET
    Please remember next time...elections matter!

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    Hi Thank you, that's not quite what i meant you are talking about layers, i mean the RGB Channels

    i need to create a new image from 3 different images

    PictureBox1 = AO Image
    PictureBox2 = Metalness Image
    PictureBox3 = Gloss Image

    PictureBox4 = New Image

    PictureBox1 --> Red Channel
    PictureBox2 --> Green Channel
    PictureBox3 --> Blue Channel

    PictureBox4 save as new PNG image (Surface)

    So if you view the image in Photoshop this is how it would be setup:

    Name:  RGB.jpg
Views: 187
Size:  26.0 KB
    Attached Images Attached Images  
    Last edited by KevanH; Oct 13th, 2020 at 10:13 AM.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Save 3 different PNG files into 1 PNG ARGB

    Something went wrong with that attachment.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    fixed it thank you

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    I have almost got this but not quite right my code is awful and needs optimising, the RGB values are where i am failing :

    Code:
    Public Class FRMxSurface
        Dim AObmp, GLOSSbmp, METALbmp, SURFACEbmp As Bitmap
        Dim clrAO, clrAOTemp, clrGLOSS, clrGLOSSTemp, clrMETAL, clrMETALTemp, clrSURFACE, clrSURFACETempR, clrSURFACETempG, clrSURFACETempB As Color
    
        Private Sub PBsurfaceMap_Click(sender As Object, e As EventArgs) Handles PBsurfaceMap.Click
    
        End Sub
    
        Dim intR, intG, intB, intTemp As Integer
        Private Sub BTNCreate_Click(sender As Object, e As EventArgs) Handles BTNCreate.Click
    
            'test for images
            If PBaoMap.Image Is Nothing Then
                Exit Sub
            ElseIf PBglossMap.Image Is Nothing Then
                Exit Sub
            ElseIf PBmetalMap.Image Is Nothing Then
                Exit Sub
            End If
            PBTempIMAGE.Image = PBglossMap.Image
            Dim xao, yao, xg, yg, xm, ym, xs, ys As Integer
    
            AObmp = New Bitmap(PBaoMap.Image)
            GLOSSbmp = New Bitmap(PBglossMap.Image)
            METALbmp = New Bitmap(PBmetalMap.Image)
    
            For xao = 0 To AObmp.Width - 1
                For yao = 0 To AObmp.Height - 1
                    clrAO = AObmp.GetPixel(xao, yao)
                    clrAOTemp = Color.FromArgb(clrAO.R, clrAO.G, clrAO.B)
    
                Next
            Next
    
            For xg = 0 To GLOSSbmp.Width - 1
                For yg = 0 To GLOSSbmp.Height - 1
                    clrGLOSS = GLOSSbmp.GetPixel(xg, yg)
                    clrGLOSSTemp = Color.FromArgb(clrGLOSS.R, clrGLOSS.G, clrGLOSS.B)
    
                Next
            Next
    
            For xm = 0 To METALbmp.Width - 1
                For ym = 0 To METALbmp.Height - 1
                    clrMETAL = METALbmp.GetPixel(xm, ym)
                    clrMETALTemp = Color.FromArgb(clrMETAL.R, clrMETAL.G, clrMETAL.B)
    
                Next
            Next
            SURFACEbmp = New Bitmap(PBglossMap.Image)
    
            For xs = 0 To SURFACEbmp.Width - 1
                For ys = 0 To SURFACEbmp.Height - 1
    
                    clrSURFACETempR = Color.FromArgb(clrAOTemp.R, clrAOTemp.G, clrAOTemp.B)
                    clrSURFACETempG = Color.FromArgb(clrGLOSSTemp.R, clrGLOSSTemp.G, clrGLOSSTemp.B)
                    clrSURFACETempB = Color.FromArgb(clrMETALTemp.R, clrMETALTemp.G, clrMETALTemp.B)
    
                    clrSURFACE = Color.FromArgb(clrSURFACETempR.R, clrSURFACETempG.G, clrSURFACETempB.B)
    
    
                    SURFACEbmp.SetPixel(xs, ys, clrSURFACE)
                Next
            Next
    
            PBsurfaceMap.Image = SURFACEbmp
            MsgBox("All Done", vbOKOnly, "Result")
    
        End Sub
    Name:  nearly.jpg
Views: 212
Size:  11.9 KB

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Save 3 different PNG files into 1 PNG ARGB

    You'll be wanting to study this thread:

    https://www.vbforums.com/showthread....mies-and-Dudes
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    Quote Originally Posted by Shaggy Hiker View Post
    You'll be wanting to study this thread:

    https://www.vbforums.com/showthread....mies-and-Dudes
    Thank you that certainly speeds things up i just have to work out why its not picking up all pixels, the images are in the correct channels but obviously don't show all details

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    here is the revised code, i am sure i will work it out but does anyone see anything obvious ??

    Code:
    Public Class FRMxSurface
        Dim AObmp, GLOSSbmp, METALbmp, SURFACEbmp As Bitmap
        Dim clrAO, clrAOTemp, clrGLOSS, clrGLOSSTemp, clrMETAL, clrMETALTemp, clrSURFACE, clrSURFACETemp, clrSURFACETempR, clrSURFACETempG, clrSURFACETempB As Color
        Dim xao, yao, xg, yg, xm, ym, xs, ys As Integer
        Dim AOColor, GLOSSColor, METALColor As Integer
        Private Sub PBsurfaceMap_Click(sender As Object, e As EventArgs) Handles PBsurfaceMap.Click
    
        End Sub
    
        Dim intR, intG, intB, intTemp As Integer
        Private Sub BTNCreate_Click(sender As Object, e As EventArgs) Handles BTNCreate.Click
    
            'test for images
            If PBaoMap.Image Is Nothing Then
                Exit Sub
            ElseIf PBglossMap.Image Is Nothing Then
                Exit Sub
            ElseIf PBmetalMap.Image Is Nothing Then
                Exit Sub
            End If
            PBTempIMAGE.Image = PBglossMap.Image
            Dim xao, yao, xg, yg, xm, ym, xs, ys As Integer
    
            AObmp = New Bitmap(PBaoMap.Image)
            GLOSSbmp = New Bitmap(PBglossMap.Image)
            METALbmp = New Bitmap(PBmetalMap.Image)
    
            Using fp As New FastPix(AObmp)
                For xao = 0 To AObmp.Width - 1
                    For yao = 0 To AObmp.Height - 1
                        clrAO = fp.GetPixel(xao, yao)
                        clrAOTemp = Color.FromArgb(clrAO.R, clrAO.G, clrAO.B)
    
                    Next
                Next
            End Using
    
            Using fp As New FastPix(GLOSSbmp)
                For xg = 0 To GLOSSbmp.Width - 1
                    For yg = 0 To GLOSSbmp.Height - 1
                        clrGLOSS = fp.GetPixel(xg, yg)
                        clrGLOSSTemp = Color.FromArgb(clrGLOSS.R, clrGLOSS.G, clrGLOSS.B)
    
                    Next
                Next
            End Using
    
            Using fp As New FastPix(METALbmp)
                For xm = 0 To METALbmp.Width - 1
                    For ym = 0 To METALbmp.Height - 1
                        clrMETAL = fp.GetPixel(xm, ym)
                        clrMETALTemp = Color.FromArgb(clrMETAL.R, clrMETAL.G, clrMETAL.B)
    
                    Next
                Next
            End Using
    
            SURFACEbmp = New Bitmap(PBTempIMAGE.Image)
    
            Using fp As New FastPix(SURFACEbmp)
                For xs = 0 To SURFACEbmp.Width - 1
                    For ys = 0 To SURFACEbmp.Height - 1
                        clrSURFACE = fp.GetPixel(xs, ys)
                        clrSURFACETemp = Color.FromArgb(clrAO.R, clrGLOSS.G, clrMETAL.B)
                        fp.SetPixel(xs, ys, clrSURFACETemp)
                    Next
                Next
            End Using
            PBsurfaceMap.Image = SURFACEbmp
            MsgBox("All Done", vbOKOnly, "Result")
    
    
        End Sub

  10. #10
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Save 3 different PNG files into 1 PNG ARGB

    You are looping through all the pixels in AObmp reading the colour of each pixel, but not doing anything with it. The same for GLOSSbmp and METALbmp.

    What you end up with is clrAo, clrGLOSS and clrMETAL holding the colour of the very last (bottom right) pixel in each of the respective images, and using just those 3 colours to set every pixel in the output image. Hence the uniform colour.

    What you probably want is a single loop that, on each iteration of the loop, reads the colour of a single pixel from each of the 3 input images and uses the necessary parts of those 3 colours to set the corresponding pixel colour in the output image.

  11. #11

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    Thank you, i will try and figure that out thankfully all source images are exactly the same size (power of 2).

    so as i get a pixel i need to set a pixel on the output image ? the single loop how would i set that up so that i could setpixel at same time ?

    sorry for being dumb

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Save 3 different PNG files into 1 PNG ARGB

    Quote Originally Posted by KevanH View Post
    thankfully all source images are exactly the same size (power of 2).
    That's a good job :-p

    So you can get the width and height from any of the 3 images and loop over all the pixel columns and rows as you are already doing.

    Read the pixel at (x,y) from each of the three input images and create a new colour from the required component channels. Then use the chimera colour to set the colour of the pixel at (x,y) in the output image. Something like this (untested):
    Code:
    AObmp = New Bitmap(PBaoMap.Image)
    GLOSSbmp = New Bitmap(PBglossMap.Image)
    METALbmp = New Bitmap(PBmetalMap.Image)
    SURFACEbmp = New Bitmap(PBTempIMAGE.Image)
    
    Using fpAO As New FastPix(AObmp),
            fpGLOSS As New FastPix(GLOSSbmp),
            fpMETAL As New FastPix(METALbmp),
            fpSurface As New FastPix(SURFACEbmp)
    
        For x = 0 To AObmp.Width - 1
            For y = 0 To AObmp.Height - 1
                clrAO = fpAO.GetPixel(x, y)
                clrGLOSS = fpGLOSS.GetPixel(x, y)
                clrMETAL = fpMETAL.GetPixel(x, y)
    
                clrSURFACE = Color.FromArgb(clrAO.R, clrGLOSS.G, clrMETAL.B)
                fpSurface.SetPixel(x, y, clrSURFACE)
            Next
        Next
    
    End Using

  13. #13

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: Save 3 different PNG files into 1 PNG ARGB

    you Sir are a star thank you so much it works perfectly

    Name:  Worked.jpg
Views: 190
Size:  30.0 KB

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2020
    Posts
    9

    Re: [RESOLVED] Save 3 different PNG files into 1 PNG ARGB

    Hi i am back LOL,

    this solution is working perfectly, however some of the textures are in DDS format, can anyone recommend a library or wrapper for vb.net to open/save DDS files.

    One with samples would be better if possible, i have had a quick look at freeimage but its C# so do not know where to start

  15. #15
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: [RESOLVED] Save 3 different PNG files into 1 PNG ARGB

    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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