[squeak-dev] Modern way to load a picture from the web?

Tim Johnson digit at sonic.net
Tue Apr 2 15:15:13 UTC 2019


Hi Hannes,

I have implemented this in various ways in my InternetArchive client library—a work in (slow) progress over the last 3+ years.  Note I did not realize a similar library had been written for Sophie ten years ago.  This one was based on the Python client library which uses the current InternetArchive API.

I finally made it public in its current state here: 

https://github.com/tcj/craggyaloofness <https://github.com/tcj/craggyaloofness>

Some examples for how to use it live in the IAExample* classes.  

The following will display the famous Byte cover and restore the display after a click:

(IAItem named: 'byte-magazine-1981-08' ) displayItemTileJpeg

Anyway, you can find my code in the 'displaying graphics' instance method category of IADownload.   Also note that you may be interested in seeing Form class>>#openImageInWindow:

Here is how I implemented some JPEG download & display in Squeak 5.2.  These methods are taken from the IADownload and perhaps IAItem classes.

getResponse
	"returns a WebResponse object"
	^ response := WebClient httpGet: self url.
	

downloadJpegIntoForm
	| s contents jww imageExtent form | 
	s := ByteArray new writeStream.
	s binary.
	self getResponse.
	response streamTo: s size: response contentLength progress: nil.
	response close.
	contents := s contents.
	(JPEGReadWriter2 understandsImageFormat: (contents readStream))
		ifFalse: [^ self error: 'cannot read jpeg'].
	jww := JPEGReadWriter2 new.
	imageExtent := jww imageExtent: contents.
	form := Form extent: imageExtent depth: 32.
	jww uncompress: contents into: form doDithering: false.
	^ form

displayJpeg
	| form | 
	form := self downloadJpegIntoForm.
	Display restoreAfter: [ form displayOn: Display ].


displayJpegInMorph
	| form morph | 
	form := self downloadJpegIntoForm.
	morph := BorderedMorph newBounds: (form bounds). 
	Display restoreAfter: [ form displayOn: Display ].


displayJpegInWindow
	| form morph window | 
	self flag: #seeFormClassSideForUtilityMethod.
	form := self downloadJpegIntoForm.
	morph := form asMorph.

	window := SystemWindow labelled: 'image'.
	window 
"		addMorphCentered: (form asMorph);"
		addMorph: (form asMorph) frame: (0.0 at 0.0 corner: 1.0 at 1.0) .
"		addMorph: (form asMorph) fullFrame: (SystemWindow menuBoxFrame) ."
	window 
		openInWorldExtent: (form extent scaleBy: 1.2 @ 1.2).



Let me know if I can help further.  I believe there is also code in there for displaying downloaded GIFs and animated GIFs but I could be wrong.

Best,
Tim






> On Mar 21, 2019, at 3:51 AM, H. Hirzel <hannes.hirzel at gmail.com <mailto:hannes.hirzel at gmail.com>> wrote:
> 
> Hello
> 
> What is a good modern way in Squeak to load a picture from the web?
> 
> I am looking for an equivalent of
> 
> | pngPicURL aStream |
> 
> pngPicURL := 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Squeak-x11.png/617px-Squeak-x11.png' <https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Squeak-x11.png/617px-Squeak-x11.png'>.
> 
> aStream := HTTPSocket httpGet: pngPicURL.
> 
> (ImageReadWriter formFromStream: aStream) asMorph openInHand
> 
> And answer or just comments are welcome.
> 
> Thank you.
> 
> Kind regards
> 
> Hannes
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20190402/759b5ec0/attachment.html>


More information about the Squeak-dev mailing list