HomeForumSourceResearchGuide
Sign in to contribute to source. how it works
Component media.image.ImageEncoder:jpg by barry
expand copy to clipboardexpand
interface JPGLib {
	int loadImage(byte fdata[], PixelMap b)
	byte[] saveImage(PixelMap b)
	}

component provides ImageEncoder:jpg requires native JPGLib lib {
	
	PixelMap map
	
	ImageEncoder:ImageEncoder()
		{
		}
	
	PixelMap ImageEncoder:getPixels()
		{
		return map
		}
	
	void ImageEncoder:setPixels(PixelMap pm)
		{
		map = pm
		}
	
	bool ImageEncoder:loadImage(File fd)
		{
		map = new PixelMap(new WH())
		
		int pos = fd.getPos()
		
		byte fdata[] = fd.read(fd.getSize() - pos)
		
		int flen = lib.loadImage(fdata, map)
		
		if (flen == 0)
			throw new Exception("failed to load PNG")
		
		return true
		}
	
	bool ImageEncoder:saveImage(File fd)
		{
		if (map == null || map.size == null) throw new Exception("no image data to encode")
		if (map.size.width == 0 || map.size.height == 0) throw new Exception("image data has a zero dimension")
		
		byte fdata[] = lib.saveImage(map)
		
		if (fdata == null)
			throw new Exception("failed to encode PNG")
		
		fd.write(fdata)
		
		return true
		}
	
	}
Revision history
To propose a new revision to this entity, use dana source put -uc your/new/version.dn -n media.image.ImageEncoder:jpg -m "reason for update" -u yourUsername
Version 2 by barry
Version 1 (this version) by barry
Notes for this version: Standard Library Initialisation