Extracting and displaying jpg pictures from a zip file.
Extracting and displaying jpg pictures from a zip file.
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?
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
Re: Extracting and displaying jpg pictures from a zip file.
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
Re: Extracting and displaying jpg pictures from a zip file.
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...
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 Stuff ➤ FREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Re: Extracting and displaying jpg pictures from a zip file.
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
Re: Extracting and displaying jpg pictures from a zip file.
@Bisonte
@JHPJHP
@falsam
Thank you all very much for you help, it is much appreciated.
It is a real privilege to be a member of this forum!
@JHPJHP
@falsam
Thank you all very much for you help, it is much appreciated.


It is a real privilege to be a member of this forum!
DE AA EB
Re: Extracting and displaying jpg pictures from a zip file.
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:davido wrote:I now wish to unpack these pictures to memory and then display them on a CanvasGadget.
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

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 

Re: Extracting and displaying jpg pictures from a zip file.
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.
You're not the only one smiling!
Thank you very much for the lovely demonstration.
The cartoons were a very nice touch.

DE AA EB
Re: Extracting and displaying jpg pictures from a zip file.
Thank you for saying so!davido wrote:You're not the only one smiling!

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)
Code: Select all
imageSize = PackEntrySize(#ZipFile)
*imageLoc = AllocateMemory(imageSize)
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 

Re: Extracting and displaying jpg pictures from a zip file.
TI-994A, you're right. I've updated the code.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.

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
Re: Extracting and displaying jpg pictures from a zip file.
Especially as the extracted zip item can be way larger than the zip file size, you have to use the uncompressed zip item size.