;iff picture scaling command
;by Curt Esser   camge@ix.netcom.com
;last modified MAY 10 1998
;use any part of this in any way you like
;but please, if you make any improvements or find any errors
;let me know.  Thanx!



;----------- CHECK FOR PROPER COMMANDS GIVEN -----------------------------
; - commands are:
; IFFscale Picturename, scale x size(%), (scale y size(%)/PROP,
; [(trimfrom x,trimfrom y)trimsize x,trimsize y]

error$=""
If NumPars = >2                 ;otherwise, not enough parameters!

  trimflag.b=False              ;do they want it trimmed too?

  picpath$=Par$(1)              ;check the picture file
  If Exists(picpath$)
    If ReadFile(0,picpath$)     ;make sure it is a valid iff picture file!
      FileInput 0
      header$ = Inkey$(20)      ;Read 20 bytes of the header
      CloseFile 0

      ;IFF picture header should read: FORM....ILBM

      If Left$(header$,4)= "FORM" AND Mid$(header$,9,4) = "ILBM"
        ILBMInfo picpath$
        picwidth.w =ILBMWidth
        picheight.w=ILBMHeight
        picdepth.b =ILBMDepth
      Else
        error$="Not an iff picture"
      EndIf
    EndIf
  Else
    error$="File not found"
  EndIf
  If error$=""                    ;OK so far!
    t$=Par$(2)                    ;read second parameter - X size
    If Val(t$)>0                  ;check for a valid size entry
      If Right$(t$,1)="%"         ;specified as a %
        Xscl.q=Val(t$)/100        ;convert to decimal form
        destX.w=picwidth*Xscl     ;set target x size
      Else
        destX.w=Val(t$)           ;check new x size
        Xscl.q=destX/picwidth     ;percent of x scaling
      EndIf
      Xoffset.q=picwidth/destX    ;for finding the pixel to copy
    Else
      error$="Bad width "
    EndIf
    t$=Par$(3)                    ;now check 3rd parameter - Y size
    If t$="PROP"                  ;proportionate scaling requested
      destY.w=picheight*Xscl
    Else
      If Val(t$)>0                ;check for a valid entry
        If Right$(t$,1)="%"       ;specified as a %
          Yscl.q=Val(t$)/100      ;so calculate target y size
          destY.w=picheight*Yscl
        Else
          destY=Val(t$)           ;specified as a size, so just read it
        EndIf

      Else
        error$=error$+"Bad height"
      EndIf
    EndIf
    Yoffset.q=picheight/destY
  EndIf
  If NumPars=>5                   ;trimming requested too!
    If NumPars>5                  ;first two are start x & y if present
      trimfromx.w=Val(Par$(4))
      trimfromy.w=Val(Par$(5))
      finalsizex.w=Val(Par$(6))
      If NumPars>6
        finalsizey.w=Val(Par$(7))
      Else
        finalsizey=finalsizex
      EndIf
    Else
      finalsizex=Val(Par$(4))
      finalsizey=Val(Par$(5))
    EndIf
    trimflag.b=True
    If trimfromx<0 OR trimfromx=>finalsizex Then trimfromx=0
    If trimfromy<0 OR trimfromy=>finalysize Then trimfromy=0
  EndIf
  NPrint "Original width  = ",picwidth
  NPrint "Original height = ",picheight
  NPrint "New width  =      ",destX
  NPrint "New height =      ",destY
  If trimflag
    NPrint "Final trim size = "+Str$(finalsizex)+" x "+Str$(finalsizey)
  EndIf
                                                                                                                              CD
Else
  error$="Wrong number of parameters"  ;We're cooked already!
EndIf

;
; ------ check for enough chip & fast  memory for the conversion -----------

If error$=""

  memchip.l=AvailMem_(131074)  ;read available chip memory
  ;memfast.l=AvailMem_(131076) ;read available fast memory

  origmem.l=picwidth*picheight*picdepth/8
  destmem.l=destX*destY;*picdepth/8

  chipmem.l=origmem+destmem+1000

  If chipmem+10000>memchip
    error$="Not enough chip memory available"
  EndIf
EndIf

;---------------- IF WE CAN'T DO THE CONVERSION -----------------------------

If error$<>""
  NPrint "IFFscale failed!"
  NPrint " "
  NPrint error$
  NPrint " "
  NPrint "IFFscale - Syntax:"
  NPrint "IFFscale <Picturename> <NewWidth(%)> <NewHeight(%)|PROP> "
  NPrint "   [TrimFromX TrimFromY <TrimToXsize TrimToYsize>]"
  End
EndIf


; ----------- READY! -----------------------------------------------------

BitMap 0,picwidth,picheight,picdepth    ;bitmap for the iff pic
LoadBitMap 0,picpath$,0                 ;load the picture
BitMap 1,destX,destY,picdepth           ;open destination bitmap


NPrint picpath$," loaded"

;---------------- Rescale the picture -------------------------------------

For y=0 To destY-1
  For x=0 To destX-1
    Use BitMap 0
    pixel.w= Point (x*Xoffset,y*Yoffset)
    Use BitMap 1
    Plot x,y,pixel
  Next x
Next y

NPrint "Scaled"

If trimflag=False
  SaveBitmap 1,picpath$+".scal",0         ;now save it
Else
  scrolltox.w=finalsizex+trimfromx        ;or trim first if necessary
  If scrolltox>destX Then scrolltox=destX
  scrolltoy.w=finalsizey+trimfromy
  If scrolltoy>destY Then scrolltoy=destY
  Free BitMap 0                           ;re-use original bitmap
  BitMap 0,finalsizex,finalsizey,picdepth
  Scroll trimfromx,trimfromy,scrolltox,scrolltoy,0,0,1
  SaveBitmap 0,picpath$+".scal",0         ;now save trimmed pic
EndIf
NPrint "IFFscale done!"
End




