home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u191.dms / in.adf / SupportFiles / Programming / iffread2.a < prev    next >
Encoding:
Text File  |  1978-02-03  |  10.1 KB  |  413 lines

  1.  
  2.       INCLUDE "EXEC/TYPES.I"
  3.       INCLUDE "GRAPHICS/GFXBASE.I"
  4.       INCLUDE "GRAPHICS/GFX.I"
  5.       INCLUDE "GRAPHICS/VIEW.I"
  6.       INCLUDE "GRAPHICS/RASTPORT.I"
  7.  
  8.  
  9.     XREF     _AbsExecBase             ;library references
  10.     XREF     _LVOExit
  11.     XREF     _LVOFreeMem
  12.     XREF     _LVOAllocMem
  13.     XREF     _LVODelay
  14.     XREF     _LVOOpen
  15.     XREF     _LVORead
  16.     XREF     _LVOClose
  17.     
  18.     XREF     dosbase                  ;first defined in pix.a                       
  19.        
  20.     XDEF     readfile                 ;called by pix.a
  21.   
  22.  CSECT text,code
  23.  
  24. readfile:                  ;reads iff picture file
  25.     move.l  d0,filename
  26.     move.l  d1,BMscene
  27.     move.l  d2,colourlist
  28.     jsr        openfile       
  29.     tst.l   d0             ;exits if file is invalid
  30.     bne     finished
  31. more:
  32.     move.l    bytesread,d0   ;reads all chunks
  33.     cmp.l    totalbytes,d0
  34.     bge        closefile
  35.     jsr        readchunk
  36.     bra     more
  37. closefile:
  38.     move.l fHandle,d1
  39.     move.l dosbase,a6
  40.     jsr    _LVOClose(a6)
  41. finished:
  42.     rts
  43.  
  44. wait:                      ;Delay routine used in debugging
  45.     move.l  dosbase,a6
  46.     move.l  #5,d1
  47.     jsr     _LVODelay(a6)
  48.     rts
  49.  
  50. openfile:                    ;opens file
  51.     move.l    #1005,d2
  52.     move.l    filename,d1
  53.     move.l  dosbase,a6
  54.     jsr        _LVOOpen(a6)
  55.     movem.l    d0,fHandle
  56.     tst.l    fHandle
  57.     beq     finished
  58.     move.l    #8,d3           ;reads first 8 bytes
  59.     move.l    #mybuf,d2
  60.     move.l    fHandle,d1
  61.     jsr        _LVORead(a6)
  62.     movem.l    d0,rLen
  63.     move.l    d0,bytesread     ;stores file length
  64.     move.l  filelength,d0
  65.     move.l  d0,totalbytes
  66.     move.l    #4,d3
  67.     move.l    #mybuf,d2
  68.     move.l    fHandle,d1
  69.     move.l  dosbase,a6
  70.     jsr        _LVORead(a6)
  71.     add.l    d0,bytesread
  72.     move.l    #mybuf,a0
  73.     move.l    #ilbm,a1          ;checks for ILBM or ACBM file
  74.     jsr        strcmp
  75.     tst.l    d0
  76.     bne        acbmtype
  77.     rts
  78. acbmtype:
  79.     move.l    #mybuf,a0
  80.     move.l    #acbm,a1
  81.     jsr    strcmp
  82.     rts
  83.  
  84. readchunk:                     ;reads chunk header
  85.     move.l    #8,d3
  86.     move.l    #mybuf,d2
  87.     move.l    fHandle,d1
  88.     move.l  dosbase,a6
  89.     jsr        _LVORead(a6)        ;adds length to total
  90.     move.l  mybuf+4,d1
  91.     move.l    d1,icLen
  92.     add.l    #8,bytesread
  93.     add.l    d1,bytesread
  94.     move.l    #mybuf,a1           ;is it a BMHD chunk?
  95.     move.l    #bmhd,a0
  96.     jsr        strcmp
  97.     tst.l    d0
  98.     bne        notbmhd
  99.     jsr        readbitmapheader
  100.     rts
  101. notbmhd:                        ;is it a CMAP chunk?
  102.     move.l    #mybuf,a1
  103.     move.l    #cmap,a0
  104.     jsr        strcmp
  105.     tst.l    d0
  106.     bne        notcmap
  107.     jsr        readcmap
  108.     rts
  109. notcmap:                          ;is it a BODY chunk?
  110.     move.l    #mybuf,a1
  111.     move.l    #body,a0
  112.     jsr        strcmp
  113.     tst.l    d0
  114.     bne        notbody
  115.     jsr        readbody
  116.     rts
  117. notbody:                          ;is it an ABIT chunk?
  118.     move.l    #mybuf,a1
  119.     move.l    #abit,a0
  120.     jsr        strcmp
  121.     tst.l    d0
  122.     bne        notabit
  123.     jsr        readabit
  124.     clr.l    d0
  125.     rts                
  126. notabit:                          ;none of these,read and ignore.
  127.     move.l    icLen,d3              ;extra chunk types could be 
  128.     move.l    #mybuf,d2             ;processed here
  129.     move.l    fHandle,d1
  130.     move.l  dosbase,a6
  131.     jsr        _LVORead(a6)
  132.     rts
  133.  
  134. readbitmapheader:                ;process BMHD info
  135.     move.l    icLen,d3
  136.     move.l    #mybuf,d2
  137.     move.l    fHandle,d1
  138.     move.l  dosbase,a6
  139.     jsr    _LVORead(a6)             ;read rest of chunk
  140.     clr.l    d0
  141.     move.w    mybuf,d1
  142.     move.w    d1,scrWidth          ;store screen width in pixels
  143.     clr.l    d0
  144.     move.w    mybuf+2,d1
  145.     move.w    d1,scrHeight         ;store screen height
  146.     clr.w    d0
  147.     move.b    mybuf+8,d0
  148.     move.w    d0,iDepth            ;store number of planes
  149.     move.b    mybuf+10,iCompr      ;store compression flag
  150.     clr.l    d0
  151.     move.w    mybuf+16,d1
  152.     move.w    d1,iWidth            ;store picture width in pixels
  153.     clr.l    d0
  154.     move.w    mybuf+18,d1          ;store picture height
  155.     move.w    d1,iHeight
  156.     move.w    iWidth,d0 
  157.     ext.l    d0
  158.     asr.l    #3,d0
  159.     move.w    d0,iRowBytes         ;calculate bytes per row for picture
  160.     move.w    scrWidth,d0
  161.     ext.l    d0
  162.     asr.l    #3,d0
  163.     move.w    d0,scrRowBytes       ;calculate bytes per row for screen
  164.     move.w    iDepth,d0            ;calculate number of colours
  165.     ext.l    d0
  166.     move.w  #1,d1
  167.     asl.w   d0,d1
  168.     move.w  d1,nColors
  169.     rts
  170.  
  171. readcmap:                        ;CMAP info into colourlist
  172.     move.l    icLen,d3
  173.     move.l    #mybuf,d2 
  174.     move.l  fHandle,d1
  175.     move.l  dosbase,a6
  176.     jsr        _LVORead(a6)          ;read data
  177.     move.w    nColors,d4
  178.     andi.l    #255,d4
  179.     sub.l   #1,d4
  180.     move.l   colourlist,a1
  181.     move.l   #mybuf,a0
  182. nextcolour:                       ;for each colour
  183.     move.b    (a0)+,d0              ;first byte -red componant R0
  184.     ext.w    d0
  185.     andi.w   #255,d0
  186.     move.w    d0,red
  187.     move.b    (a0)+,d0              ;second byte -green componant G0
  188.     ext.w    d0
  189.     andi.w   #255,d0             
  190.     move.w  d0,gre
  191.     move.b    (a0)+,d0              ;third byte -blue componant B0
  192.     ext.w    d0
  193.     andi.w    #255,d0
  194.     move.w    d0,blu
  195.     asr.w    #4,d0                 ;B0 / 16 to give 000B
  196.     move.w    gre,d1                ;G0 to 00G0
  197.     add.w    d1,d0                 ;add green to blue 00GB
  198.     move.w    red,d1                ;red to 00R0
  199.     asl.w    #4,d1                 ;00R0 x 16 to give 0R00
  200.     add.w    d1,d0                 ;add red to blue/green 0RGB
  201.     move.w    d0,(a1)+              ;store result in colourlist
  202.     dbra    d4,nextcolour
  203.     rts
  204.  
  205. readabit:                         ;read picture data from ACBM file
  206.     move.w    iDepth,d6
  207.     ext.l    d6
  208.     clr.l   d5
  209.     move.l  BMscene,a0
  210.     lea     bm_Planes(a0),a3      
  211. nextplane:                       ;for each bitplane
  212.     cmp.l    d6,d5
  213.     bge        finished
  214.     move.w    iHeight,d1
  215.     move.w    scrRowBytes,d3        ;calculate number of bytes
  216.     mulu    d1,d3
  217.     move.l    (a3)+,d2
  218.     move.l    fHandle,d1
  219.     move.l  dosbase,a6
  220.     jsr        _LVORead(a6)           ;read the data
  221.     add.l    #1,d5
  222.     bra        nextplane
  223.  
  224. readbody:                          ;read body of ILBM file
  225.     move.l    #65539,d1
  226.     move.l    icLen,d0
  227.     move.l  _AbsExecBase,a6
  228.     jsr    _LVOAllocMem(a6)           ;allocate buffer for all picture data
  229.     movem.l    d0,tempbuffer
  230.     move.l    icLen,d3
  231.     move.l    tempbuffer,d2
  232.     move.l    fHandle,d1
  233.     move.l  dosbase,a6
  234.     jsr    _LVORead(a6)                ;read all data
  235.     move.b    iCompr,d0               ;is data compressed?
  236.     tst.l    d0
  237.     bne       fibcomp                  
  238.     jsr       plainread                ;read interleaved planes
  239.     bra     freemem
  240. fibcomp:
  241.     jsr        decode                  ;decode interleaved and compressed data 
  242. freemem:
  243.     move.l    icLen,d0                ;free the data buffer
  244.     move.l    tempbuffer,a1
  245.     move.l  _AbsExecBase,a6
  246.     jsr        _LVOFreeMem(a6)
  247.     rts
  248.  
  249. decode:
  250.     move.l    tempbuffer,a3           ;beginning of data buffer
  251.     clr.w   d5                      ;rows
  252. nextrow3:                           ;for each row
  253.     move.l   BMscene,a0             ;address of start of pbitplane
  254.     lea     bm_Planes(a0),a5
  255.     cmp.w    iHeight,d5              ;check for last row
  256.     bge        finished
  257.     clr.w   d6                      ;planes
  258. nextplane3:                         ;for each plane
  259.     move.l  (a5),a2
  260.     cmp.w    iDepth,d6
  261.     bge         endrow3                ;check for last plane
  262.     move.w    scrRowBytes,d4          ;calculate offset from row start
  263.     mulu    d5,d4
  264.     add.l   d4,a2
  265.     move.w    iRowBytes,d0            ;store bytes per row
  266.     ext.l    d0
  267.     clr.l   d7                      ;byte count 
  268. nextcol3:                           ;for each column
  269.     cmp.l   d0,d7                   ;check for end of plane
  270.     bge     endplane3
  271.     move.b  (a3)+,d3                ;read next byte
  272.     move.b   d3,d2                  ;store its value 'x'
  273.     and.l    #255,d2
  274.     move.l   d2,d4
  275.     btst     #7,d2                  ;is x more than 128?
  276.     bne      code128
  277. nextbytesdiff:                      ;the next x bytes differ from each other
  278.     move.b    (a3)+,(a2)+           ;read next x bytes on to bit plane
  279.     dbra      d4,nextbytesdiff
  280.     add.l     #1,d7
  281.     add.l     d2,d7                 ;update byte count
  282.     bra       nextcol3              ;process next column
  283. code128:                            ;the next x bytes are the same 
  284.     move.l     #256,d4
  285.     sub.l     d2,d4 
  286.     move.b   (a3)+,d1               ;read value of next byte 'y'
  287. nextbytesame:
  288.     move.b    d1,(a2)+              ;copy 'y' to next x bytes on bitplane
  289.     dbra      d4,nextbytesame
  290.     add.l     #257,d7
  291.     sub.l     d2,d7                 ;update byte count
  292.     bra       nextcol3              ;process next column
  293. endplane3:
  294.     add.l    #4,a5                  ;address of start of next plane
  295.     add.w    #1,d6                   ;add to plane count
  296.     bra        nextplane3
  297. endrow3:
  298.     add.w    #1,d5                   ;add to row count
  299.     bra        nextrow3
  300.     rts
  301.  
  302. plainread:                          ;read uncompressed picture data
  303.     move.l    tempbuffer,a3           ;beginning of data buffer
  304.     clr.w   d5                      ;rows
  305. nextrow:                            ;for each row 
  306.     move.l   BMscene,a0
  307.     lea     bm_Planes(a0),a5        ;address of start of plane       
  308.     cmp.w    iHeight,d5              ;check for last row
  309.     bge        finished
  310.     clr.w   d6                      ;planes
  311. nextplane2:                         ;for each plane
  312.     move.l  (a5),a2
  313.     cmp.w    iDepth,d6
  314.     bge         endrow                 ;check for end of plane
  315.     move.w    scrRowBytes,d4          ;calculate offset from first row
  316.     mulu    d5,d4
  317.     add.l   d4,a2                   ;add to bit plane address
  318.     move.w    iRowBytes,d0
  319.     ext.l    d0
  320.     sub.l   #1,d0
  321. nextcol:                            ;for each column
  322.     move.b  (a3)+,(a2)+             ;copy from buffer to bitplane
  323.     dbra     d0,nextcol
  324. endplane:
  325.     add.l    #4,a5                  ;address of start of next plane
  326.     add.w    #1,d6                   ;add to number of planes
  327.     bra        nextplane2
  328. endrow                          
  329.     add.w    #1,d5                   ;add to number of rows
  330.     bra        nextrow
  331.      rts
  332.  
  333. strcmp:                 ;compare two characte strings
  334.     move.b    (a0)+,d0
  335.     tst.b   d0
  336.     beq     eq
  337.     cmp.b    (a1)+,d0
  338.     bne        noteq
  339.     bra     strcmp
  340. eq:  
  341.     clr.l  d0
  342. noteq:
  343.     rts
  344.  
  345.     CSECT DATA    
  346. filename:
  347.    ds.l  1
  348. BMscene:
  349.    ds.l  1
  350. colourlist:
  351.    ds.l  1
  352. bytesread:    
  353.    dc.l  0
  354. totalbytes:    
  355.    DS.l  1
  356. icLen:    
  357.    DS.l  1
  358. iHeight:    
  359.    DS.W    1
  360. iWidth:    
  361.    DS.W    1
  362. scrWidth:    
  363.    DS.W    1
  364. scrHeight:    
  365.    DS.W    1
  366. iDepth:    
  367.    DS.W    1
  368. nColors:    
  369.    DS.W    1
  370. iRowBytes:    
  371.    DS.W    1
  372. scrRowBytes:    
  373.    DS.W    1
  374. iCompr:    
  375.    DS.B    1
  376.    ds.w 0
  377. mybuf:    
  378.    DS.B    4
  379. filelength:
  380.    ds.b 116
  381. rLen:    
  382.    DS.l 1
  383. tempbuffer:    
  384.    DS.l 1
  385. fHandle:    
  386.    DS.l  1
  387. red:    
  388.    DS.W    1
  389. gre:    
  390.    DS.W    1
  391. blu:    
  392.    DS.W    1
  393.  
  394. abit:    
  395.     DC.B    "ABIT",0
  396.     DS.W    0
  397. acbm:
  398.     dc.b    "ACBM",0
  399.     ds.w    0
  400. body:
  401.     dc.b    "BODY",0
  402.     ds.w    0
  403. cmap:
  404.     dc.b    "CMAP",0
  405.     ds.w    0
  406. bmhd:
  407.     dc.b    "BMHD",0
  408.     ds.w    0
  409. ilbm:
  410.     dc.b    "ILBM",0
  411.     ds.w    0
  412.     END 
  413.