A Novel Approach to Spectral MRI
Tiffany A. Fetzner

Appendix D: IDL  code for  writing raw binaray images

PRO WRITE_SKW, file_name, skw_image, TYPE=t, CENTER=c

;+
; write a SKW image file from an array of floats in the range 0-255
;
; USAGE:  WRITE_SKW, {filename}, {image_data}, [/TYPE={t},] [/CENTER[={c}]]
;
; The TYPE keyword:
;  /TYPE=0     image will be cropped to the largest power-of-two square,
;  either the same size as or smaller than the image.
;
; /TYPE=1     image will be expanded to the smallest power of two square,
;  containing the entire image.
;
; /TYPE=2     image will be scaled to a power of two square, depending on the
;  value of the CENTER Keyword.
;
; The CENTER keyword:
; /CENTER=0   for /TYPE=0 and /TYPE=1, this will flush the image to the lower-left
;  prior to performing a crop or expand operation.  For /TYPE=2 the image
;  will be scaled to next smaller power-of-two square.
;
; /CENTER=1   for /TYPE=0 and /TYPE=1, this will center the image prior to
;  performing a crop or scale operation.  For /TYPE=2 the image will be
;  scaled to the next larger power-of-two square.
;-

;Additions to conform to IDL 5.0's parameter passing techniques:
if (NOT keyword_set(t)) then begin
 t=0  ; type defaults to zero
ENDIF
if (NOT keyword_set(c)) then begin
 c=0  ; center defaults to zero
ENDIF

; Assuming that the skw_image array *IS* a 2-dimentional array... making
; assumptions is a *BAD THING*....  --=={* FIX ME *}==--

; Determine largest (MAXD) and smallest (MIND) dimension
IF ( (size(skw_image))(1) LT (size(skw_image))(2) ) THEN Begin
 MAXD=(size(skw_image))(2)
 MIND=(size(skw_image))(1)
ENDIF ELSE Begin
 MAXD=(size(skw_image))(1)
 MIND=(size(skw_image))(2)
ENDELSE

;Determination of largest and smallest file size.
MIND=((2)^(FIX(ALOG(MIND) / ALOG(2) )))
MAXD=((2)^(100-FIX(100-(ALOG(MAXD) / ALOG(2)) ))) ;Assumption made that the image is
        ;Less than 2^100th wide/high. (2^100th
        ;is appoximately 10^30th.)

;display image size
PRINT, "Image size :  MinD=", MIND, " MaxD=", MAXD

IF ( t EQ 0) THEN BEGIN
 out_image=FINDGEN(MIND, MIND) *0.0

 IF ( c EQ 1) THEN BEGIN
  ; crop into center of next power-of-2 square down
  Offset_x=(MIND-((SIZE(skw_image))(1)))/2
  Offset_y=(MIND-((SIZE(skw_image))(2)))/2
 

  FOR x=0,MIND-1,1 DO BEGIN
   FOR y=0,MIND-1,1 DO BEGIN
    out_image(x,y)=skw_image(x-Offset_x,y-Offset_y)
   ENDFOR
  ENDFOR
 

 ENDIF ELSE BEGIN

  ; crop flushed to origin of next power-of-2 square down

  FOR x=0,MIND-1,1 DO BEGIN
 

   FOR y=0,MIND-1,1 DO BEGIN
    out_image(x,y)=skw_image(x,y)
   ENDFOR
 

  ENDFOR

 ENDELSE
 
 

ENDIF ELSE BEGIN
 IF ( t EQ 1) THEN BEGIN
  out_image=FINDGEN(MAXD, MAXD) *0.0
 

  IF ( c EQ 1) THEN BEGIN
   ; expand into center of next power-of-2 square up

   Offset_x=(((SIZE(skw_image))(1))-MAXD)/2
   Offset_y=(((SIZE(skw_image))(2))-MAXD)/2
 

   FOR x=0,((SIZE(skw_image))(1))-1,1 DO BEGIN
 

    FOR y=0,((SIZE(skw_image))(2))-1,1 DO BEGIN
     out_image(x-Offset_x,y-Offset_y)=skw_image(x,y)
    ENDFOR
 

   ENDFOR

  ENDIF ELSE BEGIN

   ; expand flushed to origin of next power-of-2 square up
   FOR x=0,((SIZE(skw_image))(1))-1,1 DO BEGIN
 

    FOR y=0,((SIZE(skw_image))(2))-1,1 DO BEGIN
     out_image(x,y)=skw_image(x,y)
    ENDFOR
 

   ENDFOR

  ENDELSE

 ENDIF ELSE BEGIN

  ; if t is not 0 or 1 pretend that it's 2

  IF ( c EQ 1) THEN BEGIN
   ; scale image up to next power-of-2 square
   out_image=REBIN(skw_image, MAXD, MAXD)
  ENDIF ELSE BEGIN

   ; scale image down to next power-of-2 square
   out_image=REBIN(skw_image, MIND, MIND)
  ENDELSE

 ENDELSE

ENDELSE
 

OPENW, rho, file_name, /get_lun  ; open file as 'rho'

;IF Mac and Unix architecture:
;
; temp = swap_endian(fix(out_image*16)) ; skw_image variable name will
;      ; change when above code is complete
;     ; swap endian of 'temp' into correct byte order:
;     ; LSB first in file
;ELSE (for intel architecture)
 temp = fix(out_image*16) ; don't swap endian, but convert to 16-bit
     ; integers in proper scale
;END architecture specific code.
 writeu, rho, temp  ; write 'temp' into 'rho'
 close, rho   ; close 'rho'
 free_lun, rho   ; deallocate 'rho
END

Table of Contents | Appendix  | Thesis