6.2.1 Picture albums and picture collections (Windows Phone and Android)
A smartphone normally holds various picture albums. On a Windows phone, they have names such as Camera Roll and Saved Favorites.
TouchDevelop provides access to these albums on the Windows Phone and will soon provide access to picture albums on Android devices. Unfortunately, security restrictions prevent access on the PC, Mac, Linux, iPad, iPhone and iPod Touch platforms.
On the platforms where it is supported, the API call
phone → picture albums
retrieves all the albums currently maintained on the phone, while the two method calls
phone → pictures
phone → saved pictures
return collections of all pictures, and the pictures held in the album named ‘saved pictures’, respectively. The methods for working with the Picture Album, Picture Albums and Pictures (a Picture collection) datatypes are listed in Table 6-3.
Once a Picture value has been obtained, perhaps by retrieving it from a collection, or by using the camera, there are many methods for manipulating the picture before it is displayed on the screen. These are covered in later sections of this chapter.
Table 6-3 Methods of Picture Album and Pictures datatypes (WP8 and Android)
6.2.2 Access to pictures on other devices
In the Web App version of TouchDevelop, a single picture can be selected from the device’s hard drive using the method media → choose picture. A picture can also be downloaded from the web via the call
Var pic := web → choose picture
or added to a script as an Art resource.
6.2.3 Manipulating a picture
Display of an image uses the post to wall method, as in this example.
var
pic1 := media → choose picture
pic1 → post to wall
An alternative way to display a picture is to set the wall’s background image, as follows.
var
pic1 := media → choose picture
wall → set background picture(pic1)
The general-purpose methods of the Picture type are listed in Table 6-4.
Table 6-4 General Picture methods
The table omits methods which change the colors or the brightness, or methods which overlay shapes etc. on top of the picture. All such methods are covered in the following subsections of this chapter.
Care in Using the at, pixel and set pixel methods
Table 6-4 includes the at, pixel and set pixel methods. Before any of these methods is used in a script, some thought should be given to how large the picture is.
Any TouchDevelop script which accesses every pixel of a picture can be exceedingly slow to run, as well as draining the battery of a portable device. The implication is that the Picture type’s at, pixel and set pixel methods should be used only on pictures containing a modest number of pixels. Pictures taken by the camera contain as many pixels as the camera’s resolution. For example, it is not uncommon for phone cameras to have 6 megapixel resolution or higher. Pictures downloaded from the internet or transferred from your computer may contain even more pixels.
Although a picture shown on the screen is scaled to fit within the screen’s size, the picture retains its original number of pixels in the device’s memory. Unless the picture is intended to be copied to another device, it would usually be appropriate to reduce the picture’s resolution to match the screen resolution. Note that any method which processes all the pixels in a single call, such as resize, is reasonably fast.
The at method is useful for determining various aggregate properties of a picture, such as its average brightness. In more sophisticated scripts, the pixel method could for example be used for analyzing a picture and extracting details such as edges or, when set pixel is used too, for sharpening edges.
An example script which computes a picture’s average brightness is shown in Figure 6-2. Each pixel in the picture has a color value composed from red (R), green (G) and blue (B) components whose values range from zero intensity or 0.0 up to the maximum intensity which is 1.0. From the R,G,B values of a pixel, its luminosity can be calculated. (See, for example, the explanation of the YUV color space and the conversion formula for computing YUV values in Wikipedia.) The luminosity is a measure of the brightness of that pixel.
The at and pixel methods are similar because they both retrieve the color of a particular pixel. Generally speaking, the at method should be used when it does not matter where the pixel is located within the picture, as is the case for the brightness calculation in Figure 6-2. It provides more efficient access because only one for loop to access all the pixels is needed. The pixel and set pixel methods would normally be placed inside two nested for loops, one to run through the rows and the other through the columns. The equivalence between the two ways to access a particular pixel is as follows.
pic1 → pixel(x,y) ≡ pic1 → at( y*pic1 → width + x )
Note that y coordinate values are measured from the top edge of the picture down. It is the opposite convention to that used in geometry.
Picture colorizing effects
The colors, the contrast and the brightness of a picture can all be modified using more methods of the Picture type which are listed in Table 6-5.
The brightness method can be used to increase or decrease the luminosities of all the pixels in the image in unison, so that the picture appears brighter or darker. The contrast method can be used to increase or decrease the range of luminosities, so that there is greater or smaller contrast between light and dark regions.
The colorize method is intended for creating a two color image from a greyscale image. All pixels darker than a specified threshold value (a number in the range 0.0 to 1.0) are replaced by the background color, while all those brighter are replaced by the foreground color. The method can also be applied to color images, but that image is converted to grayscale before the colorization is applied.
Table 6-5 Colorizing/intensity picture effects
The final picture will no longer have any variations in intensity. All pixels of the foreground color have the same intensity, as do all those with the background color.
The invert method produces a result similar to a color negative, as would be observed with a 35mm camera using chemically developed color film. (This is a type of camera which is becoming rare.)
Picture overlaying
The next section in this chapter, section 6.3, is all about drawing text, lines and various shapes on top of a picture. What about superimposing another picture on top of a picture? That facility is provided by the blend method. It is called blend as opposed to ‘superimpose’ say, because one of the method’s parameters chooses the opacity of the overlaid image. By choosing a low degree of opacity, the image at the bottom can be seen through the image on the top – achieving a blending of the two images.
The following few lines of code illustrate the concept.
var
pic1 := media → choose picture
var
w := pic1 → width
var
h := pic1 → height
var
pic2 := media → choose picture
pic2 → resize( w*0.5, h*0.5 )
pic1 → blend( pic2, w*0.3, h*0.2, 30, 0.7 )
pic1 → post to wall
The relationship of the two pictures to each other is illustrated in Figure 6-3.
The top left corner of pic2 is located at the x,y coordinates given by the second and third arguments to blend. This picture is rotated clockwise by the number of degrees given by the fourth argument. The opacity of the picture has been set at 0.7, which means that in the overlaid region, each pixel is a blend of 70% from pic2 and 30% from pic1. Finally, the bottom right of pic2 has been clipped to fit within pic1.