Generated png as hard from image and then load image to it

Just starting out? Need help? Post your questions and find answers here.
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Generated png as hard from image and then load image to it

Post by threedslider »

I would like to generate to image png in datasection and then to load it

Here the code example :

Code: Select all

EnableExplicit

Define FileID, OutFileID
Define FileSize, i
Define Byte.b
Define Count = 0

; Open the PNG in binary mode
FileID = ReadFile(#PB_Any, "image.png")
If FileID = 0
  MessageRequester("Error", "Cannot open image.png")
  End
EndIf

FileSize = Lof(FileID)

; Create the output PB file
OutFileID = CreateFile(#PB_Any, "ImageData.pb")
If OutFileID = 0
  MessageRequester("Error", "Cannot create ImageData.pb")
  CloseFile(FileID)
  End
EndIf

; DataSection header
WriteStringN(OutFileID, "DataSection")
WriteStringN(OutFileID, "  ImagePNG:")

; Read PNG in binary
For i = 0 To FileSize - 1

  Byte = ReadByte(FileID)

  ; New line every 64 values
  If Count % 64 = 0
    If Count > 0
      WriteStringN(OutFileID, "")
    EndIf
    WriteString(OutFileID, "    Data.b ")
  Else
    WriteString(OutFileID, ", ")
  EndIf

  WriteString(OutFileID, "$" + RSet(Hex(Byte & $FF), 2, "0"))
  Count + 1

Next i

; End DataSection
WriteStringN(OutFileID, "")
WriteStringN(OutFileID, "EndDataSection")

CloseFile(FileID)
CloseFile(OutFileID)

MessageRequester("OK", "Raw PNG DataSection generated 👍")
End
And in another code file for loading :

Code: Select all

UsePNGImageDecoder()

;
; ==== > Put your DataSection here!
;

  ; -------------------------------
; Usage in the program
; -------------------------------

; Load the image from the DataSection
If CatchImage(0, ?imagePNG)
  
  ; Create a window to display the image
  If OpenWindow(0, 100, 100, 200, 200, "Display PNG from DataSection")
    ImageGadget(0, 10, 10, 180, 180, 0)
    
    ; Attach the captured image to the image gadget
    SetGadgetState(0, #True)  ; refresh display
    StartDrawing(ImageOutput(0))
      DrawImage(0, 0, 0, 100, 61)
    StopDrawing()
    
    ; Wait loop
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
    
  EndIf
  
Else
  MessageRequester("Error", "Cannot load PNG from DataSection")
EndIf
Work but it doesn't load image, say, 100 x 61 pixels ... How to resolve this ? Thanks in advance :)
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Generated png as hard from image and then load image to it

Post by infratec »

For CatchImage() you need binary data and no 'Hex' bytes.

Code: Select all


CatchImage(0, ?imagePNG, ?imagePNGEnd - ?imagePNG)

DataSection
   ImagePNG:
   IncludeBinary "image.png"
   ImagePNGEnd:
EndDataSection
No need to convert it to hex :wink:
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Generated png as hard from image and then load image to it

Post by infratec »

Code: Select all

UsePNGImageDecoder()

DataSection
  ImagePNG:
  IncludeBinary "image.png"
  ImagePNGEnd:
EndDataSection

; -------------------------------
; Usage in the program
; -------------------------------

; Load the image from the DataSection
If CatchImage(0, ?imagePNG, ?imagePNGEnd - ?imagePNG)
  
  ; Create a window to display the image
  If OpenWindow(0, 100, 100, 200, 200, "Display PNG from DataSection")
    ImageGadget(0, 10, 10, 180, 180, ImageID(0))
    ; Wait loop
    Repeat
    Until WaitWindowEvent() = #PB_Event_CloseWindow
  EndIf
  
Else
  MessageRequester("Error", "Cannot load PNG from DataSection")
EndIf
or

Code: Select all

ImageGadget(0, 10, 10, 180, 180, 0)
SetGadgetState(0, ImageID(0))
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: Generated png as hard from image and then load image to it

Post by threedslider »

@infratec : Thanks it works from that but how @miso make an image embedded into the memory ? See the link here : viewtopic.php?p=650255#p650255
infratec
Always Here
Always Here
Posts: 7834
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Generated png as hard from image and then load image to it

Post by infratec »

Your ImageData.pb works too, if ...

you use

Code: Select all

ImageGadget(0, 10, 10, 180, 180, ImageID(0))
or

Code: Select all

ImageGadget(0, 10, 10, 180, 180, 0)
SetGadgetState(0, ImageID(0))
threedslider
Enthusiast
Enthusiast
Posts: 610
Joined: Sat Feb 12, 2022 7:15 pm

Re: Generated png as hard from image and then load image to it

Post by threedslider »

Thank you @infratec ! It works now :shock:
Post Reply