function change_res, img_in, index_in, index_out, res_out=res_out, qstop=qstop, decompress=decompress, nooffset=nooffset
;
;+
;NAME:
;	change_res
;PURPOSE:
;	To change the resolution of a FFI image.
;CALLING SEQUENCE:
;	img_out = change_res(data, index, index_out)	;change to 2x2
;	img_out = change_res(data(*,*,i), res_out=2)	;change to 4x4
;	img_out = change_res(data, index, /decomp)
;INPUT:
;	img_in	- Input image (should be 2-D).  Needs to be full
;		  256x256, 512x512 (full res not handled right now)
;	index_in- The index associated with that image
;OPTIONAL INPUT:
;	res_out	- Resolution of the output image.
;		  0 = 1x1, 1=2x2, and 2=4x4
;		  If not present, the output resolution is 2x2 (half)
;	decompress- If present, the data is left decompressed if it came
;		  in as byte type.
;OUTPUT:
;	data_out- The rebinned data cube
;	index_out- The index that goes with the rebinned data cube (reflects
;		  the changed resolution).  It is only defined if INDEX_IN
;		  is passed in.
;METHOD:
;	NOTE: If the data is byte type, it is decompressed for the work,
;	and then re-compressed.   It can be left decompressed by using the
;	decompress option.  It assumes BLS is off.
;	If it is on, the data will not be registered properly
;
;	TODO: Should pass back a modified index with a modification history
;	TODO: The index resolution information is not updated to reflect the
;	change
;HISTORY:
;	Written 12-Nov-91 by M.Morrison
;	 4-Jun-93 (MDM) - Added NOOFFSET keyword option
;	20-Jun-93 (MDM) - Modified to use MAKE_ARRAY instead of execute statement
;	23-Jun-93 (MDM) - Added history records
;	 4-Aug-93 (MDM) - Added INDEX_OUT parameter
;-
;
siz = size(img_in)
n = 1
if (siz(0) eq 3) then n = siz(3)
typ = siz( siz(0) + 1)
;
if (n_elements(res_out) eq 0) then res_out = 1
if (n_elements(index_in) eq 0) then begin
    qhis = 0
    nx = intarr(n) + siz(1)	;assumes that the data cube does not have padded images in it (ie: 2x2 and 4x4 mixed)
    ny = intarr(n) + siz(2)
    res_in = (2-nx/512)
end else begin
    qhis = 1
    index_out = index_in
    res_in = gt_res(index_in)
    shape_cmd = gt_shape_cmd(index_in)
    nx = shape_cmd(0,*)
    ny = shape_cmd(1,*)
end
;
typ_out = typ
if (keyword_set(decompress)) then typ_out = 2
;
nout = 1024 / (2^res_out)
img_out = make_array(nout, nout, n, type=typ_out)
;
img_out = img_out + byte(get_res_offset(res_out))     ;establish the offset value for the areas that do not get inserted (the
                                               	      ;non-binned pixels)
;^^ Changes the data type of img_out , so make offset a byte - it is just to avoid a bright column
;
for i=0,n-1 do begin
    if (res_in(i) eq res_out) then begin
	temp = img_in(0:nx(i)-1,0:ny(i)-1,i)
	if ((typ eq 1) and (keyword_set(decompress))) then temp = sxt_decomp(temp)
	;TODO - should really check the compression bit in the index
	img_out(0,0,i) = temp
    end else begin
	if (typ eq 1) then temp = sxt_decomp(img_in(*,*,i)) else temp = img_in(*,*,i)
	;
	;---- See page 96 of the "Blue Book" to understand the peculiarities of the column summation
	ist_arr = [3, 1, 0]
	temp = temp(ist_arr(res_in(i)):nx(i)-2, 0:ny(i)-1)
			;strip off last few columns (image is reversed) to make sure that when re-binning
			;the image is registered.  Also skip the first column since it is not binned (BLS)
	siz2 = size(temp)
	nx2 = siz2(1)					;should be 1020, 510, or 255
	factor = (2^res_in(i))/(2.^res_out)		;example input=4x4, output=2x2 ==> factor=2
	temp = rebin(temp, nx2*factor, ny(i)*factor, /sample)
	;
	if (not keyword_set(nooffset)) then begin
	    temp = temp + (get_res_offset(res_out)-get_res_offset(res_in(i)))		;correct the offset
	end
	if (not keyword_set(decompress) and (typ eq 1)) then temp = sxt_comp(temp)
	img_out(ist_arr(res_out),0,i) = temp		;insert the re-binned array
	;
	;---- MDM added 23-Jun-93
	rel_arr = [ 0.0, 0.5, 1.5 ]
	shft = rel_arr(res_in(i)) - rel_arr(res_out)	;FR to QR => 0.0 - 1.5 = -1.5
							;QR to HR => 1.5 - 0.5 = +1.0
							;HR to FR => 0.5 - 0.0 = +0.5
	if (qhis) then begin
	    his_index, index_out, i, 'pixel_size', res_out
	    his_index, index_out, i, 'corner_sav', gt_corner(index_in(i)) - shft
	end
	;
	if (keyword_set(qstop)) then stop
    end
end
;
return, img_out
end
