s c h e m a t i c s : c o o k b o o k

/ Cookbook.CreatingJpegWithMzScheme

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Creating Jpeg from scratch using MzScheme

Problem

You are using PLT DrScheme and you want to draw graphs, generate graphics, or process images from scratch and write out a jpeg.

Solution

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))))
  (if (file-exists? image-file)
      (delete-file image-file))
  (send bitmap save-file image-file 'jpeg 100))

Discussion

foo.jpg

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.


Comments about this recipe

Contributors

-- AustinKing - 29 Sep 2007

CookbookForm
TopicType: Recipe
ParentTopic: GUIRecipes
TopicOrder: 999

Attachment: Action: Size: Date: Who: Comment:
foo.jpg action 16807 29 Sep 2007 - 17:56 AustinKing Our Blue - Green Gradient where red is constant

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest