Page 1 of 1
Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 6:55 pm
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?
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 9:04 pm
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
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 9:10 pm
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
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 9:26 pm
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
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 9:41 pm
by davido
@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!
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Wed Aug 14, 2013 10:32 pm
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.

Re: Extracting and displaying jpg pictures from a zip file.
Posted: Thu Aug 15, 2013 7:52 am
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.

Re: Extracting and displaying jpg pictures from a zip file.
Posted: Thu Aug 15, 2013 8:25 am
by TI-994A
davido wrote:You're not the only one smiling!
Thank you for saying so!
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.
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Thu Aug 15, 2013 9:45 am
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.
Re: Extracting and displaying jpg pictures from a zip file.
Posted: Thu Aug 15, 2013 10:48 am
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.