Example: Centering an Image

The following example steps through the process of creating an image object and provides two options for centering it within a window.

The first method establishes a viewplane rectangle within a view object. The image object is added to a model object. The model object is then translated to the center of the window object.

The second method does not establish a viewplane rectangle. Instead coordinate conversions are calculated and applied to the image object to center it within the model. This method works within the normalized coordinate system of the model.

You can also position an image in a view using the LOCATION property of the image object. For additional information and examples, see Positioning Image Objects in a View.

This example uses the image from the worldelv.dat file found in the examples/data directory.

PRO CenteringAnImage 
 
; Determine path to file. 
worldelvFile = FILEPATH('worldelv.dat', $ 
   SUBDIRECTORY = ['examples', 'data']) 
 
; Initialize image parameters. 
worldelvSize = [360, 360] 
worldelvImage = BYTARR(worldelvSize[0], worldelvSize[1], /NOZERO) 
 
; Open file, read in image, and close file. 
OPENR, unit, worldelvFile, /GET_LUN 
READU, unit, worldelvImage 
FREE_LUN, unit 
 
; Initialize window parameters. 
windowSize = [400, 460] 
windowMargin = (windowSize - worldelvSize)/2 
 
; First Method:  Defining the Viewplane and 
;                Translating the Model. 
;------------------------------------------- 
 
; Initialize objects required for an Object Graphics 
; display. 
oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $ 
   DIMENSIONS = windowSize, $ 
   TITLE = 'World Elevation:  First Method') 
oView = OBJ_NEW('IDLgrView', $ 
   VIEWPLANE_RECT = [0., 0., windowSize]) 
oModel = OBJ_NEW('IDLgrModel') 
 
; Initialize palette with STD GAMMA-II color table and 
; use it to initialize the image object. 
oPalette = OBJ_NEW('IDLgrPalette') 
oPalette->LOADCT, 5 
oImage = OBJ_NEW('IDLgrImage', worldelvImage, PALETTE = oPalette) 
 
; Add image to model, which is added to view.  Model 
; is translated to center the image within the window. 
; Then view is displayed in window. 
oModel->Add, oImage 
oView->Add, oModel 
oModel->Translate, windowMargin[0], windowMargin[1], 0. 
oWindow->Draw, oView 
 
; Clean-up object references. 
OBJ_DESTROY, [oView, oPalette] 
 
; Second Method:  Using Coordinate Conversions. 
;----------------------------------------------- 
 
; Initialize objects required for an Object Graphics 
; display. 
oWindow = OBJ_NEW('IDLgrWindow', RETAIN = 2, $ 
   DIMENSIONS = windowSize, $ 
   TITLE = 'World Elevation:  Second Method') 
oView = OBJ_NEW('IDLgrView') 
oModel = OBJ_NEW('IDLgrModel') 
 
; Initialize palette with STD GAMMA-II color table and 
; use it to initialize the image object. 
oPalette = OBJ_NEW('IDLgrPalette') 
oPalette->LOADCT, 5 
oImage = OBJ_NEW('IDLgrImage', worldelvImage, $ 
   PALETTE = oPalette) 
 
; Obtain initial coordinate conversions of image object. 
oImage->GetProperty, XCOORD_CONV = xConv, $ 
   YCOORD_CONV = yConv, XRANGE = xRange, YRANGE = yRange 
 
; Output initial coordinate conversions. 
PRINT, 'Initial xConv:  ', xConv 
PRINT, 'Initial yConv:  ', yConv 
 
; Applying margins to coordinate conversions. 
xTranslation = (2.*FLOAT(windowMargin[0])/windowSize[0]) - 1. 
xScale = (-2.*xTranslation)/worldelvSize[0] 
xConv = [xTranslation, xScale] 
yTranslation = (2.*FLOAT(windowMargin[1])/windowSize[1]) - 1. 
yScale = (-2.*yTranslation)/worldelvSize[1] 
yConv = [yTranslation, yScale] 
 
; Output resulting coordinate conversions. 
PRINT, 'Resulting xConv:  ', xConv 
PRINT, 'Resulting yConv:  ', yConv 
 
; Apply resulting conversions to the image object. 
oImage->SetProperty, XCOORD_CONV = xConv, $ 
   YCOORD_CONV = yConv 
 
; Add image to model, which is added to view. Display 
; the view in the window. 
oModel->Add, oImage 
oView->Add, oModel 
oWindow->Draw, oView 
 
; Cleanup object references. 
OBJ_DESTROY, [oView, oPalette] 
 
END