home *** CD-ROM | disk | FTP | other *** search
/ DTP Toolbox / DTPToolbox.iso / utilities / propage_pdraw / donsgenies / adproscripts.lha / MassConversion.adpro < prev    next >
Encoding:
Text File  |  1996-03-16  |  13.2 KB  |  469 lines

  1. /* Program to use ADPro to convert a whole directory of files, including subdirectories. If a file is an anim, the first frame is converted. Images can be scaled to a uniform size. If it jams up while trying to load in a binary file which it thinks (wrongly) is a Targa or PCX image, just click on Abort. */
  2.  
  3. /* Written by Don Cox, June 94. Debug Sept 94, Oct 94. Extensions, July 95. Black & White, Sept 95. Bug fix, Oct 95, March 96. Copyright, not Public Domain. */
  4. /* $VER:MassConversion.adpro March 96 */
  5.  
  6.  
  7. /*call open("STDERR","ram:traceMC","W")
  8. trace r*/
  9.  
  10. options results
  11. numeric digits 14
  12.  
  13. /* Screen type definitions - do not alter */
  14. hires = 1
  15. interlace = 2
  16. pal = 4  /* NTSC if off */
  17. hoverscan = 8
  18. voverscan = 16
  19. vga = 32
  20. superhires = 64
  21. super72 = 128
  22. default = 256
  23.  
  24.  
  25. /* ++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++ */
  26.  
  27. /* These presets can be edited by the user */
  28.  
  29. screentype = hires + interlace + pal
  30. width = 640 /* default width & height for scaling or padding */
  31. height = 512
  32.  
  33. /* +++++++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++ */
  34.  
  35. address "ADPro"
  36. ADPRO_TO_FRONT
  37.  
  38. getdir '"Select source directory"'
  39. if rc=10 then exit
  40. directory = ADPro_Result
  41. if upper(directory) = "RAM DISK:" then directory = "Ram:"
  42.  
  43. getdir '"Select directory for saving"'
  44. if rc=10 then exit
  45. sdirectory = ADPro_Result
  46. if upper(sdirectory) = "RAM DISK:" then sdirectory = "Ram:"
  47. if right(sdirectory,1) ~=":" then sdirectory = sdirectory||"/"
  48.  
  49. doexecute = 0 /* defaults */
  50. rvalue = 16
  51. dogray = 0
  52. BlackWhite = 0
  53.  
  54. OKAYN '"Select format"' '"Convert images to ..."' '"GIF|TIFF|PCX|JPEG|IFF|Abort"'
  55. retc = rc /* queries within the select can change rc */
  56.  
  57. select
  58.     when retc = 0 then exit
  59.  
  60.     when retc = 1 then do
  61.         Format = "GIF"
  62.         rvalue = 256
  63.         doexecute = 1
  64.         saveline = IMAGE
  65.         end
  66.  
  67.     when retc = 2 then do
  68.         Format = "TIFF"
  69.         saveline = RAW
  70.         end
  71.  
  72.     when retc = 3 then do
  73.         Format = "PCX"
  74.         savetype = IMAGE
  75.         OKAYN '"Select format"' '"Image type ..."' '24-bit|Grayscale|2-color|16-color|256color|Abort"'
  76.         if rc = 0 then exit
  77.         if rc = 1 then savetype = RAW
  78.         if rc = 2 then do
  79.             IMAGE_TYPE
  80.             imagetype = ADPRO_RESULT
  81.             if pos("COLOR",imagetype) ~=0 then dogray = 1
  82.             savetype = "RAW"
  83.             end
  84.         if rc >2 then do
  85.             doexecute = 1
  86.             savetype = "IMAGE"
  87.             end
  88.         if rc = 3 then rvalue = 2
  89.         if rc = 4 then rvalue = 16
  90.         if rc = 5 then rvalue = 256 /* you can edit these options if you want something else */
  91.         saveline = savetype||" "||VGA
  92.         end
  93.  
  94.     when retc = 4 then do
  95.         Format = "JPEG"
  96.         savetype = "RAW"
  97.         getnumber '"Set quality level"' 800 1 1000
  98.         if rc= 10 then exit
  99.         quality = ADPRO_RESULT
  100.         saveline = RAW||" "||"QUALITY"||" "quality
  101.         end
  102.  
  103.     when retc = 5 then do
  104.         Format = "IFF"
  105.         savetype = "IMAGE"
  106.         OKAYN '"Select format"' '"Image type ..."' '24-bit|Grayscale|2-color|16-color|256color|Abort"'
  107.         if rc = 0 then exit
  108.         if rc = 1 then saveline = "RAW"
  109.         if rc = 2 then do
  110.             IMAGE_TYPE
  111.             imagetype = ADPRO_RESULT
  112.             if pos("COLOR",imagetype) ~=0 then dogray = 1
  113.             saveline = RAW
  114.             end
  115.         if rc >2 then do
  116.             doexecute = 1
  117.             saveline = IMAGE
  118.             end
  119.         if rc = 3 then do
  120.             rvalue = 2
  121.             okayn '"Black & White"' '"Make the colours Black & White?"' '"No Change|Black & White|White & Black|Abort"'
  122.             if rc = 0 then exit
  123.             BlackWhite = rc
  124.             end
  125.         if rc = 4 then rvalue = 16
  126.         if rc = 5 then rvalue = 256 /* you can edit these options if you want something else */
  127.         end
  128.     otherwise exit
  129.     end
  130.  
  131. newextension = ""
  132. extnprompt = "."||Format
  133. if extnprompt = ".IFF" then extnprompt = ".IFF"||rvalue
  134. getstring '"New filename extension"' extnprompt
  135. if rc=0 then newextension = ADPro_Result
  136.  
  137. okayn '"Resizing"' '"Resize the frames?"' '"No Change|Resize|Pad|Resize+Pad|Abort"'
  138. if rc = 0 then exit
  139. resize = rc
  140. paddit = 0
  141. scaleit = 0
  142.  
  143. if (resize=2|resize=4) then do
  144.     scaleit = 1
  145.     getnumber '"New image width (pixels)"' width 16 1600
  146.     if rc~=0 then do
  147.         okay1 "User aborted program"
  148.         exit
  149.         end
  150.     scaledwidth = ADPro_result
  151.  
  152.     getnumber '"New image height (pixels)"' height 16 1024
  153.     if rc~=0 then do
  154.         okay1 "User aborted program"
  155.         exit
  156.         end
  157.     scaledheight = ADPro_result
  158.     end
  159.  
  160.  
  161. if (resize=3|resize=4) then do
  162.     paddit = 1
  163.     getnumber '"Background width (pixels)"' width 16 1600
  164.     if rc~=0 then do
  165.         okay1 "User aborted program"
  166.         exit
  167.         end
  168.     backwidth = ADPro_result
  169.  
  170.     getnumber '"Background height (pixels)"' height 16 1024
  171.     if rc~=0 then do
  172.         okay1 "User aborted program"
  173.         exit
  174.         end
  175.     backheight = ADPro_result
  176.  
  177.  
  178.     getdir '"Select directory for temporary files"' '"ram:"'
  179.     if rc=10 then exit
  180.     tdirectory = ADPro_Result
  181.     if right(tdirectory,1) ~=":" then tdirectory = tdirectory||"/"
  182.  
  183.     end
  184.  
  185.  
  186. okayn '" "' '"Maximize Dynamic Range?"' '"Yes|No"' 
  187. dyrange = rc
  188.  
  189. lformat "UNIVERSAL"
  190. sformat Format
  191. pstatus "UNLOCKED"
  192. load_type "REPLACE"
  193.  
  194. lockit = 0
  195. if (doexecute = 1 & BlackWhite<2) then do
  196.     okayn '" "' '"Use first image palette for all images?"' '"Yes|No"'
  197.     lockit = rc
  198.     end
  199.  
  200. if lockit = 1 then do
  201.     okayn '" "' '"First image palette"' '"Computed|Current|Load|Abort"'
  202.     if rc = 0 then exit
  203.     if rc = 2 then PSTATUS "LOCKED"
  204.     if rc = 3 then do
  205.         PLOAD
  206.         PSTATUS "LOCKED"
  207.         end
  208.     end
  209.  
  210.  
  211. /* Convert files in one directory and its descendents */
  212.  
  213.  
  214. address command
  215. 'list >ram:dirlist "'directory'" dirs all LFORMAT="%P%S"'
  216. call open("dirinput","ram:dirlist","r")
  217.  
  218. /* First convert pics in root diectory */
  219. 'delete ram:filelist1'
  220. 'delete ram:filelist'
  221. 'list >ram:filelist1 "'directory'" files LFORMAT="%P%S"'
  222. call open(input,"ram:filelist1","r") /* test in case it's empty */
  223. listtest = readln(input)
  224. call close input
  225.  
  226. if listtest~="" then do
  227.     'sort from ram:filelist1 to ram:filelist'
  228.     address "ADPro"
  229.     call dirconvert
  230.     end
  231.  
  232.  
  233. /* Now do all the subdirectories */
  234. do d = 1 to 700
  235.     dirname = readln("dirinput")
  236.     if dirname = "" then break
  237.     address command
  238.     'delete ram:filelist1'
  239.     'delete ram:filelist'
  240.     'list >ram:filelist1 'dirname' files LFORMAT="%P%S"'
  241.     call open(input,"ram:filelist1","r") /* test in case it's empty */
  242.     listtest = readln(input)
  243.     call close input
  244.  
  245.     if listtest~="" then do
  246.         'sort from ram:filelist1 to ram:filelist'
  247.         address "ADPro"
  248.         call dirconvert
  249.         end
  250.     end
  251.  
  252. address "ADPro"
  253. ADPRO_TO_FRONT
  254. OKAY1 "Finished..."
  255. exit
  256. end
  257.  
  258. /* ++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++++++ */
  259.  
  260.  
  261. getpath:
  262. parse arg allname
  263. pos1 = lastpos("/",allname)
  264. if pos1 = 0 then pos1 = lastpos(":",allname)
  265. filepath = left(allname,pos1)
  266. return filepath
  267.  
  268. getname:
  269. parse arg allname
  270. pos1 = lastpos("/",allname)
  271. if pos1 = 0 then pos1 = lastpos(":",allname)
  272. justname = substr(allname,pos1+1)
  273. return justname 
  274.  
  275. /* +++++++++++++++++++++++++++++ +++++++++++++++++++++++++++++++ */
  276.  
  277.  
  278. /* Convert all the pics in a directory */
  279. dirconvert:
  280. address "ADPro"
  281.  
  282. call open(input,"ram:filelist","r")
  283. do i = 1 to 7000   /* safety limit of 7000 files  */
  284.     filename = readln(input)
  285.     if filename = "" then break
  286.     
  287.     slashpos = lastpos("/",filename)
  288.     dotpos = lastpos(".",filename)
  289.     if dotpos<slashpos then dotpos = 0 /* not dots in directory names */
  290.     if dotpos~=0 then extension = upper(substr(filename,dotpos))
  291.     else extension = ""
  292.     
  293.     if extension = ".INFO" then iterate
  294.     if extension = ".PSFONT" then iterate
  295.     if extension = ".FONT" then iterate
  296.     if extension = ".PDFONT" then iterate
  297.     if extension = ".PFB" then iterate
  298.     if extension = ".AFM" then iterate
  299.     if extension = ".METRIC" then iterate
  300.     if extension = ".LIB" then iterate
  301.     if extension = ".DEM" then iterate
  302.     if extension = ".DOC" then iterate
  303.     if extension = ".ZIP" then iterate
  304.     if extension = ".LHA" then iterate
  305.     if extension = ".ARC" then iterate
  306.     if extension = ".ME" then iterate
  307.     if extension = ".DAT" then iterate
  308.     if extension = ".TF" then iterate
  309.     if extension = ".DB" then iterate
  310.     if extension = ".C" then iterate    
  311.     if extension = ".H" then iterate
  312.     if extension = ".GF" then iterate
  313.     if extension = ".PPRX" then iterate
  314.     if extension = ".PDRX" then iterate
  315.     if extension = ".REXX" then iterate
  316.     if extension = ".ADPRO" then iterate
  317.     if word(filename,2) = "files" then iterate /* This line not a file name */
  318.     if word(filename,1) = "TOTAL:" then break  /* or this one */
  319.  
  320.     load_type "REPLACE"
  321.     lformat "UNIVERSAL"
  322.  
  323.     call open("picinput",filename,"R")
  324.     chunks = readch("picinput",1000)
  325.     call close("picinput")
  326.  
  327.     if substr(chunks,9,4) = "ANIM" then do
  328.         lformat "ANIM"
  329.         load '"'filename'"' FRAME 1
  330.         end
  331.     else load '"'filename'"' /* Quotes allow file names with spaces */
  332.     if rc ~=0 then iterate /* if load fails, try the next one */
  333.  
  334.     pos1 = lastpos("/",filename)
  335.     if pos1 = 0 then pos1 = lastpos(":",filename)
  336.  
  337.     imagetype = 24bit /* if in doubt */
  338.     IMAGE_TYPE
  339.     itype = ADPRO_RESULT
  340.     if left(itype,8)="BITPLANE" then imagetype = bitplane
  341.     else if left(itype,5) = "COLOR" then imagetype = 24bit
  342.     else if left(itype,4) = "GRAY" then imagetype = gray
  343.  
  344.     if imagetype = bitplane then iterate /* can't scale if no 24-bit or 8-bit data  - i.e. picture too big for memory */
  345.  
  346. /* Get size of loaded image, before scaling */
  347. xsize
  348. inputwidth = ADPro_result
  349. ysize
  350. inputheight = ADPro_result
  351.  
  352.  
  353. if scaleit = 1 then do
  354.  
  355.     /* Adjusting proportions */
  356.         CONTRAST "-1" /* Prevents ADPro's scaling artefacts, seen on 0 or 255 colours */
  357.         OPERATOR "APPLY_MAP"
  358.         prop = 1 /* for starters */
  359.  
  360.     /* If it's an IFF file, the CAMG chunk should tell us its proportions */
  361.         CAMG = "null" /* just a dummy string */
  362.         binCAMG = "null"
  363.         filetype = left(chunks,4)
  364.         if filetype = "FORM" then do
  365.             CAMG = substr(chunks,pos("CAMG",chunks)+10,2)
  366.             if pos("CAMG",chunks)=0 then break
  367.             binCAMG = c2b(CAMG)
  368.             binCAMG = overlay("000000000000",binCAMG,2) /* don't want these bits */
  369.  
  370.             picformat = "null"
  371.     /* These comparisons only just work, with numeric digits at 14, as the last 2 digits don't matter */
  372.             select
  373.                 when binCAMG = "0000000000000000" then do
  374.                     picformat = "lores"
  375.                     prop=1
  376.                     end
  377.                 when binCAMG = "1000000000000000" then do
  378.                     picformat = "medres"
  379.                     prop=0.5
  380.                     end
  381.                 when binCAMG = "0000000000000100" then do
  382.                     picformat = "interlace"
  383.                     prop=2
  384.                     end
  385.                 when binCAMG = "1000000000000100" then do
  386.                     picformat = "hires"
  387.                     prop=1
  388.                     end
  389.                 otherwise do
  390.                     picformat = "unknown"
  391.                     prop=1
  392.                     end
  393.                 end
  394.  
  395.             end
  396.  
  397.  
  398.         imageheight = scaledheight
  399.         scalewidth.tn = scaledwidth
  400.         proportion = (inputheight)/(inputwidth*prop)
  401.         scaleheight.tn = trunc(scalewidth.tn * proportion ) /* keep proportions */
  402.         if scaleheight.tn >imageheight then do  /* vertical pics */
  403.             proportion = inputwidth*prop/inputheight
  404.             scalewidth.tn = trunc(imageheight* proportion )
  405.             scaleheight.tn = imageheight
  406.             end
  407.         ABS_SCALE scalewidth.tn scaleheight.tn
  408.         end   /* of scaling */
  409.  
  410.     if scaleit = 0 then do
  411.         scalewidth.tn = inputwidth
  412.         scaleheight.tn = inputheight
  413.         end
  414.  
  415.     if dyrange = 1 then OPERATOR "DYNAMIC_RANGE" 0 255
  416.  
  417.     if paddit = 1 then do
  418.         savename = tdirectory||"tpic"
  419.         sformat "IFF"
  420.         SAVE savename "RAW"
  421.         sformat Format
  422.         lformat "BACKDROP"
  423.         LOAD "XXX" backwidth backheight "COLOR"
  424.         lformat "IFF"
  425.         LOAD_TYPE "COMPOSE"
  426.         loadname = tdirectory||"tpic"
  427.  
  428.         offsetX = (backwidth-scalewidth.tn)%2 /* integer division - no half pixels */
  429.         offsetY = (backheight-scaleheight.tn)%2
  430.  
  431.         LOAD loadname offsetX offsetY
  432.         lformat "UNIVERSAL"
  433.         LOAD_TYPE "REPLACE"
  434.         end
  435.  
  436.         if dogray = 1 then OPERATOR "COLOR_TO_GRAY"
  437.  
  438.         if doexecute = 1 then do
  439.             render_type rvalue
  440.             execute
  441.             if BlackWhite>1 then do
  442.                 ppoke 0 0 0 0
  443.                 ppoke 1 255 255 255
  444.                 end
  445.             end
  446.         if lockit ~= 0 then pstatus "LOCKED"
  447.  
  448.         justname = getname(filename)
  449.         justname2 = justname
  450.         dotpos2 = lastpos(".",justname)
  451.         if dotpos2~=0 then do
  452.             justname2 = left(justname,dotpos2-1)
  453.             extension2 = substr(filename,dotpos+1)
  454.             if datatype(extension2, n) then justname2 = justname /* don't chop off frame numbers */
  455.             end
  456.         filename2 = sdirectory||justname2||newextension
  457.         SAVER Format '"'filename2'"' saveline
  458.         why = ""
  459.         if rc~=0 then why = ADPRO_RESULT
  460.  
  461.     end    /* end of directory */
  462.  
  463. call close(input)
  464.  
  465. ADPRO_UNDISPLAY
  466.  
  467.  
  468. return
  469.