Creating Jpeg from scratch using MzScheme
You are using PLT
DrScheme and you want to draw graphs, generate graphics, or process images from scratch and write out a jpeg.
MrEd, a part of
MzScheme gives you functions for opening image files, writing images to disk, and for drawing on images once you have a device context.
Let's draw a 255 pixel by 255 pixel color gradient.
(let ((bitmap (make-object bitmap% 255 255))
(dc (make-object bitmap-dc%))
(red-amount 150)
(image-file "/tmp/foo.jpg"))
(send dc set-bitmap bitmap)
(do ((x 0 (+ x 1)))
((>= x 255))
(do ((y 0 (+ y 1)))
((>= y 255))
(send dc set-pixel x y
(make-object color% red-amount x y))))
(when (file-exists? image-file)
(delete-file image-file))
(send bitmap save-file image-file 'jpeg 100))
The code above when run, creates a jpg on you hard drive which is a color gradation where red is constantly 150 for all values and the green and blue vary from 0 to 254 across the image.
First we make a bitmap and a bitmap-dc. A bitmap-dc is a "device context" which allows us to draw into this new bitmap. We then connect them with the 'set-bitmap' message. We create a value for red 150 and an image path that should be legal for our computer. Windows users may need to change this to 'C:\Temp' etc.
We have two loops from 0 to 254 by increments of 1. This is our x and y, so that we can iterate across the bitmap. A color's value is made up of three componetnts - red, blue, and green. The values for each part can be from 0 to 254.
The point of our exercise is to send the 'dc' object messages such as 'set-pixel', so that can draw into the bitmap. The documentation for bitmap-dc% in PLT
MrEd Graphical Toolbox Manual describes all of the other drawing operations you can do, including drawing shapes and copying portions from other images into your bitmap.
Next we check if the image file exists, and if it does we delete it. And lastly we save the bitmap out in jpeg format at 100% quality.
MzScheme can read and write the jpeg format.
You can now open the file in an image viewer. Change the loop and experiment. Try using (random 255) when making a color.
--
AustinKing - 29 Sep 2007