Extracting and displaying jpg pictures from a zip file.

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Extracting and displaying jpg pictures from a zip file.

Post by davido »

Can anyone help, please?

I have created a zip file containing some jpg pictures. That was easy.

I now wish to unpack these pictures to memory and then display them on a CanvasGadget.
Can anyone tell me the correct procedure to do this, please?
DE AA EB
User avatar
Bisonte
Addict
Addict
Posts: 1313
Joined: Tue Oct 09, 2007 2:15 am

Re: Extracting and displaying jpg pictures from a zip file.

Post by Bisonte »

something like this ?

Code: Select all

UseJPEGImageDecoder()
UseZipPacker()

Procedure GetPackedImage(ArchivName.s, PictureName.s, PackPlugin = #PB_PackerPlugin_Zip)
  
  Protected Archiv, Size, *Buffer, Image
  
  If FileSize(ArchivName) > 0
    
    Archiv = OpenPack(#PB_Any, ArchivName, PackPlugin)
    
    If Archiv
      
      If ExaminePack(Archiv)
        
        While NextPackEntry(Archiv)
          
          If PackEntryName(Archiv) = PictureName
            
            Size = PackEntrySize(Archiv, #PB_Packer_UncompressedSize)
            *PictureBuffer = AllocateMemory(Size)
            
            If *PictureBuffer
              Result = UncompressPackMemory(Archiv, *PictureBuffer, Size, "abc.jpg")
              
              If Result = Size
                Image = CatchImage(#PB_Any, *PictureBuffer)
              EndIf  
              
              FreeMemory(*PictureBuffer)
              
            EndIf
          EndIf
          
        Wend
        
      EndIf
      
      ClosePack(Archiv)
      
    EndIf
    
  EndIf
  
  ProcedureReturn Image
  
EndProcedure

Image = GetPackedImage("d:\abc.zip","abc.jpg")

If IsImage(Image)
  Debug "Picture : "+Str(Image)
EndIf

If Not OpenWindow(0, 0, 0, 640, 480, "TestWindow", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) : End : EndIf

CanvasGadget(0,0,0,640,480)

If StartDrawing(CanvasOutput(0))
  If IsImage(Image)
    DrawImage(ImageID(Image), 0, 0)
  EndIf
  StopDrawing()
EndIf
  
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
JHPJHP
Addict
Addict
Posts: 2257
Joined: Sat Oct 09, 2010 3:47 am

Re: Extracting and displaying jpg pictures from a zip file.

Post by JHPJHP »

If your using PureZip (PureBasic 5.11 - 32bit):

I don't think PureZip is ready for the latest Beta release... I was getting a CRC error...

Code: Select all

ZipFile.s = "C:\Users\Administrator\Desktop\test.zip"

If PureZIP_Archive_Read(ZipFile)
  UseJPEGImageDecoder()
  myFileinfo.PureZIP_FileInfo
  ReturnValue = PureZIP_Archive_FindFirst()
  
  While ReturnValue = #UNZ_OK
    PureZIP_Archive_FileInfo(@myFileinfo)
    
    If myFileinfo\unCompressedSize > 0
      *Buffer = AllocateMemory(myFileinfo\unCompressedSize)
      
      If *Buffer
        If PureZIP_Archive_ExtractMem(*Buffer, myFileinfo\unCompressedSize)
          If LCase(GetExtensionPart(myFileinfo\FileName)) = "jpg"
            ZipImage = CatchImage(#PB_Any, *Buffer, MemorySize(*Buffer))
            
            If IsImage(ZipImage)
              ;...
              
              FreeImage(ZipImage)
            EndIf
          EndIf
        EndIf
        FreeMemory(*Buffer)
      EndIf
    EndIf
    ReturnValue = PureZIP_Archive_FindNext()
  Wend
  PureZIP_Archive_Close()
EndIf

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Extracting and displaying jpg pictures from a zip file.

Post by falsam »

Another contribution (PB 5.20 Beta 10)

Code: Select all

EnableExplicit

Enumeration
  #ZipFile
  
  #Mainform
  #ListImg
  #Canvas
  #Image
EndEnumeration

Define.l Event, GEvent

Global WindowStyle.i=#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_ScreenCentered

Global ZipFile.s

Procedure ShowZipContent()
  Protected PackEntryName.s
  
  ZipFile = OpenFileRequester("Open file ...","", "zip files|*.zip", 0)

  If ZipFile <> ""
        
    OpenPack(#ZipFile, ZipFile, #PB_PackerPlugin_Zip)

    If ExaminePack(#ZipFile)
      While NextPackEntry(#ZipFile)
        
        PackEntryName = PackEntryName(#ZipFile)
      
        Select PackEntryType(#ZipFile)
      
          Case #PB_Packer_File
            AddGadgetItem(#ListImg, -1, PackEntryName)
                
        EndSelect
      Wend  
    EndIf
  EndIf
  
  ClosePack(#ZipFile)
EndProcedure


Procedure ShowImage(ImageName.s)
  Protected PackEntryName.s, ImageSize.i, *Image
  
  OpenPack(#ZipFile, ZipFile, #PB_PackerPlugin_Zip)
    
  ; Search entry name
  If ExaminePack(#ZipFile)
    While NextPackEntry(#ZipFile)
      
      PackEntryName = PackEntryName(#ZipFile)

      Select PackEntryType(#ZipFile)
        
        Case #PB_Packer_File
          If PackEntryName = ImageName
            ;Uncompress PackEntryName in memory 
            ImageSize = PackEntrySize(#ZipFile)
            *Image = AllocateMemory(ImageSize)
            UncompressPackMemory(#ZipFile, *Image, ImageSize)
    
          EndIf  
      EndSelect
    Wend  
  EndIf
      
  ClosePack(#ZipFile)
  
  ; Show image
  CatchImage(#Image, *Image, ImageSize)
    
  StartDrawing(CanvasOutput(#Canvas))
  DrawingMode(#PB_2DDrawing_Default)
  Box(0, 0, 370, 350) ;Canvas Clear
  DrawImage(ImageID(#Image), 0, 0)
  StopDrawing()
  
  FreeMemory(*Image)
  
EndProcedure

Procedure Open_MainForm()
  OpenWindow(#Mainform, 0, 0, 600, 400, "Unpack image in memory", WindowStyle)
  ListViewGadget(#ListImg, 10, 10, 200, 350)
  CanvasGadget(#Canvas, 220, 10, 370, 350)
EndProcedure

UseJPEGImageDecoder()
UsePNGImageDecoder()
UseTGAImageDecoder()

UseZipPacker()  
Open_MainForm()
ShowZipContent()

Repeat
  Event  = WaitWindowEvent(10)
  GEvent = EventGadget()
  
  Select Event
    Case #PB_Event_Gadget
      Select GEvent
          
        Case #ListImg
          ShowImage(GetGadgetItemText(#ListImg, GetGadgetState(#ListImg)))
          
      EndSelect
        
    Case #PB_Event_CloseWindow
      End
      
  EndSelect
ForEver
Last edited by falsam on Thu Aug 15, 2013 9:39 am, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Extracting and displaying jpg pictures from a zip file.

Post by davido »

@Bisonte
@JHPJHP
@falsam

Thank you all very much for you help, it is much appreciated. :D :D

It is a real privilege to be a member of this forum!
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Extracting and displaying jpg pictures from a zip file.

Post by TI-994A »

davido wrote:I now wish to unpack these pictures to memory and then display them on a CanvasGadget.
Hello davido. This one opens the zip file, enumerates the contents in a ListViewGadget(), and then displays the image that is selected from the list:

Code: Select all

InitNetwork()
UseZipPacker()
UseJPEGImageDecoder()

Enumeration
  #MainWindow
  #List
  #Image
  #Button
  #ZipFile
EndEnumeration

Procedure InitZip()
  If ReceiveHTTPFile("https://dl.dropboxusercontent.com/u/38177172/images.zip", 
                     GetTemporaryDirectory() + "images.zip")
    If OpenPack(#ZipFile, GetTemporaryDirectory() + "images.zip")
      AddGadgetItem(#List, -1, "file: images.zip")
      If ExaminePack(#ZipFile)
        While NextPackEntry(#ZipFile)
          AddGadgetItem(#List, -1, PackEntryName(#ZipFile))
        Wend
      EndIf
      ClosePack(#ZipFile)
    EndIf
  EndIf
EndProcedure

Procedure DisplayZipItem(fileNum)
  Protected zippedImage, *imageLoc, imageSize, files
  If OpenPack(#ZipFile, GetTemporaryDirectory() + "images.zip")
    If ExaminePack(#ZipFile)
      While NextPackEntry(#ZipFile)
        files + 1
        If files = fileNum
          imageSize = PackEntrySize(#ZipFile)
          *imageLoc = AllocateMemory(imageSize)
          UncompressPackMemory(#ZipFile, *imageLoc, imageSize)
          zippedImage = CatchImage(#PB_Any, *imageLoc, imageSize)
          SetGadgetState(#Image, ImageID(zippedImage))
          FreeMemory(*imageLoc)
        EndIf
      Wend
    EndIf
    ClosePack(#ZipFile)
  EndIf
EndProcedure

OpenWindow(#MainWindow, #PB_Any, #PB_Any, 220, 180, "Unzip & Display Images", 
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ListViewGadget(#List, 10, 10, 100, 150)
ImageGadget(#Image, 120, 10, 85, 100, 0, #PB_Image_Raised)
InitZip()

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      appQuit = 1
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #List
          DisplayZipItem(GetGadgetState(#List))
      EndSelect
  EndSelect
Until appQuit
Hope you'll find it useful too. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Extracting and displaying jpg pictures from a zip file.

Post by davido »

Hi TI-994A,

You're not the only one smiling!

Thank you very much for the lovely demonstration.

The cartoons were a very nice touch. :D
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Extracting and displaying jpg pictures from a zip file.

Post by TI-994A »

davido wrote:You're not the only one smiling!
Thank you for saying so! :D

On the whole, falsam's example is clearly more comprehensive. However, it seems to allocate memory based on the entire zip file size:

Code: Select all

FileLength = Lof(#Zipfile)  
*Image = AllocateMemory(FileLength)
I believe that it may be better to allocate only for the particular entry being used:

Code: Select all

imageSize = PackEntrySize(#ZipFile)
*imageLoc = AllocateMemory(imageSize)
Just a suggestion.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Extracting and displaying jpg pictures from a zip file.

Post by falsam »

TI-994A wrote: ... However, it seems to allocate memory based on the entire zip file size ..... I believe that it may be better to allocate only for the particular entry being used.
TI-994A, you're right. I've updated the code. :)

PS : This code does not resize the image when displaying in canvasgadget.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
Fred
Administrator
Administrator
Posts: 18204
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Extracting and displaying jpg pictures from a zip file.

Post by Fred »

Especially as the extracted zip item can be way larger than the zip file size, you have to use the uncompressed zip item size.
Post Reply