subr mvsave_2d_cb, cube
 ;the series of files option, need the range
 ;10/3/2000 - modify to use $play_index
 it1 = fix(xmtextfieldgetstring($mvsave_filenamewidgets(5)))
 it2 = fix(xmtextfieldgetstring($mvsave_filenamewidgets(6)))
 first_offset = fix(xmtextfieldgetstring($mvsave_filenamewidgets(7)))
 name = xmtextfieldgetstring ($mvsave_filenamewidgets(4))
 ;if the name has a number template then use, otherwise append a #
 if strpos(name,'#') lt 0 then name=name+'#'
 ;nt = get_nt(cube)
 nt = num_elem($play_index)
 it2 = it2<(nt-1)
 if (it1 gt it2) then {
 	errormess, 'image number ranges\nare bad'
	return }

 ;presently we only expect movie cubes to be specially scaled byte arrays,
 ;matching the color cells available. For the series of files we make a
 ;more uniform product by always re-scaling to the 0-255 range
 ;if 12 bit data is used in the future, this could be changed for the
 ;fz and fits outputs
 
 ;need cutout?
 cutout_flag = 0
 yq = 0
 zeroifnotdefined, $mv_track_flag
 if xmtogglegetstate($mvsave_mode(1)) eq 1 then {
   cutout_flag = 1
   ixbase = $view_ix(#play_window)
   ix = ixbase
   iybase = $view_iy(#play_window)
   iy = iybase
   nxbase = $view_width(#play_window)
   nybase = $view_height(#play_window)
   yq = bytarr(nxbase, nybase)
 }

 ;lookup table to re-scale the array to the range 0-255 assuming it
 ;was already in the !scalemin, !scalemax range
 mv256_lookup = zero(bytarr(256))
 inc = 255./float(!scalemax - !scalemin)
 ;;ty,'inc, !scalemin, !scalemax =', inc, !scalemin, !scalemax
 for i = !scalemin, !scalemax do mv256_lookup(i) = rfix((i-!scalemin) * inc)
 for i = !scalemax, 255 do mv256_lookup(i) = 255
 ;;ty, mv256_lookup

 ;read in the jpeg Q if a jpeg file
 if $mv2d_file_type eq 4 then $jpeg_quality = fix(xmtextfieldgetstring ($jpegvalue))
 
 ;need color info for GIF files, currently color is only done for GIF's, all
 ;others are just data with no color table info
 ;also we always scale for gif output, even if data is already byte
 if $mv2d_file_type eq 6 then {
	$gif_color = [ [#red_map],[#green_map],[#blue_map] ]
	$gif_color = byte(255.*$gif_color(>1,>0))
	}
 
 for it = it1, it2 do {
 j = it + first_offset - it1
 ty,'it, j =', it, j
 ;get the xy slice
 xq = getxy( cube, it)
 
 if cutout_flag then {
  ;cutout situation
  ;don't have to worry about orientation but the movie view can be
  ;larger than the cube size in x and y, this requires 2 approaches
  ix = ixbase + $mv_track_dx(it)
  iy = iybase + $mv_track_dy(it)
  ;don't do both the track and the drift; i.e., the drift can be used only
  ;when solar tracking is off
  if $movie_drift_flag eq 1 and $mv_track_flag eq 0 then {
	ix = ix + rfix(it * $movie_drift_x)
	}

  ;if either ix or iy are lt 0, an insert is necessary
  if ix lt 0 then { ixin = -ix	i1 = 0 } else { ixin = 0  i1 = ix }
  if iy lt 0 then { iyin = -iy	j1 = 0 } else { iyin = 0  j1 = iy }
  i2 = min( [dimen(xq,0), nxbase + ix]) - 1
  j2 = min( [dimen(xq,1), nybase + iy]) - 1
  ty,'i1,i2,j1,j2 =',i1,i2,j1,j2
  zero, yq
  insert, yq, mv256_lookup(xq(i1:i2, j1:j2)), ixin, iyin
 } else {
 yq = mv256_lookup(xq)
 }
 d, yq
 iq = datawrite($mv2d_file_type, yq, fns(name,j))
 xtloop, 2
 if iq ne 1 then { write_error, fns(name,j)		return }
 }
 endsubr
 ;========================================================
