## 8.4 Photos

### 8.4.1 Description

A photo is an image whose pixels can display any color or be transparent. A photo image is stored internally in full color (24 bits per pixel), and is displayed using dithering if necessary. Image data for a photo image can be obtained from a file, a string or a url. At present, only GIF and PPM/PGM formats are supported. A photo image is transparent in regions where no image data has been supplied.

In QTk, photos are built by the `QTk.newImage` function. This function takes one parameter: a record of the form `photo(param1:value1 ... paramX:valueX)` and returns an object allowing to reference that photo and change it as needed. The valid parameters and methods are described below.

### 8.4.2 Parameters

- `channel:C`: `C` gives the name of a channel open for reading which is to be read to supply data for the photo image. The data format in the channel must be one of those for which there is an image format handler that can read data from a file or channel.

- `data:VS`: Specifies the contents of the image as a string. The format of the string must be one of those for which there is an image file format handler that will accept string data.

- `url:VS`: `VS` gives the url of a file that is to be read to supply data for the photo image. The file format must be one of those for which there is an image file format handler that can read data from a file or channel.

- `format:VS`: Specifies the name of the file format for the data specified with the `data`, `url` or `file` option.

- `file:VS`: `VS` gives the name of a file that is to be read to supply data for the photo image. The file format must be one of those for which there is an image file format handler that can read data from a file or channel.

- `gamme:F`: Specifies that the colors allocated for displaying this image in a window should be corrected for a non-linear display with the specified gamma exponent value. (The intensity produced by most CRT displays is a power function of the input value, to a good approximation; gamma is the exponent and is typically around 2). The value specified must be greater than zero. The default value is one (no correction). In general, values greater than one will make the image lighter, and values less than one will make it darker. `F` must be a float.

- `height:I`: Specifies the height of the image, in pixels. This option is useful primarily in situations where the user wishes to build up the contents of the image piece by piece. A value of zero (the default) allows the image to expand or shrink vertically to fit the data stored in it. `I` must be non-negative integer.

- `palette:VS`: Specifies the resolution of the color cube to be allocated for displaying this image, and thus the number of colors used from the colormaps of the windows where it is displayed. The `VS` string may be either a string containing a single decimal number, specifying the number of shades of gray to use, or a string containing three decimal numbers separated by slashes (/), specifying the number of shades of red, green and blue to use, respectively. If the first form (a single number) is used, the image will be displayed in monochrome (i.e., grayscale).

- `width:I`: Specifies the width of the image, in pixels. This option is useful primarily in situations where the user wishes to build up the contents of the image piece by piece. A value of zero (the default) allows the image to expand or shrink horizontally to fit the data stored in it. `I` must be non-negative integer.

### 8.4.3 Interface

- `set(parameter1:value1 ... parameterX:valueX)`: Changes the value of one or more parameters. `parameterX` must be a valid parameter for the widget. `valueX` must be a valid value for the parameter.

- `get(parameter1:Variable1 ... parameterX:VariableX)`: Obtains the value of one or more parameters. `parameterX` must be a valid parameter for the widget. `VariableX` must be free variables that will receive the current value of the parameters.

- `blank()`: Blank the image; that is, set the entire image to have no data, so it will be displayed as transparent, and the background of whatever window it is displayed in will show through.

- `copy(I param1:value1 ... paramX:valueX)`: Copies a region from the image `I` (which must be a photo image) to this image, possibly with pixel zooming and/or subsampling. If no options are specified, this command copies the whole of `I` into this image, starting at coordinates (0,0) in this image. The following options may be specified:

  - `from:R`: Specifies a rectangular sub-region of the source image to be copied. `R` must be a record of the form: `o(X1 Y1 X2 Y2)`, where (X1,Y1) and (X2,Y2) specify diagonally opposite corners of the rectangle. If `X2` and `Y2` are not specified, the default value is the bottom-right corner of the source image. The pixels copied will include the left and top edges of the specified rectangle but not the bottom or right edges. If the `from` option is not given, the default is the whole source image.

  - `to:R)`: Specifies a rectangular sub-region of the destination image to be affected. `R` must be a record of the form: `o(X1 Y1 X2 Y2)`, where (X1,Y1) and (X2,Y2) specify diagonally opposite corners of the rectangle. If `X2` and `Y2` are not specified, the default value is (X1,Y1) plus the size of the source region (after subsampling and zooming, if specified). If `X2` and `Y2` are specified, the source region will be replicated if necessary to fill the destination region in a tiled fashion.

  - `"-shrink"`: Specifies that the size of the destination image should be reduced, if necessary, so that the region being copied into is at the bottom-right corner of the image. This option will not affect the width or height of the image if the user has specified a non-zero value for the `width` or `height` configuration option, respectively.

  - `zoom:R`: Specifies that the source region should be magnified by a factor. `R` must be a record of the form: `o(X Y)` where `X` is the factor in the X direction and `Y` the one in the Y direction. If `Y` is not given, the default value is the same as `X`. With this option, each pixel in the source image will be expanded into a block of `X` x `Y` pixels in the destination image, all the same color. `X` and `Y` must be greater than 0.

  - `subsample:R`: Specifies that the source image should be reduced in size. `R` must be a record of the form `o(X Y)` where only `X`th pixel are kept in the X direction and only `Y`th pixel in the Y direction. Negative values will cause the image to be flipped about the Y or X axes, respectively. If `Y` is not given, the default value is the same as `X`.

- `getColor(X Y V)`: Binds `V` to the color of the pixel at coordinates (X,Y) in the image as a list of three integers between 0 and 255, representing the red, green and blue components respectively.

- `put(LR to:R)`: Sets pixels in the photo to the colors specified in `LR`. `LR` is used to form a two-dimensional array of pixels that are copied into the photo. `LR` is a record of the form:

  > `q(q(P11 P21 ... Px1)
  > q(P12 P22 ... Px2)
  > ...
  > q(P1y P2y ... Pxy))`

  Where all `Pij` are valid colors (see Section 8.2). The `to` option can be used to specify the area of the photo to be affected. `R` must be a record of the corm `o(X1 Y1 X2 Y2)`. If only `X1` and `Y1` are given, the area affected has its top-left corner at (X1,Y1) and is the same size as the array given in data. If all four coordinates are given, they specify diagonally opposite corners of the affected rectangle, and the array given in data will be replicated as necessary in the X and Y directions to fill the rectangle.

- `read(VS param1:value1 ... paramX:valueX)`: Reads image data from the file named `VS` into the image. This command first searches the list of image file format handlers for a handler that can interpret the data in `VS`, and then reads the image in `VS` into the photo. The following options may be specified:

  - `format:VS`: Specifies the format of the image data in filename. Specifically, only image file format handlers whose names begin with format-name will be used while searching for an image data format handler to read the data.

  - `from:R`: Specifies a rectangular sub-region of the source image to be copied. `R` must be a record of the form: `o(X1 Y1 X2 Y2)`, where (X1,Y1) and (X2,Y2) specify diagonally opposite corners of the rectangle. If `X2` and `Y2` are not specified, the default value is the bottom-right corner of the source image. The pixels copied will include the left and top edges of the specified rectangle but not the bottom or right edges. If the `from` option is not given, the default is the whole source image.

  - `"-shrink"`: Specifies that the size of the destination image should be reduced, if necessary, so that the region being copied into is at the bottom-right corner of the image. This option will not affect the width or height of the image if the user has specified a non-zero value for the `width` or `height` configuration option, respectively. -to x y Specifies the coordinates of the top-left corner of the region of imageName into which data from filename are to be read. The default is (0,0).

  - `to:R)`: Specifies a rectangular sub-region of the destination image to be affected. `R` must be a record of the form: `o(X1 Y1 X2 Y2)`, where (X1,Y1) and (X2,Y2) specify diagonally opposite corners of the rectangle. If `X2` and `Y2` are not specified, the default value is (X1,Y1) plus the size of the source region (after subsampling and zooming, if specified). If `X2` and `Y2` are specified, the source region will be replicated if necessary to fill the destination region in a tiled fashion.

- `redither()`: The dithering algorithm used in displaying photo images propagates quantization errors from one pixel to its neighbors. If the image data is supplied in pieces, the dithered image may not be exactly correct. Normally the difference is not noticeable, but if it is a problem, this command can be used to recalculate the dithered image in each window where the image is displayed.

- `write(VS param1:value1 ... paramX:valueX)`: Writes image data to a file named `VS`. The following options may be specified:

  - `format:VS`: Specifies the name of the image file format handler to be used to write the data to the file. Specifically, this subcommand searches for the first handler whose name matches a initial substring of format-name and which has the capability to write an image file. If this option is not given, the first handler that has the capability to write an image file is used.

  - `from:R`: Specifies a rectangular region of the image to be written to the image file. `R` must be a record of the form `o(X1 Y1 X2 Y2)`. If only `X1` and `Y1` are specified, the region extends from (X1,Y1) to the bottom-right corner of the photo. If all four coordinates are given, they specify diagonally opposite corners of the rectangular region. The default, if this option is not given, is the whole image.