ChangeFSI 294 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
REM Copyright 2012 Castle Technology Ltd
REM
REM Licensed under the Apache License, Version 2.0 (the "License");
REM you may not use this file except in compliance with the License.
REM You may obtain a copy of the License at
REM
REM     http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
REM
REM > ChangeFSI
REM
REM
REM This program written by Roger Wilson at Acorn Computers Apr 1989-Jun 1994
REM and by Sophie Wilson Aug 1994-
REM - please send bugs back!
REM Subject to change without notice
REM
REM All information is held in 32 bit fixed point with the . at bit 28
REM this gives an integer range of 0-15 (OR negative numbers!)
REM
REM ChangeFSI is written so as to allow use from CLI and from other BASIC
REM programs such as WIMP front ends, plus use of its own wimp front end.
REM
areanumber%=0
leaf$=""
SYS "OS_GetEnv" TO A$
REM Read CLI string
task%=INSTR(A$," -wimp")
SYS "OS_SWINumberFromString",,"OS_ReadMonotonicTime" TO Time%
SYS "OS_SWINumberFromString",,"ColourTrans_ReturnColourNumber" TO ctran%
IF task% THEN
37 38 39 40
  DIM z%(1),p%(3),s%(3)
  SYS"MessageTrans_FileInfo",,"<ChangeFSI$Dir>.Messages" TO,,S%
  DIM msgb% 16,msgf% S%
  SYS"MessageTrans_OpenFile",msgb%,"<ChangeFSI$Dir>.Messages",msgf%
41
  startfile%=RIGHT$(A$,6)<>"-wimp "
42
  DIM taskid% 4,taskmsg% 4
43
  $taskid%="TASK"
44 45
  !taskmsg%=0:REM All messages
  SYS "Wimp_Initialise",310,!taskid%,FNmsg0("_TaskName"),taskmsg% TO version%
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  IF version%>=350 THEN
    SYS "XOS_Memory",8+(2<<8) TO ,r1,r2;F%
    IF F% AND 1 vramsize%=0 ELSE vramsize%=r1*r2
    IF vramsize%>0 vram%= TRUE ELSE vram%= FALSE
  ELSE
    vram%= FALSE
  ENDIF
  Wimp    = (1<<18) + (3<<6)
  CreateW = Wimp+1
  OpenW   = Wimp+5
  CloseW  = Wimp+6
  RedrawW = Wimp+8
  UpdateW = Wimp+9
  GetR%   = Wimp+10
  GetW    = Wimp+11
  SetI    = Wimp+13
  GetI    = Wimp+14
  GetP    = Wimp+15
  Drag    = Wimp+16
  CrMenu  = Wimp+20
  DcMenu  = Wimp+21
  SYS "OS_SWINumberFromString",,"Wimp_PollIdle" TO Poll%
  SYS "OS_SWINumberFromString",,"Wimp_SetCaretPosition" TO Caretpos%
  SYS "OS_SWINumberFromString",,"Wimp_GetCaretPosition" TO Getcaret%
  SYS "XOS_SWINumberFromString",,"DragASprite_Start" TO dstart%;f%
  IF f% AND 1 dstart%=0
  SYS "XOS_SWINumberFromString",,"JPEG_PlotScaled" TO jpegpaint%;f%
  IF f% AND 1 jpegpaint%=0
  SYS "OS_Byte",161,&1C TO ,,f%
  IF (f% AND 2)=0 dstart%=0
  nextlocation%=HIMEM
  SYS "Wimp_SlotSize",-1,-1 TO originalslot%
  PROClocale_initialise
  IF version%>=300 THEN
    DIM q% 255,ifactors% 15,factors% 15,pixtrans% 255,paltemp% 1023,buffer% 255,idata% 1023
  ELSE
    DIM z% 1699,q% 255,ifactors% 15,factors% 15,pixtrans% 255,paltemp% 1023,buffer% 255,idata% 2399
    eidata%=idata%+2400
  ENDIF
  scrap%= FALSE
  sprite%= FALSE
  sar%= FALSE
  saved%= TRUE
  isopen%= FALSE
90
  lastm%= 0
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  z$= STRING$(12,CHR$13)
  SYS "OS_Byte",161,&8C TO ,,f%
  IF f% AND 1 THEN
    SYS "Wimp_OpenTemplate",,"<ChangeFSI$Dir>.Template3d"
  ELSE
    SYS "Wimp_OpenTemplate",,"<ChangeFSI$Dir>.Templates"
  ENDIF
  LOCAL ERROR
  ON ERROR SYS "Wimp_CloseTemplate":ERROR 0,REPORT$+" (code "+ STR$ERL+")"
  IF version%>=300 THEN
    DIM z% FNlargest_buffer
    indsize%=FNsize_indirect("Info")
    DIM info_wind% indsize%
    SYS "Wimp_LoadTemplate",,z%,info_wind%,info_wind%+indsize%,-1,"Info"+z$,0
  ELSE
    SYS "Wimp_LoadTemplate",,z%,idata%,eidata%,-1,"Info"+z$,0 TO ,,idata%
  ENDIF
  SYS CreateW,,z% TO infow%
109
  $!(z%+88+7*32+20)=FNmsg0("_Version")
110 111 112 113 114
  PROCdotemplate("Processing")
  SYS CreateW,,z% TO proc%
  p%(0)=!(z%+88+8*32+20)
  p%(1)=!(z%+88+9*32+20)
  p%(2)=!(z%+88+10*32+20)
115
  p%(3)=!(z%+88+11*32+20)
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  PROCdotemplate("Output")
  SYS CreateW,,z% TO output%
  admode%=!(z%+88+20*32+20)
  adspcl%=!(z%+88+19*32+20)
  oldmode%=!(z%+88+33*32+20)
  PROCdotemplate("Scaling")
  SYS CreateW,,z% TO scalew%
  adscale%=!(z%+88+1*32+20)
  s%(0)=!(z%+88+10*32+20)
  s%(1)=!(z%+88+11*32+20)
  s%(2)=!(z%+88+12*32+20)
  s%(3)=!(z%+88+13*32+20)
  PROCdotemplate("Save")
  SYS CreateW,,z% TO save%
  adsave%=!(z%+88+1*32+20)
  adsavesprite%=!(z%+88+0*32+20)
  PROCdotemplate("Sprite")
  SYS CreateW,,z% TO sinfo%
  adsname%=!(z%+88+5*32+20)
  adsbytes%=!(z%+88+8*32+20)
  adsx%=!(z%+88+7*32+20)
  adsy%=!(z%+88+9*32+20)
  adsm%=!(z%+88+6*32+20)
  PROCdotemplate("Source")
  SYS CreateW,,z% TO srcinfo%
  adsrc%=!(z%+88+0*32+20)
  PROCdotemplate("Range")
  SYS CreateW,,z% TO raninfo%
  adran%=!(z%+88+0*32+20)
  PROCdotemplate("Zoom")
  SYS CreateW,,z% TO zoom%
  z%(0)=!(z%+88+4*32+20)
  z%(1)=!(z%+88+5*32+20)
  PROCdotemplate("JPEGOutput")
  SYS CreateW,,z% TO jpego%
  adjpegq%=!(z%+88+5*32+20)
  PROCdotemplate("Pic")
  SYS CreateW,,z% TO pic%
  adtitle%=z%!72
  RESTORE ERROR
  SYS "Wimp_CloseTemplate"
  !q%=-1
  q%!4=0
  q%!8=0
  q%!12=68
  q%!16=68
  q%!20=%11000000000010
163
  $(q%+24)="!"+FNmsg0("_TaskName")
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  SYS "Wimp_CreateIcon",,q% TO iconbar%
  SYS "XOS_Find",&40,"Choices:ChangeFSI.Choices" TO A%;V%
  IF (A%=0) OR ((V% AND 1)=1) THEN
    fast%= FALSE
    dest%=1
    statescale%=%10000000100
    stateproc%=0
    stateoutput%=&4052008
    statejpeg%=&2
    $p%(0)=""
    $p%(1)="2"+decimal_point$+"2"
    $p%(2)="24"
    $p%(3)="4"
    $adspcl%=""
    $admode%="28"
    $s%(0)="1"
    $s%(1)="4"
    $s%(2)="1"
    $s%(3)="4"
    $adjpegq%="75"
    $oldmode%=""
  ELSE
    INPUT#A%,fast%,dest%,statescale%,stateproc%,stateoutput%,statejpeg%,$adjpegq%,$oldmode%
    INPUT#A%,$p%(0),$p%(1),$p%(2),$p%(3),$admode%,$adspcl%
    $p%(1)=FNlocale_convert($p%(1),".",decimal_point$)
    INPUT#A%,$s%(0),$s%(1),$s%(2),$s%(3)
    CLOSE#A%
  ENDIF
  q%!12=1<<21
  !q%=scalew%
  FOR I%=0 TO 9
    IF statescale%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  FOR I%=10 TO 13
    IF statescale%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%+6
    SYS SetI,,q%
  NEXT
  !q%=proc%
  FOR I%=0 TO 7
    IF stateproc%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  !q%=output%
  FOR I%=0 TO 26
    IF stateoutput%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  !q%=jpego%
  FOR I%=0 TO 3
    IF statejpeg%>>I% AND 1 q%!8=1<<21 ELSE q%!8=0
    q%!4=I%
    SYS SetI,,q%
  NEXT
  IF stateoutput%>>17 AND 1 q%!8=1<<22 ELSE q%!8=0
  !q%=output%
  q%!12=1<<22
  q%!4=7
  SYS SetI,,q%
  q%!4=8
  SYS SetI,,q%
  q%!4=13
  SYS SetI,,q%
  q%!4=14
  SYS SetI,,q%
  PROCshowscaletofit
  $z%(0)="1"
  $z%(1)="1"
  SYS "OS_UpdateMEMC" TO oldmemc
  jpegsave%= FALSE
  ON ERROR SYS "OS_UpdateMEMC",oldmemc,&700:PROCerrorbox:SYS CrMenu,,-1 : REM close menus!
  SYS Drag,,-1
  IF NOT sprite% SYS "Wimp_SlotSize",originalslot%,-1
  F$=""
  IF startfile% startfile%= FALSE :F$=MID$(A$,INSTR(A$,"-wimp")+6):PROCcallFSI
  REPEAT
    SYS Poll%,1,q% TO A%
    CASE A% OF
      WHEN 1
        IF sprite% PROCredraw_window(!q%)
      WHEN 2
        PROCopen_window(!q%,q%!4,q%!8,q%!12,q%!16,q%!20,q%!24,q%!28)
      WHEN 3
        SYS &400c3,,q%
        nextlocation%=HIMEM
        PROCremovearea
        SYS "Wimp_SlotSize",originalslot%,-1
        saved%= TRUE
        sprite%= FALSE
        isopen%= FALSE
      WHEN 6
        PROCcheckmouse(!q%,q%!4,q%!8,q%!12,q%!16,q%!20)
      WHEN 7
        SYS GetP,,q%
        dw%=q%!12
        di%=q%!16
        IF dstart% SYS "DragASprite_Stop"
        q%!12=0
        q%!16=1
        q%!20=dw%
        q%!24=di%
        q%!28=!q%
        q%!32=q%!4
        q%!36=!sar%+3
        IF jpegsave% THEN
          q%!40=&c85
        ELSE
          q%!40=&ff9
        ENDIF
        temp$=FNZS(adsave%)
        foo%=INSTR(temp$,".")
        WHILE foo%
          temp$=MID$(temp$,foo%+1)
          foo%=INSTR(temp$,".")
        ENDWHILE
        $(q%+44)=temp$+CHR$0
        !q%=44+(LEN temp$+1+3 AND NOT 3)
        SYS "Wimp_SendMessage",17,q%,dw%,di%
        myref%=q%!8
      WHEN 8
        PROCkey(!q%,q%!4,q%!24)
      WHEN 9
        PROCmenuselect(q%)
      WHEN 17,18
        PROCreceive(q%)
    ENDCASE
  UNTIL FALSE
ELSE
  Q%=HIMEM
  REM Remember memory size
  END=&4000000
  REM Grab all the memory
  ON ERROR ON ERROR OFF:OSCLI"Set ChangeFSI$ReturnCode "+REPORT$+" (code "+ STR$ERL+")":PROCremovearea:CLEAR:END=Q%:END
  foo%=0
  REM Call program
303
  A%=FNChangeFSI(A$,-1,-1,0,-1,-1,foo%,TRUE)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  PROCremovearea
  OSCLI"unset ChangeFSI$ReturnCode"
  CLEAR
  END=Q%
  REM Return it all again
ENDIF
END
:
REM    
DEF PROCdotemplate(a$)
  LOCAL A%
  IF version%>300 THEN
    SYS "Wimp_LoadTemplate",,0,,,,a$+z$,0 TO ,,indsize%
    DIM A% indsize%
    SYS "Wimp_LoadTemplate",,z%,A%,A%+indsize%,-1,a$+z$,0
  ELSE
    SYS "Wimp_LoadTemplate",,z%,idata%,eidata%,-1,a$+z$,0 TO ,,idata%
  ENDIF
ENDPROC
:
REM    
DEF PROClocale_initialise
  LOCAL p%
  LOCAL ERROR
  ON ERROR LOCAL decimal_point$=".":ENDPROC
  SYS "Territory_ReadSymbols",-1,0 TO p%
  decimal_point$=CHR$?p%
ENDPROC
:
REM    
DEF FNlocale_convert(n$,f$,t$)
  IF f$=t$ THEN =n$
  LOCAL i%
  i%=INSTR(n$,f$)
  IF i%<>0 : =LEFT$(n$,i%-1)+t$+MID$(n$,i%+1)
=n$
:
REM    
DEF FNsize_indirect(name$)
  LOCAL size%
  SYS "Wimp_LoadTemplate",,0,,,,name$+z$,0 TO ,,size%
=size%
:
REM Returns the largest buffer size necessary for passing
REM to R1 on a Wimp_LoadTemplate
DEF FNlargest_buffer
  LOCAL largest_tem%,size%,here%,name
  DIM name 12
  $name="*"
  here%=0
  largest_tem%=0
  SYS "Wimp_LoadTemplate", ,0,,,,name,0 TO ,size%,,,,,here%
  WHILE here%
    IF largest_tem%<size% largest_tem%=size%
    $name= "*"
    SYS "Wimp_LoadTemplate", ,0,,,,name,here% TO ,size%,,,,,here%
  ENDWHILE
=largest_tem%
:
REM    
DEF PROCreceive(q%)
  CASE q%!16 OF
    WHEN 0
367
      PROCfinishandquit
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    WHEN 1
      IF q%!20<>save% THEN
        !q%=60
        q%!12=q%!8
        q%!16=2
        q%!36=-1
        $(q%+44)="<Wimp$Scrap>"+CHR$0
        SYS "Wimp_SendMessage",17,q%,q%!4
        scrap%=q%!8
      ENDIF
    WHEN 2
      IF jpegsave% THEN
        SYS "OS_File",10,FNZS(q%+44),&c85,,sar%,sar%+jpegsize%
      ELSE
        SYS &2E,&10C,sar%,FNZS(q%+44)
      ENDIF
      $adsave%=FNZS(q%+44)
      q%!12=q%!8
      q%!16=3
      SYS "Wimp_SendMessage",18,q%,q%!4
      IF q%!36<>-1 saved%= TRUE
      SYS CrMenu,,-1
    WHEN 3
      F$=FNZS(q%+44)
      IF scrap% scrap%=scrap%=q%!12
      q%!12=q%!8
      q%!16=4
      SYS "Wimp_SendMessage",17,q%,q%!4
      PROCcallFSI
    WHEN 5
      CASE q%!40 OF
        WHEN &ff0,&c85
          q%!12=q%!8
          q%!16=4
          SYS "Wimp_SendMessage",17,q%,q%!4
          F$=FNZS(q%+44)
          PROCcallFSI
      ENDCASE
    WHEN 8
      PROCchecksaved(q%)
    WHEN 9,&400C1
      IF sprite% PROCgetmodeinfo:SYS &400d1,pic%,0,-32767,32767,0
      PROCshowscaletofit
    WHEN 10
      SYS "XOS_ReadVarVal","ChangeFSI$Dir",paltemp%,256,0,3 TO ,,bootlen%
      paltemp%?bootlen%=13
      BPUT#q%!20,"Run "+$paltemp%
415 416 417
    WHEN &502
      REM Interactive help
      PROCgethelp(q%!32,q%!36)
418 419 420 421
  ENDCASE
ENDPROC
:
REM    
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
DEF PROCgethelp(w%,i%)
  IF i%<-1 THEN ENDPROC
  CASE w% OF
    WHENinfow%
      $(q%+20)=FNmsg0("Hinfo"+STR$(i%))
    WHENproc%
      $(q%+20)=FNmsg0("Hproc"+STR$(i%))
    WHENoutput%
      $(q%+20)=FNmsg0("Hout"+STR$(i%))
    WHENscalew%
      $(q%+20)=FNmsg0("Hscale"+STR$(i%))
    WHENsave%
      $(q%+20)=FNmsg0("Hsave"+STR$(i%))
    WHENsinfo%
      $(q%+20)=FNmsg0("Hsprite"+STR$(i%))
    WHENsrcinfo%
      $(q%+20)=FNmsg0("Hsrc"+STR$(i%))
    WHENraninfo%
      $(q%+20)=FNmsg0("Hrange"+STR$(i%))
    WHENzoom%
      $(q%+20)=FNmsg0("Hzoom"+STR$(i%))
    WHENjpego%
      $(q%+20)=FNmsg0("Hjpeg"+STR$(i%))
    WHENpic%
      $(q%+20)=FNmsg0("Hpic"+STR$(i%))
    WHEN-2
      ENDPROC:REM Default from !Help used
    OTHERWISE
      IF i%<>-1 THEN
        REM Must be a menu then
        SYS "Wimp_GetMenuState",1,q%+20,w%,i%
        IF lastm%=101 THEN
          REM The iconbar menu
          LOCAL grey$
          IF (q%!20=6) AND vram% THEN grey$="g"
          IF (q%!20=5) AND (F$="" OR F$="SpriteFile" OR F$="JPEGImage") THEN grey$="g"
          $(q%+20)=FNmsg0("Himnu"+STR$(q%!20)+grey$)
        ELSE
          IF lastm%=pic% THEN
            REM The picture menu
            $(q%+20)=FNmsg0("Hpmnu"+STR$(q%!20))
          ELSE
            REM Yikes, some menu we didn't open
            ENDPROC
          ENDIF
        ENDIF
      ENDIF
  ENDCASE
  q%!0=((20+LEN($(q%+20))+1)+3)ANDNOT3
  q%!12=q%!8
  q%!16=&503:REM Message_HelpReply
  SYS "Wimp_SendMessage",17,q%,q%!4
ENDPROC
:
REM    
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
DEF PROCcallFSI
  IF FNcheckvalues <> 0 THEN ENDPROC
  A$=FNdeducemode
  A%=47
  jpegsave%= FALSE
  IF dest%=0 THEN
    jpegsave%= TRUE
    A$="JPEG"
    IF FNgeticonstate(jpego%,2) A$="JPEGMONO"
    A$+=$adjpegq%
  ELSE
    IF FNgeticonstate(output%,15) THEN
      A$+=$adspcl%
    ELSE
      IF FNgeticonstate(output%,16) THEN
        IF FNgeticonstate(output%,5) OR FNgeticonstate(output%,11) A$+="R"
      ELSE
        SYS 53,VAL A$,3 TO ,,A%
        IF A%=63 THEN
          IF FNgeticonstate(output%,6) A$="27t"
          IF FNgeticonstate(output%,12) A$="12t"
        ELSE
          IF A%=255 A$+="d"
        ENDIF
      ENDIF
    ENDIF
  ENDIF
  A$="garbage "+F$+" <ChangeFSI$Dir>.Foo "+A$
  PROCChangeFSISuffix
  info$=""
507
  range$=FNmsg0("NoRange")
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
  cputime%=0
  IF sprite% !q%=pic%:SYS &400c3,,q%:saved%= TRUE :sprite%= FALSE
  nextlocation%=HIMEM
  SYS "Wimp_SlotSize",originalslot%,-1
  PROCremovearea
  IF fast% SYS "OS_UpdateMEMC",&300,&700 TO oldmemc
  A%=FNChangeFSI(A$,-1,-1,nextlocation%,0,0,sar%,0)
  IF fast% SYS "OS_UpdateMEMC",oldmemc,&700
  IF scrap% scrap%= FALSE :SYS "OS_File",6,F$
  IF A%<2 AND sar%<>0 THEN
    $adsx%= STR$xsp%
    $adsy%= STR$ysp%
    IF jpegsave% THEN
      $adsm%="JPEG"
      $adsname%="JPEG"
      $adsbytes%= STR$jpegsize%
      x=xsp%*2
      y=ysp%*2
      SYS &35,-1,4 TO ,,nx
      SYS &35,-1,5 TO ,,ny
      !ifactors%=1
      ifactors%!4=1
      ifactors%!8=nx
      ifactors%!12=ny
      $adsavesprite%="file_c85"
      IF LEFT$(F$,7)="<Wimp$S" F$="JPEGImage"
    ELSE
      SYS &2002E,&10d,sar%,pixtrans%,255,1
      n$=FNZS(pixtrans%)
      SYS &2002E,&128,sar%,n$ TO ,,,x,y,,m
      $adsm%= STR$m
      $adsname%=n$
      $adsbytes%= STR$sar%!12
      SYS &35,m,4 TO ,,nx
      x=x<<nx
      SYS &35,m,5 TO ,,ny
      y=y<<ny
      PROCgetmodeinfo
      $adsavesprite%="file_ff9"
      IF LEFT$(F$,7)="<Wimp$S" F$="SpriteFile"
    ENDIF
    !q%=save%
    q%!4=0
    q%!8=0
    q%!12=0
    SYS SetI,,q%
    $adran%=range$
555
    $adsrc%=info$+" "+FNmsg1("Proc",STR$(cputime% DIV 100)+decimal_point$+RIGHT$("0"+ STR$(cputime%MOD 100),2))
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
    saved%= FALSE
    sprite%= TRUE
    REM Get the leafname of F$ for the save box (so you don't overwrite the source),but have the full path in the title bar
    A%=LEN F$
    WHILE MID$("."+F$,A%,1)<>"."
      A%-=1
    ENDWHILE
    $adsave%=MID$("."+F$,A%+1)
    pic%=FNcreate_window(x,y,F$)
    PROCpopup(pic%,-1)
  ENDIF
ENDPROC
:
REM    
DEF FNcheckvalues
  LOCAL e,error$,flag%
  REM Is the JPEG quality within the correct range?
  IF dest%=0 AND VAL ($adjpegq%) > 100 THEN
    e = 1
575
    error$ = FNmsg0("BadJQ")
576 577 578 579 580 581 582 583 584
  ENDIF
  REM Is it a numerical mode?
  IF LEFT$($admode%,1) < "A" AND dest%=1 THEN
    SYS &35, VAL ($admode%), 0 TO ;flag%
    REM Is the numerical mode valid?
    IF LEN ($admode%) = 0 flag%=2
    IF VAL ($admode%) > 127 AND VAL ($admode%) < 256 flag%=2
    IF (flag% AND 2) > 0 THEN
      e = 2
585
      error$ = FNmsg0("BadMode")
586 587 588 589 590 591
    ENDIF
  ENDIF
  IF e <> 0 THEN
    SYS Drag,,-1
    !buffer%=e
    $(buffer%+4)=error$+CHR$0
592 593
    SYS "Wimp_ReportError",buffer%,%01,FNmsg0("_TaskName") TO ,A%
    IF A%=2 THEN PROCfinishandquit
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
  ENDIF
=e
:
REM    
DEF PROCChangeFSISuffix
  safe%= TRUE
  IF FNgeticonstate(scalew%,1) A$+=" "+ STR$tofitx+": "+ STR$tofity+":":safe%= FALSE
  IF FNgeticonstate(scalew%,5) A$+=" 1:2":safe%= FALSE
  IF FNgeticonstate(scalew%,4) A$+=" 1:2 1:1":safe%= FALSE
  IF FNgeticonstate(scalew%,3) A$+=" 1:1 1:2":safe%= FALSE
  IF FNgeticonstate(scalew%,9) THEN
    safe%= FALSE
    IF VAL $s%(0) A$+=" "+$s%(0) ELSE A$+=" 1"
    A$+=":"+$s%(1)
    IF VAL $s%(2) A$+=" "+$s%(2) ELSE A$+=" 1"
    A$+=":"+$s%(3)
  ENDIF
  IF FNgeticonstate(scalew%,0) A$+=" -nosize"
  IF FNgeticonstate(scalew%,18) A$+=" -noscale"
  IF FNgeticonstate(scalew%,19) A$+=" -lock"
  IF FNgeticonstate(scalew%,6) THEN
    safe%= FALSE
    IF FNgeticonstate(scalew%,16) A$+=" -rotate" ELSE A$+=" -rotate-"
  ENDIF
  IF FNgeticonstate(scalew%,7) A$+=" -hflip":safe%= FALSE
  IF FNgeticonstate(scalew%,8) A$+=" -vflip":safe%= FALSE
  IF FNgeticonstate(proc%,0) A$+=" -range":safe%= FALSE
  IF FNgeticonstate(proc%,1) A$+=" -equal":safe%= FALSE
  IF FNgeticonstate(proc%,2) A$+=" -nodither"
  IF FNgeticonstate(proc%,3) A$+=" -invert":safe%= FALSE
  IF FNgeticonstate(proc%,4) A$+=" -brighten"
  IF FNgeticonstate(proc%,5) A$+=" -black"+$p%(0)
  IF FNgeticonstate(proc%,6) A$+=" -gamma"+FNlocale_convert($p%(1),decimal_point$,"."):safe%= FALSE
  IF FNgeticonstate(proc%,7) A$+=" -sharpen"+$p%(2):safe%= FALSE
  IF FNgeticonstate(proc%,11) A$+=" -smooth"+$p%(3)
ENDPROC
:
REM    
632 633 634 635
DEF PROCfinishandquit
  PROCremovearea
  SYS "Wimp_CloseDown"
  SYS "XMessageTrans_CloseFile",msgb%
636
  END
637 638 639
ENDPROC
:
REM    
640
DEF PROCcheckandfinish
641
  IF saved% OR NOT sprite% THEN PROCfinishandquit
642
  !buffer%=ERR
643 644 645
  $(buffer%+4)=FNmsg1("Unsaved",F$)+CHR$0
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButCon") TO ,A%
  IF A%=4 THEN PROCfinishandquit
646 647 648 649 650 651
ENDPROC
:
REM    
DEF PROCchecksaved(q%)
  IF saved% OR NOT sprite% ENDPROC
  !buffer%=ERR
652 653
  $(buffer%+4)=FNmsg1("Unsaved",F$)+CHR$0
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButCon") TO ,A%
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  q%!12=q%!8
  IF A%<>4 SYS "Wimp_SendMessage" ,19, q%, 0
ENDPROC
:
REM    
DEF PROCkey(handle%,icon%,key%)
  IF key%=&1b SYS CrMenu,,-1:ENDPROC
  CASE handle% OF
    WHEN zoom%
      CASE key% OF
        WHEN 13
          PROCdozoom(icon%-4,0)
          IF icon%=4 SYS &400d2,zoom%,5,,,-1,LEN $z%(0)
          IF icon%=5 SYS CrMenu,,-1
          ENDPROC
        WHEN &18e,&18f
          PROCdozoom(icon%-4,0)
          SYS &400d2,zoom%,icon% EOR 1,,,-1,LEN $z%(icon% EOR 5)
          ENDPROC
      ENDCASE
    WHEN save%
      IF key%=13 PROCsavesprite:ENDPROC
    WHEN output%
677
      IF icon%=33 AND FNgeticonstate(output%, 24) THEN $admode% = $oldmode%:!q%=output%:q%!4=20:q%!8=0:q%!12=0:SYS SetI,,q%
678 679 680 681 682
      PROCshowscaletofit
      IF key%=13 SYS CrMenu,,-1:ENDPROC
      IF key%=&18e THEN
        CASE icon% OF
          WHEN 19
683
            icon%=33
684
            key%=oldmode%
685
          WHEN 33
686 687 688 689 690 691
            icon%=19
            key%=adspcl%
        ENDCASE
      ENDIF
      IF key%=&18f THEN
        CASE icon% OF
692
          WHEN 33
693 694 695
            icon%=19
            key%=adspcl%
          WHEN 19
696 697
            icon%=33
            key%=oldmode%
698 699 700 701 702 703 704
        ENDCASE
      ENDIF
      IF key%>1024 SYS &400d2,output%,icon%,,,-1,LEN $key%:ENDPROC
    WHEN proc%
      CASE key% OF
        WHEN &18e,13
          icon%+=1
705
          IF icon%>11 icon%=8:IF key%=13 SYS CrMenu,,-1:ENDPROC
706 707 708 709
          SYS &400d2,proc%,icon%,,,-1,LEN $p%(icon%-8)
          ENDPROC
        WHEN &18f
          icon%-=1
710
          IF icon%<8 icon%=11
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
          SYS &400d2,proc%,icon%,,,-1,LEN $p%(icon%-8)
          ENDPROC
      ENDCASE
    WHEN scalew%
      CASE key% OF
        WHEN &18e,13
          icon%+=1
          IF icon%>13 icon%=10:IF key%=13 SYS CrMenu,,-1:ENDPROC
          SYS &400d2,scalew%,icon%,,,-1,LEN $s%(icon%-10)
          ENDPROC
        WHEN &18f
          icon%-=1
          IF icon%<10 icon%=13
          SYS &400d2,scalew%,icon%,,,-1,LEN $s%(icon%-10)
          ENDPROC
      ENDCASE
  ENDCASE
  SYS "Wimp_ProcessKey",key%
ENDPROC
:
REM    
DEF PROCsavesprite
  IF F$ = $adsave% THEN
    SYS Drag,,-1
    !buffer%=3
736 737
    $(buffer%+4)=FNmsg0("OverW")+CHR$0
    SYS "Wimp_ReportError",buffer%,%11,FNmsg0("_TaskName") TO ,A%
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    IF A%=2 THEN
      REM Cancel
      SYS CrMenu,,-1
      ENDPROC
    ENDIF
  ENDIF
  IF jpegsave% THEN
    SYS "OS_File",10,$adsave%,&c85,,sar%,sar%+jpegsize%
  ELSE
    SYS &2E,&10C,sar%,$adsave%
  ENDIF
  SYS CrMenu,,-1
ENDPROC
:
REM    
DEF PROCcheckmouse(mousex%,mousey%,b%,handle%,icon%,ob%)
754 755 756 757 758 759 760 761
  LOCAL moved%,imenu$,pmenu$
  REM Picture menu structure
  pmenu$=       FNmsg0("Pimg")+">sinfo%,"+FNmsg0("Psrc")+">srcinfo%,"+FNmsg0("Prange")+">raninfo%,"
  pmenu$=pmenu$+FNmsg0("Pzoom")+">zoom%,"+FNmsg0("Psave")+">save%,"+FNmsg0("PIredo")
  REM Iconbar menu structure
  imenu$=       FNmsg0("Iinfo")+">infow%,"+FNmsg0("Iscale")+">scalew%,"+FNmsg0("Iproc")+">proc%,"
  imenu$=imenu$+FNmsg0("Ispr")+">output%:dest%,"+FNmsg0("Ijpg")+">jpego%:1-dest%,"+FNmsg0("PIredo")+","
  imenu$=imenu$+FNmsg0("Ifast")+":fast%AND1,"+FNmsg0("Ichoice")+","+FNmsg0("Iquit")
762
  moved%= FALSE
763
  REM Button 'menu'
764 765 766
  IF b% AND 2 THEN
    CASE handle% OF
      WHEN pic%
767
        PROCdomenu(pic%,pmenu$,FNmsg0("_TaskName"))
768
      WHEN -2
769
        PROCdomenu(101,imenu$,FNmsg0("_TaskName"))
770 771 772
    ENDCASE
    rmenuhandle%=handle%
  ENDIF
773
  REM Button 'select'
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
  IF b% AND 4 THEN
    CASE handle% OF
      WHEN zoom%
        CASE icon% OF
          WHEN 0
            PROCdozoom(0,1)
          WHEN 1
            PROCdozoom(0,-1)
          WHEN 2
            PROCdozoom(1,1)
          WHEN 3
            PROCdozoom(1,-1)
        ENDCASE
    ENDCASE
  ENDIF
789
  REM Button 'adjust'
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
  IF b% AND 1 THEN
    CASE handle% OF
      WHEN zoom%
        CASE icon% OF
          WHEN 0
            PROCdozoom(0,-1)
          WHEN 1
            PROCdozoom(0,1)
          WHEN 2
            PROCdozoom(1,-1)
          WHEN 3
            PROCdozoom(1,1)
        ENDCASE
    ENDCASE
  ENDIF
805
  REM Button 'adjust' or 'select'
806 807 808
  IF b% AND 5 THEN
    CASE handle% OF
      WHEN output%
Robert Sprowson's avatar
Robert Sprowson committed
809
        IF dest%=0 THEN dest%=1:PROCdomenu(101,imenu$,FNmsg0("_TaskName")):SYS Getcaret%,,q%:moved%= TRUE
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
        CASE icon% OF
          WHEN 3,4,5,6
            PROCkillcurrent( STR$(22+icon%))
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 7
            PROCkillcurrent("S16,90,90")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 8
            PROCkillcurrent("S32,90,90")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 9
            PROCkillcurrent("0")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 10
            PROCkillcurrent("8")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 11
            PROCkillcurrent("12")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 12
            PROCkillcurrent("15")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 13
            PROCkillcurrent("S16,90,45")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 14
            PROCkillcurrent("S32,90,45")
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            q%!4=24
            SYS SetI,,q%
          WHEN 15
            PROCkillcurrent(FNdeducemode)
            SYS SetI,,q%
            SYS Caretpos%,output%,19,,,-1,LEN ($adspcl%)
            !q%=output%
            q%!4=7
            q%!8=0
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
          WHEN 16
            PROCkillcurrent(FNdeducemode)
            !q%=output%
            q%!4=7
            q%!8=0
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
          WHEN 17
            PROCkillcurrent(FNdeducemode)
            !q%=output%
            q%!4=7
            q%!8=1<<22
            q%!12=1<<22
            SYS SetI,,q%
            q%!4=8
            SYS SetI,,q%
            q%!4=13
            SYS SetI,,q%
            q%!4=14
            SYS SetI,,q%
            IF LEFT$($admode%,1) = "S" THEN
              IF RIGHT$($admode%,2) = "45" THEN
                PROCkillcurrent("15")
                !q%=output%
                q%!4=18
                q%!8=0
                q%!12=1<<21
                SYS SetI,,q%
                q%!4=24
                SYS SetI,,q%
              ELSE
                PROCkillcurrent("28")
                !q%=output%
                q%!4=18
                q%!8=0
                q%!12=1<<21
                SYS SetI,,q%
                q%!4=24
                SYS SetI,,q%
              ENDIF
            ENDIF
          WHEN 18
            !q%=output%
            q%!4=24
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
          WHEN 24
            !q%=output%
            q%!4=18
            q%!8=0
            q%!12=1<<21
            SYS SetI,,q%
            PROCkillcurrent(FNdeducemode)
967 968
            SYS Caretpos%,output%,33,,,-1,LEN ($oldmode%)
          WHEN 33
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
            IF moved% SYS Caretpos%,output%,q%!4,q%!8,q%!12,q%!16,q%!20
          WHEN 19
            IF moved% SYS Caretpos%,output%,q%!4,q%!8,q%!12,q%!16,q%!20
        ENDCASE
        IF FNgeticonstate(output%,18) $admode%=FNdeducemode:REM SYS Caretpos%,output%,20,,,-1,LEN(FNdeducemode)
        !q%=output%
        q%!4=20
        q%!8=0
        q%!12=0
        SYS SetI,,q%
        PROCshowscaletofit
      WHEN save%
        CASE icon% OF
          WHEN 2
            PROCsavesprite
        ENDCASE
      WHEN -2
        IF sprite% PROCgetw(pic%):PROCopen_window(pic%,x0%,y0%,x1%,y1%,scx%,scy%,-1)
      WHEN jpego%
        IF dest%=1 THEN
          SYS Getcaret%,,q%
          dest%=0
991
          PROCdomenu(101,imenu$,FNmsg0("_TaskName"))
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
          IF icon%=5 SYS Caretpos%,jpego%,q%!4,q%!8,q%!12,q%!16,q%!20
        ENDIF
        CASE icon% OF
          WHEN 3
            IF b% AND 4 $adjpegq% = STR$(VAL ($adjpegq%)-1) ELSE $adjpegq% = STR$(VAL ($adjpegq%)+1)
            IF VAL ($adjpegq%)<0 $adjpegq%="0"
            IF VAL ($adjpegq%)>100 $adjpegq%="100"
            !q%=jpego%
            q%!4=5
            q%!8=0
            q%!12=0
            SYS SetI,,q%
            SYS Caretpos%,jpego%,5,,,-1,LEN ($adjpegq%)
          WHEN 4
            IF b% AND 4 $adjpegq% = STR$(VAL ($adjpegq%)+1) ELSE $adjpegq% = STR$(VAL ($adjpegq%)-1)
            IF VAL ($adjpegq%)<0 $adjpegq%="0"
            IF VAL ($adjpegq%)>100 $adjpegq%="100"
            !q%=jpego%
            q%!4=5
            q%!8=0
            q%!12=0
            SYS SetI,,q%
            SYS Caretpos%,jpego%,5,,,-1,LEN ($adjpegq%)
          WHEN 5
            REM Quality
        ENDCASE
    ENDCASE
  ENDIF
  IF b% AND &50 THEN
    CASE handle% OF
      WHEN save%
        CASE icon% OF
          WHEN 0
            PROCgetw(save%)
            !q%=save%
            q%!4=0
            SYS GetI,,q%
            !q%=save%
            q%!4=5
            q%!8+=bx%
            q%!12+=by%
            q%!16+=bx%
            q%!20+=by%
            q%!24=0
            q%!28=0
            q%!32=&7FFFFFFF
            q%!36=&7FFFFFFF
            t$="file_ff9"
            IF jpegsave% t$="file_c85"
            IF dstart% SYS dstart%,%11000101,1,t$,q%+8,q%+24 ELSE SYS "Wimp_DragBox",,q%
        ENDCASE
    ENDCASE
  ENDIF
ENDPROC
:
REM    
DEF PROCdozoom(s%,a%)
  IF s%=0 THEN
    $z%(0)= STR$(VAL $z%(0)+a%)
    IF $z%(0)="0" $z%(0)="1"
  ELSE
    $z%(1)= STR$(VAL $z%(1)+a%)
    IF $z%(1)="0" $z%(1)="1"
  ENDIF
  !q%=zoom%
  q%!4=4+s%
  q%!8=0
  q%!12=0
  SYS SetI,,q%
  !q%=pic%
  SYS GetW,,q%
  f%=q%!28
  SYS &400c3,,q%
  pic%=FNcreate_window(x,y,F$)
  PROCpopup(pic%,f%)
ENDPROC
:
REM    
DEF PROCdomenu(handle%,menu$,menutitle$)
  menuhandle%=handle%
  menuptr%=buffer%
  indirect%=idata%
  i%=0
  menuptr%!20=44
  $menuptr%=menutitle$
  menuptr%?12=7
  menuptr%?13=2
  menuptr%?14=7
  menuptr%?15=0
  menuptr%!16=200
  menuptr%!24=0
  menuptr%+=28
  REPEAT
    i1%=i%+1
    i%=INSTR(menu$+",",",",i1%)
    item$=MID$(menu$,i1%,i%-i1%)
    PROCmenuitem(item$)
  UNTIL item$=""
  menuptr%!-24=(menuptr%!-24) OR &80
  IF handle%=101 SYS CrMenu,,buffer%,mousex%-64,96+9*44 ELSE SYS CrMenu,,buffer%,mousex%-64,mousey%
1092
  lastm%=handle%:REM Record last menu opened for interactive help
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
ENDPROC
:
REM    
DEF PROCmenuitem(text$)
  IF text$="" ENDPROC
  flg%=0
  submenu%=-1
  I%=INSTR(text$,":")
  IF I% THEN
    flg%=EVALMID$(text$,I%+1)
    text$=LEFT$(text$,I%-1)
  ENDIF
  I%=INSTR(text$,">")
  IF I% THEN
    submenu%=EVALMID$(text$,I%+1)
    text$=LEFT$(text$,I%-1)
  ENDIF
  menuptr%!0=flg%
  menuptr%!4=submenu%
1112
  IF (text$=FNmsg0("PIredo") AND (F$="" OR F$="SpriteFile" OR F$="JPEGImage")) OR (text$=FNmsg0("Ifast") AND vram%) THEN
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
    menuptr%!8=&07400021
  ELSE
    menuptr%!8=&07000021
  ENDIF
  IF LEN text$*16+8>buffer%!16 buffer%!16=LEN text$*16+8
  IF LEN text$>12 THEN
    menuptr%!8=menuptr%!8 OR 256
    menuptr%!20=LEN text$+1
    menuptr%!16=0
    menuptr%!12=indirect%
    $indirect%=text$
    indirect%+=LEN text$+2
  ELSE
    $(menuptr%+12)=text$
  ENDIF
  menuptr%+=24
ENDPROC
:
REM    
DEF PROCmenuselect(menus%)
  SYS GetP,,paltemp%
  adjust%=paltemp%!8 AND 1
  SYS DcMenu,,buffer%,menus%, STRING$(200," ") TO ,,,a$
  IF a$="" THEN ENDPROC
  CASE menuhandle% OF
    WHEN pic%
      CASE a$ OF
1140
        WHEN FNmsg0("PIredo")
1141 1142 1143 1144
          PROCcallFSI
      ENDCASE
    WHEN 101
      CASE a$ OF
1145
        WHEN FNmsg0("Ifast")
1146
          fast%=NOT fast%
1147
        WHEN FNmsg0("Ijpg")
1148
          dest%=0
1149
        WHEN FNmsg0("Ispr")
1150
          dest%=1
1151
        WHEN FNmsg0("PIredo")
1152
          PROCcallFSI
1153
        WHEN FNmsg0("Ichoice")
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
          statescale%=0
          FOR I%=0 TO 9
            statescale%+=FNgeticonstate(scalew%,I%)<<I%
          NEXT
          FOR I%=10 TO 13
            statescale%+=FNgeticonstate(scalew%,I%+6)<<I%
          NEXT
          stateproc%=0
          FOR I%=0 TO 7
            stateproc%+=FNgeticonstate(proc%,I%)<<I%
          NEXT
          stateoutput%=0
          FOR I%=0 TO 26
            stateoutput%+=FNgeticonstate(output%,I%)<<I%
          NEXT
          statejpeg%=0
          FOR I%=0 TO 3
            statejpeg%+=FNgeticonstate(jpego%,I%)<<I%
          NEXT
          OSCLI"CDIR <Choices$Write>.ChangeFSI"
          A%= OPENOUT"<Choices$Write>.ChangeFSI.Choices"
          PRINT#A%,fast%,dest%,statescale%,stateproc%,stateoutput%,statejpeg%,$adjpegq%,$oldmode%
          PRINT#A%,$p%(0),FNlocale_convert($p%(1),decimal_point$,"."),$p%(2),$p%(3),$admode%,$adspcl%
          PRINT#A%,$s%(0),$s%(1),$s%(2),$s%(3)
          CLOSE#A%
1179
        WHEN FNmsg0("Iquit")
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
          PROCcheckandfinish
      ENDCASE
  ENDCASE
  IF adjust% PROCcheckmouse(0,0,2,rmenuhandle%,0,0)
ENDPROC
:
REM    
DEF FNcreate_window(x,y,title$)
  x=x*VAL $z%(0) DIV VAL $z%(1)
  y=y*VAL $z%(0) DIV VAL $z%(1)
  !z%=0
  z%!4=-y
  z%!8=x
  z%!12=0
  z%!40=0
  z%!44=-y
  z%!48=x
  z%!52=0
  IF x<128 OR y<128 z%?35=0 ELSE z%?35=255
  $adtitle%=""
  SYS CreateW,,z% TO handle%
  $adtitle%=title$
=handle%
:
REM    
DEF PROCpopup(handle%,f%)
  SYS "OS_ReadModeVariable",-1,4 TO ,,modexshift%
  SYS "OS_ReadModeVariable",-1,5 TO ,,modeyshift%
  SYS "OS_ReadModeVariable",-1,11 TO ,,rightscr%
  SYS "OS_ReadModeVariable",-1,12 TO ,,topscr%
  rightscr%=rightscr%<<modexshift%
  topscr%=topscr%<<modeyshift%
  PROCgetw(handle%)
  IF isopen% THEN
    REM It's already open,so remember its top left corner
    xo%=isopen% AND &FFFF
    yo%=isopen% >>>16
  ELSE
    REM New image,start in the middle
    xo%=rightscr%/2-(x0%+x1%)/2
    yo%=topscr%/2-(y0%+y1%)/2
  ENDIF
  PROCopen_window(handle%,x0%+xo%,y0%+yo%,x1%+xo%,y1%+yo%,scx%,scy%,f%)
ENDPROC
:
REM    
DEF PROCopen_window(handle%,x0%,y0%,x1%,y1%,scx%,scy%,bhandle%)
  IF handle%=-1 THEN ENDPROC
  q%!0=handle%
  q%!28=bhandle%
  q%!4=x0%
  q%!8=y0%
  q%!12=x1%
  q%!16=y1%
  q%!20=scx%
  q%!24=scy%
  SYS OpenW,,q%
  isopen%=x0% OR (y1%<<16)
ENDPROC
:
REM    
DEF PROCgetw(handle%)
  !q%=handle%
  SYS GetW,,q%
  PROCinfo(q%+4)
  bhandle%=q%!28
  flags%=q%!32
ENDPROC
:
REM    
DEF PROCinfo(q%)
  x0%=!q%
  y0%=q%!4
  x1%=q%!8
  y1%=q%!12
  scx%=q%!16
  scy%=q%!20
  bx%=x0%-scx%
  by%=y1%-scy%
ENDPROC
:
REM    
DEF PROCredraw_window(handle%)
  IF handle%=pic% THEN
    !factors%=!ifactors%*VAL $z%(0)
    factors%!4=ifactors%!4*VAL $z%(0)
    factors%!8=ifactors%!8*VAL $z%(1)
    factors%!12=ifactors%!12*VAL $z%(1)
    PROCreduce(!factors%,factors%!8)
    PROCreduce(factors%!4,factors%!12)
    !q%=handle%
    SYS RedrawW,,q% TO more%
    PROCinfo(q%+4)
    by%=by%-INT (y*VAL $z%(0) DIV VAL $z%(1))
    WHILE more%
      IF jpegsave% THEN
        IF jpegpaint% SYS jpegpaint%,sar%,bx%,by%,factors%,jpegsize%,1 TO ;V%:IF (V% AND 1)=0 ELSE CLG
      ELSE
        SYS &2002E,&134,sar%,n$,bx%,by%,,factors%,spx%
      ENDIF
      SYS GetR%,0,q% TO more%
    ENDWHILE
  ENDIF
ENDPROC
:
REM    
DEF FNgeticonstate(handle%,icon%)
  !q%=handle%
  q%!4=icon%
  SYS GetI,,q%
=q%!24>>21 AND 1
:
REM    
DEF PROCgetmodeinfo
  IF jpegsave% THEN
    SYS &35,-1,4 TO ,,nx
    SYS &35,-1,5 TO ,,ny
    !ifactors%=1
    ifactors%!4=1
    ifactors%!8=nx
    ifactors%!12=ny
    ENDPROC
  ENDIF
  SYS "XWimp_ReadPixTrans",&100,sar%,n$,,,,ifactors%,pixtrans%
  SYS &2002E,&118,sar%,n$ TO ,,sptr%
  IF sptr%!32=44 THEN
    palptr%=0
  ELSE
    FOR grab%=0 TO 2048-8 STEP 8
      paltemp%!(grab%>>1)=sptr%!(grab%+44)
    NEXT
    palptr%=paltemp%
  ENDIF
  FOR Q%=0 TO 255
    pixtrans%?Q%=Q%
  NEXT
  IF sptr%!32=44+2048 THEN
    FOR Q%=0 TO 255
      SYS ctran%,palptr%!(Q%<<2) TO pixtrans%?Q%
    NEXT
  ELSE
    SYS "ColourTrans_SelectTable",m,palptr%,-1,-1,pixtrans%
  ENDIF
  spx%=0
  FOR Q%=0 TO 255
    IF pixtrans%?Q%<>Q% spx%=pixtrans%
  NEXT
ENDPROC
:
REM    
DEF FNdeducemode
  LOCAL m,x,y
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
  IF FNgeticonstate(output%,18) THEN
    REM Use current screen mode
    SYS &35,-1,9 TO ,,m:REM log2bpp
    IF m>3 THEN
      SYS &35,-1,4 TO ,,x:REM xeigen
      SYS &35,-1,5 TO ,,y:REM yeigen
      x=180/(2^x)
      y=180/(2^y)
      ="S"+ STR$(2^m)+","+ STR$x+","+ STR$y
    ELSE
      = STR$MODE
    ENDIF
1344
  ELSE
1345 1346 1347
    IF FNgeticonstate(output%,24) THEN
      REM Use the number from the writeable icon
      =$oldmode%
1348
    ELSE
1349
      =$admode%
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
    ENDIF
  ENDIF
:
REM    
DEF PROCshowscaletofit
  LOCAL m,x,y
  $admode%=FNdeducemode
  CASE ?admode% OF
    WHEN ASC "S"
      IF VAL MID$($admode%,5)>VAL MID$($admode%,8) x=6 ELSE x=0
      IF VAL MID$($admode%,2)>16 x+=5 ELSE x+=4
      SYS &35,-1,11 TO ,,tofitx
      SYS &35,-1,12 TO ,,tofity
    WHEN ASC "J"
      SYS &35,-1,11 TO ,,tofitx
      SYS &35,-1,12 TO ,,tofity
      REM IFVALMID$($admode%,5)=8 x=3 ELSE x=5
      x = 12
      REM So no radio button is selected.
    OTHERWISE
      REM IF FNgeticonstate(output%,24) THEN
      REM  SYS&35,m,11 TO,,tofitx:SYS&35,m,12 TO,,tofity
      REM  x = 13: REM So no radio button is selected.
      REM ELSE
      m=VAL $admode%
      SYS &35,m,11 TO ,,tofitx
      SYS &35,m,12 TO ,,tofity
      SYS &35,m,4 TO ,,x
      SYS &35,m,5 TO ,,y
      IF y>x x=6 ELSE x=0
      SYS &35,m,9 TO ,,y
      x+=y
      REM ENDIF
  ENDCASE
  tofitx+=1
  tofity+=1
1386
  $adscale%=FNmsg2("ScaleFill",STR$tofitx,STR$tofity)
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  q%!12=1<<21
  !q%=output%
  FOR I%=0 TO 11
    IF I%=x q%!8=1<<21 ELSE q%!8=0
    q%!4=I%+3
    SYS SetI,,q%
  NEXT
  IF x=12 q%!8=0:q%!4=24:SYS SetI,,q%
ENDPROC
:
REM    
DEF PROCkillcurrent(m$)
  q%!12=1<<21
  !q%=output%
  q%!8=0
  q%!4=20
  SYS SetI,,q%
  $admode%=m$
  REM SYS Caretpos%,output%,20,,,-1,LEN(m$)
ENDPROC
:
REM    
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
DEF FNmsg0(t$)
=FNmsg2(t$,"","")
:
REM    
DEF FNmsg1(t$,a$)
=FNmsg2(t$,a$,"")
:
REM    
DEF FNmsg2(t$,a$,b$)
  LOCALr$
  REM Superset of message lookup with substitution
  SYS"MessageTrans_Lookup",msgb%,t$,STRING$(200,"@"),200,a$,b$ TO,,r$
=r$
:
REM    
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
DEF FNZS(I%)LOCAL A$
  REPEAT
    A$+=CHR$?I%
    I%+=1
  UNTIL ?I%<14
=A$
:
REM    
DEF PROCerrorbox
  SYS Drag,,-1
  !buffer%=ERR
  $(buffer%+4)=REPORT$+CHR$0
  REM $(buffer%+4)=REPORT$+" (ref: "+STR$ERL+")"+CHR$0
1437 1438
  SYS "Wimp_ReportError",buffer%,%010100000000,FNmsg0("_TaskName"),"!changefsi",1,FNmsg0("ButErr") TO ,A%
  IF A%=4 THEN PROCfinishandquit
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
ENDPROC
:
REM A$ Command String
REM spritearea% = address                 - Use the spritearea at 'address'
REM            or -1
REM
REM workspace% = -1 and worklimit% = 0    - Use BASIC variable space
REM
REM workspace% = -1                       - ChangeFSI's slot handling
REM worklimit% = current end of wimp slot   will be used.
REM
REM workspace% <> -1                      - Use memory at workspace%
REM worklimit% = end of workspace%
REM
REM oksave% TRUE  = save to output file given in command string
REM okinfo% TRUE  = print messages (and histograms if appropriate)
REM                 during processing
REM ram% receives address of sprite area
REM fast%   TRUE  = switch down to MODE0 to reduce RAM bandwidth during
REM                 processing (only useful if no VRAM fitted)
REM Returned values
REM 0   = Image processed as requested
REM 1   = Information messages printed
REM 100 = Error - image may not be complete
REM
DEF FNChangeFSI(A$,spritearea%,workspace%,worklimit%,oksave%,okinfo%,RETURN ram%,fast%)
  LOCAL A$(),r%(),g%(),b%(),rpal%(),gpal%(),bpal%(),table%(),cl%(),xl%(),xl2%()
  LOCAL xp%(),rm%(),rm1%(),rm2%(),vals%(),nl%(),A%(),B%(),new%(),st%(),jpq%(),pos%,m,Z%()
  DIM Z%(2): Z%(0)=%10000:Z%(2) =%10010
  WHILE LEFT$(A$,1)<>" " AND LEN A$<>0 A$=MID$(A$,2)
  ENDWHILE
  WHILE LEFT$(A$,1)=" " A$=MID$(A$,2)
  ENDWHILE
  WHILE RIGHT$(A$,1)=" " A$=LEFT$(A$)
  ENDWHILE
  IF FNuc(LEFT$(A$,5))="-QUIT" THEN
    A$=MID$(A$,7)
    WHILE LEFT$(A$,1)<>" " AND LEN A$<>0 A$=MID$(A$,2)
    ENDWHILE
    WHILE LEFT$(A$,1)=" " A$=MID$(A$,2)
    ENDWHILE
  ENDIF
  DIM A$(100)
  ARG%=0
  B%=INSTR(A$," ")
  WHILE B%<>0
    ARG%+=1
    A$(ARG%)=LEFT$(A$,B%-1)
    A$=MID$(A$,B%+1)
    WHILE LEFT$(A$,1)=" "
      A$=MID$(A$,2)
    ENDWHILE
    B%=INSTR(A$," ")
  ENDWHILE
  IF A$<>"" ARG%+=1:A$(ARG%)=A$
  invert%= FALSE
  range%= FALSE
  sharpen%= FALSE
  info%= FALSE
  cache%= FALSE
  cacheareanumber%=0
  dither%= TRUE
  black%= FALSE
  hist%= FALSE
  equal%= FALSE
  vflip%= FALSE
  hflip%= FALSE
  scale%= TRUE
  scaleo%= TRUE
  scrapf%= FALSE
  gamma=1
  rotate%= FALSE
  max%=64*1024*1024
  bright%=15
  lock%= FALSE
1514
  rwt=.299 : gwt=.587 : bwt=.114 : REM CIE Y weightings for R, G, B
1515
  IF ARG%<3 THEN
1516
    PRINT "ChangeFSI interactive input (read CmdBasUse for command line details)"
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
    INPUT "Source file: "s$,"Destination file: "f$,"Output mode: "m$
    INPUT "X Scale (eg. 2:1): "xs$
    INPUT "Y Scale (eg. 2:1): "ys$
    INPUT "Info on input picture? "a$
    IF FNuc(LEFT$(a$,1))="Y" info%= TRUE
    INPUT "Invert picture colours? "a$
    IF FNuc(LEFT$(a$,1))="Y" invert%= TRUE
    INPUT "Compute histogram of input? "a$
    IF FNuc(LEFT$(a$,1))="Y" hist%= TRUE
    INPUT "Apply histogram equalisation? "a$
    IF FNuc(LEFT$(a$,1))="Y" equal%= TRUE
    INPUT "Expand input dynamic range? "a$
    IF FNuc(LEFT$(a$,1))="Y" range%= TRUE :equal%= FALSE
    INPUT "Sharpen picture by (eg. 12 for harsh, 24 for soft): "a$
    IF a$<>"" sharpen%=VAL a$
    INPUT "Rotate? "a$
    IF FNuc(LEFT$(a$,1))="Y" rotate%=1
    scale%= FALSE
    scaleo%= FALSE
    fast%= FALSE
  ELSE
    REPEAT
      A%=ARG%
      IF FNuc(A$(ARG%))="-INFO" info%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-HIST" hist%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-LOCK" lock%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-EQUAL" equal%= TRUE :range%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-RANGE" range%= TRUE :equal%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-INVERT" invert%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-NOSCALE" scale%= FALSE :scaleo%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-NOSIZE" scale%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-NODITHER" dither%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-BRIGHTEN" bright%=16:A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-ROTATE" rotate%=1:A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-ROTATE-" rotate%=-1:A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-VFLIP" vflip%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-HFLIP" hflip%= TRUE :A$(ARG%)="":ARG%-=1
      IF FNuc(A$(ARG%))="-NOMODE" fast%= FALSE :A$(ARG%)="":ARG%-=1
      IF FNuc(LEFT$(A$(ARG%),6))="-BLACK" THEN
        black%=32
        IF LEN A$(ARG%)>6 black%=VAL MID$(A$(ARG%),7):IF black%>128 black%=128
        A$(ARG%)=""
        ARG%-=1
      ENDIF
      IF FNuc(LEFT$(A$(ARG%),6))="-GAMMA" THEN
        gamma=2.2
        IF LEN A$(ARG%)>6 gamma=VAL MID$(A$(ARG%),7)
        A$(ARG%)=""
        ARG%-=1
      ENDIF
      IF FNuc(LEFT$(A$(ARG%),8))="-SHARPEN" THEN
        sharpen%=24
        IF LEN A$(ARG%)>8 sharpen%=VAL MID$(A$(ARG%),9)
        A$(ARG%)=""
        ARG%-=1
      ENDIF
      IF FNuc(LEFT$(A$(ARG%),7))="-SMOOTH" THEN
        sharpen%=-1
        IF LEN A$(ARG%)>7 sharpen%=-VAL MID$(A$(ARG%),8)
        A$(ARG%)=""
        ARG%-=1
        IF sharpen%<-23 sharpen%=-23
      ENDIF
      IF FNuc(LEFT$(A$(ARG%),4))="-RED" rwt=VAL MID$(A$(ARG%),5):A$(ARG%)="":ARG%-=1
      IF FNuc(LEFT$(A$(ARG%),6))="-GREEN" gwt=VAL MID$(A$(ARG%),7):A$(ARG%)="":ARG%-=1
      IF FNuc(LEFT$(A$(ARG%),5))="-BLUE" bwt=VAL MID$(A$(ARG%),6):A$(ARG%)="":ARG%-=1
      IF FNuc(LEFT$(A$(ARG%),4))="-MAX" THEN
        max%=VAL MID$(A$(ARG%),5)
        IF INSTR(A$(ARG%),"K") OR INSTR(A$(ARG%),"k") max%=max%*1024
        A$(ARG%)=""
        ARG%-=1
      ENDIF
    UNTIL A%=ARG%
    s$=A$(1)
    f$=A$(2)
    m$=A$(3)
    xs$=A$(4)
    ys$=A$(5)
  ENDIF
  IF okinfo% ELSE info%= FALSE :REM if no output, then no info!
  IF workspace%<>-1 THEN
    flex%= FALSE
  ELSE
    IF worklimit%=0 THEN flex%= FALSE ELSE flex%= TRUE :nextlocation%=worklimit%
  ENDIF
  pnm$=FNuc(m$)
  CASE LEFT$(pnm$+",",3) OF
    WHEN "AIM"
      m=-1
      m$=""
    WHEN "P1,","P4,"
      m=-2
      m$=""
      spm=18
    WHEN "P2,","P5,"
      m=-2
      m$="T"
      spm=20
    WHEN "P3,","P6,"
      m=-4
      pnm$=LEFT$(pnm$,2)
      p6bits=8
      I%=VAL MID$(m$,4)
      m$=""
      IF I%>0IF I%<8 p6bits=I%
    WHEN "P15"
      m=-3
      m$=""
    WHEN "C15"
      m=-5
      m$=""
    WHEN "IRL"
      m=-4
      m$=""
      p6bits=8
    WHEN "S16"
      m=-6
      m$=""
    WHEN "S24"
      m=-8
      m$=""
      p6bits=8
      REM 24bpp is an alias for 32bpp for now
    WHEN "S32"
      m=-8
      m$=""
      p6bits=8
    WHEN "JPE"
      m$=""
      p6bits=8
      IF LEFT$(pnm$,8)="JPEGMONO" m=-9:jpegquality%=VAL MID$(pnm$,9) ELSE m=-10:jpegquality%=VAL MID$(pnm$,5)
    OTHERWISE
      m=VAL m$
      spm=m
      m$=FNuc(RIGHT$(m$,1))
      IF INSTR("CDRT",m$)=0 m$=""
  ENDCASE
  IF xs$="" OR LEFT$(xs$,1)="-" THEN
    xmul%=1
    xdiv%=1
    ymul%=1
    ydiv%=1
  ELSE
    IF ys$="" ys$=xs$
    xmul%=VAL xs$
    xdiv%=VAL MID$(xs$,INSTR(xs$,":")+1)
    ymul%=VAL ys$
    ydiv%=VAL MID$(ys$,INSTR(ys$,":")+1)
  ENDIF
  flag=-1
  SYS 8,5,s$ TO r0,,r2
  dir=r0=2
  IF dir THEN
    panic= TRUE
    SYS 8,5,s$+".raw" TO r0,,r2
    IF r0=1 s$+=".raw":dir= FALSE :panic= FALSE
    IF panic SYS 8,5,s$+".red" TO r0,,r2:IF r0=1 panic= FALSE
    IF panic ERROR 42,"Directory given"
  ENDIF
  ftype=r2>>8 AND &FFF
  bits=28
  F=2^bits
  c%=0
  cc%=0
  ccc%=0
  o%=0
  IF dir THEN
    c%= OPENIN(s$+".red")
    IF c%=0 ERROR 42,"Red file "+s$+".red not found"
    cc%= OPENIN(s$+".green")
    IF cc%=0 ERROR 42,"Green file "+s$+".green not found"
    ccc%= OPENIN(s$+".blue")
    IF ccc%=0 ERROR 42,"Blue file "+s$+".blue not found"
  ELSE
    c%= OPENINs$
    IF c%=0 ERROR 42,"File "+s$+" not found"
    leaf$=FNleaf(s$)
    pos%=INSTR(leaf$,"/")
    IF pos% THEN leaf$=LEFT$(leaf$,pos%-1)
  ENDIF
  LOCAL ERROR
  ON ERROR LOCAL RESTORE ERROR :TRACE OFF:PROCclose:ERROR ERR,REPORT$
  REM TRACE TO ":4.foo"
  REM TRACE ON
  colourindex=255
  SYS &66f4d TO ;pcd%
  CASE ftype OF
    WHEN &FF9
      flag=0
      IF INSTR("."+FNuc(s$),".HIP.") OR INSTR(FNuc(s$),":HIP.") THEN
        flag=300
        I%=INSTR(FNuc(s$),"HIP.")
        MID$(s$,I%)="LOP."
        cc%= OPENINs$
        IF cc%=0 ERROR 42,"Can't find Lop file "+s$
      ENDIF
    WHEN &FF0
      PTR#c%=0
      S$=""
      FOR I%=1 TO 4
        S$+=CHR$BGET#c%
      NEXT
      IF S$="II*"+CHR$0 flag=1500:bigendian= FALSE
      IF S$="MM"+CHR$0+"*" flag=1500:bigendian= TRUE
    WHEN &DFA
      flag=800
      IF dir flag=801
    WHEN &DE2
      flag=1000
    WHEN &CBE
      flag=3800
    WHEN &BE8
      IF (pcd% AND 1)=0 flag=3600
    WHEN &7A0
      flag=1200
    WHEN &69d
      flag=2000
    WHEN &699
      flag=700
    WHEN &698
      flag=100
    WHEN &697
      flag=1900
    WHEN &691
      flag=2600
    WHEN &690
      flag=2500
    WHEN &666
      flag=666
    WHEN &601
      flag=3000
    WHEN &371
      flag=3100
    WHEN &300
      flag=1300
      IF dir flag=1301
    WHEN 4
      flag=900
      IF dir flag=901
    WHEN 6
      flag=950
  ENDCASE
  IF flag=-1 THEN
    PTR#c%=0
    S$=""
    FOR I%=1 TO 22
      S$+=CHR$BGET#c%
    NEXT
    IF LEFT$(S$,7)="FSIfile" flag=2100
    IF flag=-1 IF LEFT$(S$,8)="SIMPLE  " flag=2800
    IF flag=-1 IF FNuc(LEFT$(S$,8))="MERCSOFT" OR FNuc(LEFT$(S$,8))="SNAPSHOT" flag=902
    IF flag=-1 IF FNuc(LEFT$(S$,8))="MERCSOF1" flag=903
    IF flag=-1 IF FNuc(LEFT$(S$,13))="IRLAM YUV 411" flag=2250
    IF flag=-1 IF LEFT$(S$,5)="Irlam" flag=2200
    IF flag=-1 IF LEFT$(S$,2)="BM" flag=2400:riffoff%=0
    IF flag=-1 IF LEFT$(S$,4)="RIFF" AND MID$(S$,9,8)="RDIBdata" AND MID$(S$,21,2)="BM" flag=2400:riffoff%=20
    IF flag=-1 IF LEFT$(S$,4)="GIF8" flag=500
    IF flag=-1 IF LEFT$(S$,5)="AV_VO" flag=600
    IF flag=-1 IF LEFT$(S$,4)=CHR$&59+CHR$&A6+CHR$&6A+CHR$&95 flag=1100
    IF flag=-1 IF LEFT$(S$,4)=CHR$0+CHR$1+CHR$0+CHR$8 flag=1400
    IF flag=-1 IF LEFT$(S$,4)=CHR$0+CHR$1+CHR$0+CHR$1 flag=2900
    IF flag=-1 IF LEFT$(S$,4)="RIX3" flag=2700
    IF flag=-1 IF LEFT$(S$,4)="II*"+CHR$0 flag=1500:bigendian= FALSE
    IF flag=-1 IF LEFT$(S$,4)="MM"+CHR$0+"*" flag=1500:bigendian= TRUE
    IF flag=-1 IF LEFT$(S$,2)=CHR$&52+CHR$&CC flag=2300
    IF flag=-1 IF LEFT$(S$,2)="P4" OR LEFT$(S$,2)="P5" OR LEFT$(S$,2)="P6" OR LEFT$(S$,2)="P8" flag=3300:info$=""
    IF flag=-1 IF LEFT$(S$,3)="P15" flag=3400
    IF flag=-1 IF LEFT$(S$,3)="P14" flag=3410
    IF flag=-1 IF LEFT$(S$,3)="PIC" flag=3500
    IF flag=-1 IF LEFT$(S$,3)="T-I" flag=3800
    IF flag=-1 IF FNuc(LEFT$(S$,3))="P13" flag=3450
    IF flag=-1 IF LEFT$(S$,4)="FORM" IF MID$(S$,9,4)="ILBM" flag=1600
    IF flag=-1 IF LEFT$(S$,8)="ALPIAR12" flag=3900:colourindex=4095
    IF flag=-1 IF LEFT$(S$,8)="ALPIAR16" flag=3905:colourindex=65535
Robert Sprowson's avatar
Robert Sprowson committed
1791
    IF flag=-1 IF LEFT$(S$,8)=CHR$&89+"PNG"+CHR$13+CHR$10+CHR$26+CHR$10 flag=4000
Robert Sprowson's avatar
Robert Sprowson committed
1792
    IF flag=-1 IF LEFT$(S$,2)=CHR$0+CHR$0 AND (ftype=&F8F OR FNuc(RIGHT$(s$,5))="/WBMP") THEN flag=4100
1793 1794 1795 1796
    IF flag=-1 THEN
      IF MID$(S$,7,4)="JFIF" OR LEFT$(S$,3)=CHR$&FF+CHR$&D8+CHR$&FF OR ftype=&C85 THEN
        REM Read the cache size variable
        jpegblk%=FNdim(20)
1797
        PROCcachesize
1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856
        A%=0
        IF cache%>=EXT#c% THEN
          REM Pre load the high speed reader
          A%=OPENIN"<ChangeFSI$Dir>.CFSIjpeg"
          IF A%<>0 THEN
            jpeg%=FNdim(EXT#A%)
            SYS 12,4,A%,jpeg%,EXT#A%
            CLOSE#A%
            REM Call the reader to get the image dimensions, this fails if it's an unsupported type
            datacache%=FNcachedim(EXT#c%)
            IF datacache%=-1 datacache%=FNdim(EXT#c%)
            PTR#c%=0
            SYS 12,4,c%,datacache%,EXT#c%
            A%=datacache%
            B%=jpegblk%
            C%=jpegblk%+4
            D%=jpegblk%+8
            E%=jpegblk%+12
            IFUSR(jpeg%+8)<>0 THENA%=0
          ENDIF
        ENDIF
        IF A%=0 THEN
          CLOSE#c%
          c%=0
          REM Not something that can be done cached with the high speed reader
          X%= OPENIN"Run:djpeg"
          IF X% THEN
            CLOSE#X%
            PROCsubtask("djpeg "+s$+" <Wimp$Scrap>")
          ELSE
            SYS "XOS_CLI","djpeg "+s$+" <Wimp$Scrap>" TO ;V
            IF V AND 1 THEN
              PROCsubtask("<ChangeFSI$Dir>.djpeg "+s$+" <Wimp$Scrap>")
            ENDIF
          ENDIF
          c%= OPENIN"<Wimp$Scrap>"
          IF c%=0 THEN =100
          IF EXT#c%=0 THEN CLOSE#c%:=100:REM When djpeg fails it can output a zero length file
          scrapf%= TRUE
          flag=3300:REM Converted to a PBM by djpeg
          info$="JFIF image converted to "
        ELSE
          REM Can use the high speed reader
          flag=3700
        ENDIF
      ENDIF
    ENDIF
    IF flag=-1 THEN
      IF LEFT$(S$,5)="btpc " THEN
        CLOSE#c%
        c%=0
        PROCsubtask("<ChangeFSI$Dir>.btpc "+s$+" <Wimp$Scrap>")
        c%= OPENIN"<Wimp$Scrap>"
        IF c%=0 THEN =100
        scrapf%= TRUE
        flag=3300
        info$="BTPC file converted to "
      ENDIF
    ENDIF
1857
    IF flag=-1 AND EXT#c%>&19 THEN
1858 1859 1860 1861 1862 1863 1864
      PTR#c%=&10
      S$=""
      FOR I%=1 TO 9
        S$+=CHR$BGET#c%
      NEXT
      IF S$="MILLIPEDE" flag=200
    ENDIF
1865
    IF flag=-1 AND EXT#c%>&49 THEN
1866 1867 1868 1869 1870 1871 1872
      PTR#c%=&41
      S$=""
      FOR I%=1 TO 8
        S$+=CHR$BGET#c%
      NEXT
      IF LEFT$(S$,4)="PNTG" flag=1800:st%=&280
    ENDIF
1873 1874
    IF flag=-1 AND EXT#c%>&CC THEN
      PTR#c%=&C4
1875 1876 1877 1878 1879 1880
      S$=""
      FOR I%=1 TO 8
        S$+=CHR$BGET#c%
      NEXT
      IF LEFT$(S$,4)="PNTG" flag=1800:st%=&2e2
    ENDIF
1881 1882
    IF flag=-1 AND EXT#c%>&212 THEN
      PTR#c%=&20A
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
      S$=""
      FOR I%=1 TO 8
        S$+=CHR$BGET#c%
      NEXT
      IF S$=CHR$0+CHR$&11+CHR$2+CHR$&ff+CHR$&c+CHR$0+CHR$&ff+CHR$&fe flag=1850
    ENDIF
    IF flag=-1 THEN
      PTR#c%=0
      IF BGET#c%=10 THEN
        C%=BGET#c%
        IF C%<6 AND C%<>1 AND BGET#c%=1 flag=1900
      ENDIF
    ENDIF
    IF flag=-1 IF INSTR("."+FNuc(s$),".PCX.") OR INSTR(FNuc(s$),":PCX.") OR RIGHT$(s$,4)="/PCX" flag=1900
    IF flag=-1 IF INSTR("."+FNuc(s$),".IMAGE.") OR INSTR(FNuc(s$),":IMAGE.") flag=400
    IF flag=-1 IF FNuc(RIGHT$(s$,4))=".RAW" flag=100
    IF flag=-1 IF FNuc(RIGHT$(s$,3))="TGA" flag=2000
    IF flag=-1 IF FNuc(RIGHT$(s$,3))="VDA" flag=2000
    IF flag=-1 IF INSTR("."+FNuc(s$),".PIC.") OR INSTR(FNuc(s$),":PIC.") flag=700
    IF flag=-1 IF INSTR("."+FNuc(s$),".IMG.") OR INSTR(FNuc(s$),":IMG.") OR RIGHT$(s$,4)="/IMG" flag=1400
    IF flag=-1 IF INSTR("."+FNuc(s$),".DSP.") OR INSTR(FNuc(s$),":DSP.") OR RIGHT$(s$,4)="/DSP" flag=1700
    IF flag=-1 IF FNuc(RIGHT$(s$,4))="/RGB" flag=3200
    IF flag=-1 AND (FNuc(RIGHT$(s$,4))="/PCD" OR ftype=&be8) AND ((pcd% AND 1)=1) THEN
      CLOSE#c%
      c%=0
      PROCsubtask("<ChangeFSI$Dir>.hpcdtoppm -3 -a "+s$+" <Wimp$Scrap> { > <Wimp$ScrapDir>.pcdtemp }")
      c%= OPENIN"<Wimp$Scrap>"
      scrapf%= TRUE
      flag=3300
      info$="PhotoCD file converted to "
    ENDIF
  ENDIF
  IF flag=-1 PROCclose:ERROR 42,"This type of file is not recognised by ChangeFSI"
  step24=1
  input=8
  ham=0
  planar%=0
  bigendianbits= FALSE
  r$="FN"+ STR$flag
  compression=0
  hpredict%=1
  striprows%=-1
  cachebytes%= FALSE
  IF xdiv%=0 AND ydiv%=0 scale%= FALSE
1927 1928
  REM Colour maps (or just r%() for monochrome), in 0-colourindex => out 0-1 fixed point
  DIM r%(colourindex),g%(colourindex),b%(colourindex)
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180
  CASE flag OF
    WHEN 0
      REM Archimedes Sprite file
      quant%=4
      PTR#c%=4
      F%=FNW+&1C
      PTR#c%=F%
      st%=FNW+F%-&20:REM Sprite
      K%=FNW+F%-&20:REM Mask
      sm=FNW
      s$=""
      IF sm<256 THEN
        SYS 53,sm,3 TO ,,I%
        CASE I% OF
          WHEN 1
            IF st%=F%+12 PROCdefpal2 ELSE PROCipal(2):s$="paletted, "
            PTR#c%=F%-16
            sx%=(FNW+1)*32
            rowbytes%=sx%>>3
            input=1
            sy%=FNW+1
            I%=FNW
            sx%-=31-FNW
          WHEN 3
            IF st%=F%+12 PROCdefpal4 ELSE PROCipal(4):s$="paletted, "
            PTR#c%=F%-16
            sx%=(FNW+1)*16
            rowbytes%=sx%>>2
            input=2
            sy%=FNW+1
            I%=FNW
            sx%-=(31-FNW) DIV 2
          WHEN 15
            IF st%=F%+12 PROCdefpal16 ELSE PROCipal(16):s$="paletted, "
            PTR#c%=F%-16
            sx%=(FNW+1)*8
            rowbytes%=sx%>>1
            input=4
            sy%=FNW+1
            I%=FNW
            sx%-=(31-FNW) DIV 4
          WHEN 63,255
            IF st%=F%+12 THEN
              PROCdefpal
            ELSE
              IF K%>st% K%=st%:REM find first of mask or sprite
              IF K%-12-F%=2048 OR I%=255 THEN
                PROCnewropal(256):s$="256 entry palette, ":REM A full size 256 entry palette
              ELSE
                PROCipal63:s$="64 entry palette, ":REM VIDC1 compatible 64 entry palette
              ENDIF
            ENDIF
            PTR#c%=F%-16
            sx%=(FNW+1)*4
            rowbytes%=sx%
            sy%=FNW+1
            I%=FNW
            sx%-=(31-FNW) DIV 8
          OTHERWISE
            ERROR 42,"Not understood RISC OS sprite"
        ENDCASE
        r$="FN8"
        SYS 53,sm,4 TO ,,nx
        SYS 53,sm,5 TO ,,ny
        nx=1<<nx
        ny=1<<ny
        IF scale% xmul%=xmul%*nx/2:ymul%=ymul%*ny/2
        info$="RISC OS sprite, "+s$+"mode "+STR$sm+", "+STR$sx%+" by "+STR$sy%+" pixels, "+FNbits(input)
      ELSE
        bpp%=sm>>>27
        CASE bpp% OF
          WHEN 1
            bpp%=1
            r$="FN8"
            IF st%=F%+12 PROCdefpal2 ELSE PROCnewropal(2):s$="paletted, "
          WHEN 2
            bpp%=2
            r$="FN8"
            IF st%=F%+12 PROCdefpal4 ELSE PROCnewropal(4):s$="paletted, "