home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / webslice.zip / WebSlice.cmd < prev   
OS/2 REXX Batch file  |  2001-10-07  |  11KB  |  270 lines

  1. /* WebSlice.cmd v0.70                                                  */
  2. /* Copyright (c) Martin R. Hadam 2001                                  */
  3. /* REXX Program to slice larg images into tiles for web presentation   */
  4. /*    'slices' (tiles) are sized 200x150 pixels by default             */
  5. /*                                                                     */
  6. /* requires Impos/2 REXX; uses undocumented functions:                 */
  7. /*  ImgSelectAll(numImage)                                             */
  8. /*  ImgSelectCircle(numImage,num?,numXPos,numYPos,numXRad,numYRad)     */
  9. /*  ImgSelectRectangle(numImage,num?,numXPos,numYPos,numXSize,numYSize)*/
  10. /*  ImgClosePgm()                                                      */
  11. /* WARNING: use of such functions REQUIRES Impos/2 v2.10               */
  12. /* place CMD file code into impos directory (other locations untested) */
  13. /* logfile will be created in the webslice.cmd (Impos) directory       */
  14. /* image files may be anywhere; image slices (.gif) and html will be   */
  15. /*   created in in the same (image) directory                          */
  16. /* HIGH color images will be converted to 256 colors and saved as GIF  */
  17. /*       version history                                     */
  18. /* v0.10 first code sample                                   */
  19. /* v0.20 now tackling converted file                         */
  20. /* v0.30 add looping through image                           */
  21. /* v0.40 generate html table (in basic page) with same name  */
  22. /* v0.50 first fully working version                         */
  23. /* v0.51 fixed bug in loop termination; now use 'until'      */
  24. /* v0.52 change coding for loop end points                   */
  25. /* v0.53 make relative path for logfile                      */
  26. /* v0.55 added Impos/2 version checking                      */
  27. /* v0.56 load RexxUtil if not already loaded                 */
  28. /* v0.60 allow tile dimensions as optional parameter         */
  29. /*       uncomment ImgShowImage() & add ImgUndo()            */
  30. /*       added colored error messages                        */
  31. /*       add width / height checking controls                */
  32. /* v0.70 fixed bug in calculating y coordinates              */
  33. /*       announce number oftiles being generated             */
  34. SIGNAL ON syntax
  35. SIGNAL ON novalue
  36. CALL ON halt
  37. CALL ON error
  38. PARSE ARG incoming width height
  39. if strip(incoming,,)="" then call errfile
  40. if stream(incoming,"C","Query exists")="" then call errfile
  41. baseimg=strip(incoming,,)
  42. if width||height\="" & (datatype(width,"W")\=1 | datatype(height,"W")\=1) then call errfile
  43.  
  44. if width>0 | height>0 then do
  45.     /* custom size for tiles                                 */
  46.     if width=0 | height=0 then call errfile
  47.     slice_width=strip(width,,)
  48.     slice_height=strip(height,,)
  49.     end /* do */
  50. else do 
  51.     /* standard size for tiles                               */
  52.     slice_width=200
  53.     slice_height=150
  54.     end /* do */
  55.  
  56. /* load REXXUTIL if needed                                   */
  57. if RxFuncQuery('sysloadfuncs')=1 then do
  58.     call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  59.     call SysLoadFuncs
  60.     end
  61.  
  62. /* find path of incoming file                                */
  63. drive=filespec('D',baseimg)
  64. path=filespec('P',baseimg)
  65. filename=filespec('N',baseimg)
  66. basename=substr(filename,1,lastpos(".",filename)-1)
  67. newbase=drive||path||basename
  68. if drive="" then call errfile
  69.  
  70. logfile=".\WEB_SLICE.LOG"
  71. rc=lineout(logfile)
  72. rc=lineout(logfile,"==================================================")
  73. rc=lineout(logfile,"Start Logging WebSlice")
  74. rc=lineout(logfile,date("L")||"   "||time())
  75.  
  76. /* check for correct version of Impos/2                      */
  77. impos=SysSearchPath('PATH','IMPOS.EXE')
  78. if impos="" then do
  79.     say; say "Impos/2 not found. Aborting CMD file"
  80.     call lineout logfile,"Impos/2 not found. Aborting CMD file"
  81.     call final
  82.     end /* do */
  83. if stream(impos,"C","Query size")\="824990" then do
  84.     say; say '1B'x||'[33;40m'||'1B'x||'[1;m'||'This CMD file requires Impos/2 v2.10'||'1B'x||'[37;40m'||'1B'x||'[1;m'
  85.     say '1B'x||'[33;40m'||'1B'x||'[1;m'||'Please update your software before using it'||'1B'x||'[37;40m'||'1B'x||'[1;m'
  86.     call lineout logfile,"This CMD file requires Impos/2 v2.10"
  87.     call lineout logfile,"Execution aborted due to wrong Impos/2 version"
  88.     call lineout logfile,"Please update your software"
  89.     call final
  90.     end /* do */
  91.  
  92. /* load Impos REXX functions & Impos/2                       */
  93. call RxFuncAdd 'ImgInit', 'ImpRexx', 'ImgInitiate'
  94. call ImgInit
  95.  
  96. /* say DlgImageInfo(curr_image)  */
  97. call lineout logfile,"processing" baseimg
  98.  
  99. curr_image=ImgLoadImage(baseimg,TRUE)
  100. if curr_image == 0 then do
  101.     say "Image could not be read"
  102.     say "you need to enter the full path"
  103.     call final
  104.     end
  105. else do
  106.     say; say "Image Info"
  107.     say
  108.     say "Width:           " ImgQueryImageInfo(curr_image, 1);
  109.     say "Height:          " ImgQueryImageInfo(curr_image, 2);
  110.     say "Horz. Resolution:" ImgDpiFromDpm(ImgQueryImageInfo(curr_image, 3));
  111.     say "Vert. Resolution:" ImgDpiFromDpm(ImgQueryImageInfo(curr_image, 4));
  112.     say "Colors:          " ImgQueryImageInfo(curr_image, 5);
  113.     say "Image Type:      " ImgQueryImageInfo(curr_image, 6);
  114.     say "Bits per Pixel:  " ImgQueryImageInfo(curr_image, 7);
  115.     say
  116.     end
  117.  
  118. /* reduce colors to 256 before slicing; save as GIF          */
  119. if ImgQueryImageInfo(curr_image, 5)>256 then do
  120.     parms=DlgConvert8BitColor(0,1,2,256)
  121.     call lineout logfile,"color conversion required: parms = "parms
  122.     if parms\="" then interpret "rc=ImgConvert8BitColor("parms")"
  123.     /* rc=ImgShowImage(curr_image) */
  124.     newimage=newbase||"."
  125.     /* call lineout logfile,"new file name" newimage */
  126.     parms=DlgSaveImage(0,newimage,"GIF","LZW",TRUE)
  127.     call lineout logfile,"save converted file: "parms
  128.     if parms\="" then interpret "rc=ImgSaveImage("parms")"
  129.     end /* do */
  130. else say "no color conversion required"
  131.  
  132. /* generate slices from top left to down right               */
  133. /* recalc image coordinates from os/2 style:                 */
  134. /* lower left to upper right                                 */
  135. /* impos pixel numbering starts at 0                         */
  136. img_width=ImgQueryImageInfo(curr_image, 1)
  137. img_height=ImgQueryImageInfo(curr_image, 2)
  138. /* calculate resulting number of slices                      */
  139. if img_width // slice_width=0 then xrounds=img_width % slice_width
  140.     else xrounds=img_width % slice_width +1
  141. if img_height // slice_height=0 then yrounds=img_height % slice_height
  142.     else yrounds=img_height % slice_height +1
  143. /* announce number of slices being generated                 */
  144. say; say "slicing will produce" xrounds*yrounds "tiles sized" slice_width "x" slice_height "pixels"
  145. rc=lineout(logfile,"slicing will produce" xrounds*yrounds "tiles sized" slice_width "x" slice_height "pixels")
  146.  
  147. /* create basic html page with table containg slices         */
  148. if stream(newbase||"_table.htm","C","Query exists")\="" then call SysFileDelete(newbase||"_table.htm")
  149. html=newbase||"_table.htm"
  150. rc=lineout(html,"<HTML><HEAD><TITLE>Sliced Image" filename "in a Table</TITLE></HEAD><BODY BGCOLOR=WHITE>")
  151. rc=lineout(html,"<CENTER><TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH="img_width">")
  152.  
  153. /* coordinates of top left image corner                      */
  154. xstart=0
  155. ystart=img_height-1
  156.  
  157. /* i is horizontal move   j is vertical move                 */
  158. /* slice numbering is appended as "_ij.gif"                  */
  159. do j=1 to yrounds
  160.     call lineout html,"<TR>"    
  161.     /* calculate y coordinates                               */    
  162.     ystart=img_height-(j*slice_height)
  163.  
  164.     do i=1 to xrounds
  165.         /* calculate x coordinates                           */
  166.         xstart=(i-1)*slice_width
  167.         call lineout logfile,"coordinates for slice" j||i "are" xstart ystart
  168.  
  169.         /* show image for control only; leaves open windows  */
  170.         /* rc=ImgShowImage(curr_image) */
  171.         rc=ImgSelectNone()
  172.         rc=ImgSelectRectangle(curr_image,2,xstart,ystart,slice_width,slice_height)
  173.         if rc\=0  then call lineout logfile,"select rectangle error code" rc
  174.  
  175.         rc=ImgCopyImage()
  176.         pasted=ImgImageFromClipboard()
  177.         if pasted==0 then do 
  178.             say "image could not be generated"
  179.             call lineout logfile,"image tile" j i "could not be generated"
  180.             end 
  181.  
  182.         /*
  183.         /* interactive save for slices                       */
  184.         parms=DlgSaveImage(pasted,newbase||"_"||ij,"GIF","LZW",TRUE)
  185.         call lineout logfile,"save slice #"j||i parms
  186.         if parms\="" then interpret "rc=ImgSaveImage("parms")"
  187.         */
  188.  
  189.         /* automated save for slices                         */    
  190.         rc=ImgSaveImage(pasted,newbase||"_"||j||i||".GIF","GIF","LZW",TRUE)
  191.         if rc\=0  then call lineout logfile,"autosave slice #"j||i rc
  192.  
  193.         /* write image-containing html tablecell             */
  194.         call lineout html,'<TD><IMG SRC="./'||basename||"_"||j||i||'.GIF" WIDTH='ImgQueryImageInfo(pasted,1) 'HEIGHT='ImgQueryImageInfo(pasted,2)'></TD>'
  195.     
  196.         /* close used image slices & undo                    */
  197.         rc=ImgCloseImage(pasted)
  198.         if rc\=0  then call lineout logfile,"image close rc" rc
  199.         rc=ImgUndo()
  200.         if rc\=0  then call lineout logfile,"image undo rc" rc
  201.  
  202.     end /* do */
  203.     call lineout html,"</TR>"    
  204. end /* do */
  205.  
  206. rc=lineout(html,"</TABLE></CENTER>")
  207. rc=lineout(html,"</BODY></HTML>")
  208. rc=stream(html,"C","Close")
  209.  
  210. rc=ImgCloseAllImages()
  211. /* call lineout logfile,"close all images" rc                */
  212. /* ClosePgm doesn't seem to have any effect ?                */
  213. rc=ImgClosePgm()
  214.  
  215.  
  216. FINAL:
  217. /* Schlussroutinen                                   */
  218. rc=lineout(logfile,"WebSlice ended")
  219. rc=stream(logfile,"C","close")
  220. say " "
  221. say "WebSlice ended"
  222. exit
  223.  
  224.  
  225.  
  226. ERRFILE:
  227. /* Error Routine                                         */
  228. say " "
  229. say "Please enter image file name including FULL path:"
  230. say '1B'x||'[33;40m'||'1B'x||'[1;m'||'WebSlice X:\path\imagefile.typ'||'1B'x||'[37;40m'||'1B'x||'[1;m'
  231. say ""
  232. say "Optionally add tile dimensions in pixels:"
  233. say '1B'x||'[33;40m'||'1B'x||'[1;m'||'WebSlice X:\path\imagefile.typ' '1B'x||'[36;40m'||'1B'x||'[1;m'||'slice_width slice_height'||'1B'x||'[37;40m'||'1B'x||'[1;m'
  234. say ""
  235. say "Default tile size is 200 x 150 pixel"
  236. say ""
  237. say; say "Program ended with error"
  238. exit
  239.  
  240. /* Error Routinen nach Wolek p114                        */
  241. SYNTAX:
  242. say "Syntax- oder Laufzeitfehler in Zeile " sigl
  243. say "Fehlercode: " rc
  244. say "Fehlertext: " errortext(rc)
  245. say ""
  246. say "Text der Quellzeile :  >>> " sourceline(sigl) "<<<"
  247. exit
  248.  
  249. NOVALUE:
  250. say "Die Variable " condition("D") " in Zeile " sigl " ist nicht initialisiert"
  251. say "Text der Quellzeile :  >>> " sourceline(sigl) "<<<"
  252. exit
  253.  
  254. HALT:
  255. say "soll die Prozedur beendet werden (j/n)? "
  256. pull antw
  257. if antw="j" then
  258.     exit
  259. else
  260.     return
  261.  
  262. ERROR:
  263. say "Unbekannter Befehl oder Argumentfehler in Zeile "sigl||":"
  264. say "Komponente:" condition("D")
  265. say ""
  266. say "Text der Quellzeile :  >>> " sourceline(sigl) "<<<"
  267. exit
  268. /* Ende Error-Routinen nach Wolek                        */
  269.  
  270.