X
3642

Programming with Objects: Basic Examples Using I/O and Images

This help article demonstrates how to define an object that associates a file to a variable. It then contains an example of how to use that object to read in an image file and display the image.

For general information on object programming and the structure of IDL objects, refer to Tech Tip Introduction to Objects.

The following code creates an object structure that can then be used to read in a file and display sections of it to the screen.

First the object is "defined". Notably, this code comes last, but it makes logical sense to define it first. Then the object's Init and Destroy methods are created. Finally, its additional methods, a get pointer (GetPtr), a set pointer (SetPtr), and a file association (MyAssoc) method are defined. Although, there is technically no necessary order for the definition of the methods, it is recommended to create the Init and Destroy methods first, as they are very important for the initialization and the proper clean up of the object instance. It is important that if you are going to save the code with the same name as the "definition" code, then the definition code must come last in the program. If the methods are not defined before the "definition" of the object, then the methods will not be found at the time of compilation. The following example has been written in this manner.

That is all that is necessary to create the object.

The code in this section shows the object creation code. The EXAMPLE CODE section contains an example of how to make use of this object code within another routine.
   
   ; BEGIN SAMPLE CODE
   ;
   ;FILE:
   ; mystruct__define.pro
   ;
   ;
   ; CALLING SEQUENCE:
   ; To initially create:
   ; oStruct = OBJ_NEW('MyStruct')
   ;
   ; To retrieve pointer:
   ; MyPtr=oStruct->GetPtr()
   ;
   ; To set a pointer:
   ; oStruct->SetPtr, ptr
   ;
   ; To associate variable with file for I/O
   ; oStruct->MyAssoc, lun, whatsize
   ;
   ; To destroy:
   ; OBJ_DESTROY, oMyStruct
   ;
   ;
   function MyStruct::init, FirstVar=firstvar, $
    SecondVar=secondvar, str=str
   self.firstvar=0B
   self.secondvar=FINDGEN(10)
   self.str='Strings'
   return,1
   end

   ; cleanup
   pro MyStruct::cleanup
   if (obj_valid(self.asscPTR)) then $
    obj_destroy,self.asscPTR
   end
   ;
   ;Methods
   ;Associate Variable with File for I/O and create pointer to that var.
   pro MyStruct::MyAssoc, lun, whatSize
   self.asscPTR = PTR_NEW(ASSOC(lun, whatSize))
   end
   
   ;Get Property
   function MyStruct::GetPtr
   return, self.asscPTR
   end

   ;Set Property
   pro MyStruct::SetPtr, ptr
   self.asscPTR = ptr
   end

   ;
   ;Definition
   pro MyStruct__define

   myStruct = { MyStruct, $
    firstVar: 0B, $ ; Single byte value
    secondVar: FINDGEN(10), $ ; Array of 10 floats
    asscPTR: PTR_NEW(), $ ; Pointer to ass'd variable
    str: 'Strings' $ ;String
    }
   end

   ; END SAMPLE CODE

Below, an instance to the object is created (MyObj), which calls the init method implicitly. The various methods of the object are then applied. First a data file is opened and then, through the MyAssoc method, the data in the file is mapped into the Object and pointed to with MyObj through the GetPtr method. A section of the data is extracted and displayed, then the file is closed. The pointers are destroyed, and the object referenced is destroyed with OBJ_DESTROY which implicitly calls the object's Destroy method.

pro test_obj
device,decompose=0
;
;create object instance
MyObj = OBJ_NEW('MyStruct')
file = FILE_WHICH('people.dat')
OPENR, lun, file, /GET_LUN
whatSize = BYTARR(192,192)
;
; Points to associated variable.
;
MyObj->MyAssoc, lun, whatSize
iPtr = MyObj->GetPtr()
;
; Points to individual images.
;
WINDOW, 0 , XSIZE = 192*2, YSIZE = 192
for ii = 0, 1 do begin
   loadct,ii*2+1
   TVSCL, (*iPtr)[ii], ii
   WAIT, .5
   WSHOW, 0
endfor
; Close the file
CLOSE, lun
; Free the file pointer
FREE_LUN, lun
; Destroy all objects
OBJ_DESTROY, MyObj

end