st80

Smalltalk-80 (image Xerox de 1983) dans le navigateur, VM du livre bleu en WebAssembly
git clone https://git.schurger.org/st80.git
Log | Files | Refs | README

bitblt.cpp (17664B)


      1 //
      2 //  bitblt.cpp
      3 //  Smalltalk-80
      4 //
      5 //  Created by Dan Banay on 2/20/20.
      6 //  Copyright © 2020 Dan Banay. All rights reserved.
      7 //
      8 //  MIT License
      9 //  Permission is hereby granted, free of charge, to any person obtaining a copy
     10 //  of this software and associated documentation files (the "Software"), to deal
     11 //  in the Software without restriction, including without limitation the rights
     12 //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     13 //  copies of the Software, and to permit persons to whom the Software is
     14 //  furnished to do so, subject to the following conditions:
     15 //
     16 //  The above copyright notice and this permission notice shall be included in all
     17 //  copies or substantial portions of the Software.
     18 //
     19 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20 //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21 //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     22 //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23 //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     24 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     25 //  SOFTWARE.
     26 //
     27 
     28 #include "bitblt.h"
     29 
     30 /* "source"
     31  "initialize a table of bit masks ... p.356"
     32  RightMasks <-
     33      #(0    16r1 16r3 16r7 16rF
     34          16r1F 16r3F 16r7F 16rFF
     35          16r1FF 16r3FF 16r7FF 16rFFF
     36          16r1FFF 16r3FFF 16r7FFF 16rFFFF).
     37  AllOnes <- 16rFFFF
     38 */
     39 
     40 static std::uint16_t RightMasks[] = {
     41     0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF,
     42     0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
     43 };
     44 
     45 
     46 // clipRange
     47 void BitBlt::clipRange()
     48 {
     49    /* "source"
     50    	"clip and adjust source origin and extent appropriately"
     51    	"first in x"
     52    	destX >= clipX
     53    		ifTrue: [sx <- sourceX.
     54    			dx <- destX.
     55    			w <- width]
     56    		ifFalse: [sx <- sourceX + (clipX - destX).
     57    			w <- width - (clipX - destX).
     58    			dx <- clipX].
     59    	(dx + w) > (clipX + clipWidth)
     60    		ifTrue: [w <- w - ((dx + w) - (clipX + clipWidth))].
     61    	"then in y"
     62    	destY >= clipY
     63    		ifTrue: [sy <- sourceY.
     64    			dy <- destY.
     65    			h <- height]
     66    		ifFalse: [sy <- sourceY + clipY - destY.
     67    			h <- height - (clipY - destY).
     68    			dy <- clipY].
     69    	(dy + h) > (clipY + clipHeight)
     70    		ifTrue: [h <- h - ((dy + h) - (clipY + clipHeight))].
     71    	sx < 0
     72    		ifTrue: [dx <- dx - sx.
     73    			w <- w + sx.
     74    			sx <- 0].
     75     "ERROR dbanay need to check if sourceForm is nil."
     76 
     77    	sx + w > sourceForm width
     78    		ifTrue: [w <- w - (sx + w - sourceForm width)].
     79    	sy < 0
     80    		ifTrue: [dy <- dy - sy.
     81    			h <- h + sy.
     82    			sy <- 0].
     83    	sy + h > sourceForm height
     84    		ifTrue: [h <- h - (sy + h - sourceForm height)]
     85    */
     86     
     87      if (clipX < 0)
     88      {
     89          clipWidth += clipX;
     90          clipX = 0;
     91      }
     92      if (clipY < 0)
     93      {
     94          clipHeight += clipY;
     95          clipY = 0;
     96      }
     97     
     98      if ((clipX + clipWidth) > destFormWidth)
     99      {
    100          clipWidth = destFormWidth - clipX;
    101      }
    102     
    103      if ((clipY + clipHeight) > destFormHeight)
    104      {
    105          clipHeight = destFormHeight - clipY;
    106      }
    107 
    108     // clip and adjust source origin and extent appropriately
    109    // first in x
    110     if (destX >= clipX)
    111     {
    112         sx = sourceX;
    113         dx = destX;
    114         w = width;
    115     }
    116     else
    117     {
    118         sx = sourceX + (clipX - destX);
    119         w = width - (clipX - destX);
    120         dx = clipX;
    121     }
    122     if  ((dx + w) > (clipX + clipWidth))
    123     {
    124         w = w - ((dx + w) - (clipX + clipWidth));
    125     }
    126     
    127     //then in y
    128     if (destY >= clipY)
    129     {
    130         sy = sourceY;
    131         dy = destY;
    132         h = height;
    133     }
    134     else
    135     {
    136         sy = sourceY + clipY - destY;
    137         h = height - (clipY - destY);
    138         dy = clipY;
    139     }
    140     if ((dy + h) > (clipY + clipHeight))
    141     {
    142         h = h - ((dy + h) - (clipY + clipHeight));
    143     }
    144     
    145     if (sourceForm == NilPointer)
    146         return;
    147     
    148     if (sx < 0)
    149     {
    150         dx = dx - sx;
    151         w = w + sx;
    152         sx = 0;
    153     }
    154     
    155     if (sx + w > sourceFormWidth)
    156     {
    157         w = w - (sx + w - sourceFormWidth);
    158     }
    159     
    160     if (sy < 0)
    161     {
    162         dy = dy - sy;
    163         h = h + sy;
    164         sy = 0;
    165     }
    166     
    167     if (sy + h > sourceFormHeight)
    168     {
    169         h = h - (sy + h - sourceFormHeight);
    170     }
    171 }
    172 
    173 
    174 // WordArray class>>maxSize
    175 // maxSize
    176 //      "The maximum size of a WordArray is 64640 elements."
    177 //      ^64640
    178 //
    179 // When a Form is allocated, the number of words required to hold the bits is
    180 // (width + 15)/16 * height. If the form is large enough this will exceed maxSize
    181 // and rather than fail, Smalltalk clamps the array size to 64640! This mucks
    182 // things up if such a form is used as a source or target.
    183 
    184 // copyBits
    185 bool BitBlt::copyBits()
    186 {
    187    /* "source"
    188    	"from copyBits, p.356"
    189    	"sets w and h"
    190    	self clipRange.
    191    	(w <= 0 or: [h <= 0]) ifTrue: [^self ]. "null range"
    192    	self computeMasks.
    193    	self checkOverlap.
    194    	self calculateOffsets.
    195    	self copyLoop
    196    */
    197     
    198     // from copyBits, p.356
    199     clipRange();
    200     if (w > 0 && h > 0)
    201     {
    202         updatedX = dx;
    203         updatedY = dy;
    204         updatedWidth = w;
    205         updatedHeight = h;
    206         computeMasks();
    207         // Check if source or dest is "bad"
    208         if (sourceForm != NilPointer && formWordCount(sourceFormWidth, sourceFormHeight) != sourceBitsWordLength  )
    209             return false;
    210         if (formWordCount(destFormWidth, destFormHeight) != destBitsWordLength  )
    211             return false;
    212 
    213         checkOverlap();
    214         calculateOffsets();
    215         copyLoop();
    216     }
    217     else
    218     {
    219         updatedX = 0;
    220         updatedY = 0;
    221         updatedWidth = 0;
    222         updatedHeight = 0;
    223 
    224     }
    225     
    226     return true;
    227     
    228 }
    229 
    230 
    231 // copyLoop
    232 void BitBlt::copyLoop()
    233 {
    234    std::uint16_t prevWord;
    235    std::uint16_t thisWord = 0; //eliminate warning
    236    std::uint16_t skewWord;
    237    std::uint16_t mergeMask;
    238    std::uint16_t halftoneWord;
    239    std::uint16_t mergeWord;
    240    int word;
    241 
    242    /* "source"
    243    	1 to: h do: "here is the vertical loop"
    244    		[ :i |
    245    			(halftoneForm notNil)
    246    				ifTrue: [halftoneWord <- halftoneBits at: (1 + (dy bitAnd: 15)).
    247    					dy <- dy + vDir]
    248    				ifFalse: [halftoneWord <- AllOnes].
    249    			skewWord <- halftoneWord.
    250    			preload
    251    				ifTrue: [prevWord <- sourceBits at: sourceIndex + 1.
    252    					"load the 32-bit shifter"
    253    					sourceIndex <- sourceIndex + hDir]
    254    				ifFalse: [prevWord <- 0].
    255    			mergeMask <- mask1.
    256    			"ERROR: extra to on next line"
    257    			1 to: nWords do: "here is the inner horizontal loop"
    258    				[ :word |
    259    					sourceForm notNil "if source used"
    260    						ifTrue: [prevWord <- prevWord bitAnd: skewMask.
    261                             "ERROR: dbanay need to check if out of range. use 0 if that is the case"
    262    							thisWord <- sourceBits at: sourceIndex + 1.
    263    									"pick up next word"
    264    							skewWord <-
    265    								prevWord bitOr: (thisWord bitAnd:
    266    									skewMask bitInvert).
    267    							prevWord <- thisWord.
    268    							skewWord <- (skewWord bitShift: skew) bitOr:
    269    									(skewWord bitShift: skew - 16)].
    270    									"16-bit rotate"
    271    					mergeWord <- self merge: (skewWord bitAnd: halftoneWord)
    272    							with: (destBits at: destIndex + 1).
    273    					destBits
    274    						at: destIndex + 1
    275    						put: ((mergeMask bitAnd: mergeWord)
    276    							bitOr: (mergeMask bitInvert
    277    								bitAnd: (destBits at: destIndex + 1))).
    278    					sourceIndex <- sourceIndex + hDir.
    279    					destIndex <- destIndex + hDir.
    280    					word = (nWords - 1)
    281    						ifTrue: [mergeMask <- mask2]
    282    						ifFalse: [mergeMask <- AllOnes]].
    283    			sourceIndex <- sourceIndex + sourceDelta.
    284    			destIndex <- destIndex + destDelta]
    285    */
    286    
    287     
    288  
    289     for(int i = 1; i <=h; i++)
    290     {
    291         // here is the vertical loop
    292         if (halftoneForm != NilPointer)
    293         {
    294             halftoneWord = memory.fetchWord_ofObject((dy & 15), halftoneBits);
    295             dy = dy + vDir;
    296         }
    297         else
    298         {
    299             halftoneWord = AllOnes;
    300         }
    301         skewWord = halftoneWord;
    302         if (preload)
    303         {
    304             // load the 32-bit shifter
    305             
    306             prevWord = memory.fetchWord_ofObject(sourceIndex, sourceBits);
    307             sourceIndex = sourceIndex + hDir;
    308         }
    309         else
    310         {
    311             prevWord = 0;
    312         }
    313         mergeMask = mask1;
    314         
    315         // here is the inner horizontal loop
    316         for(word = 1; word <= nWords; word++)
    317         {
    318             if (sourceForm != NilPointer)  // if source used
    319             {
    320                 prevWord = prevWord & skewMask;
    321                 if (word <= sourceRaster && sourceIndex >= 0 && sourceIndex < sourceBitsWordLength)
    322                     thisWord = memory.fetchWord_ofObject(sourceIndex, sourceBits);
    323                 //else
    324                 //    thisWord = 0;
    325                 skewWord = prevWord | (thisWord & ~skewMask);
    326                 
    327                 prevWord = thisWord;
    328                 // Note: replaced left shift by negative value with a right shift of complement
    329                 skewWord = (skewWord << skew) | (skewWord >> (16-skew));
    330             }
    331             
    332             if (destIndex >= destBitsWordLength) return;
    333             
    334             std::uint16_t destWord =  memory.fetchWord_ofObject(destIndex, destBits);
    335             // 16-bit rotate
    336             mergeWord = merge_with(skewWord & halftoneWord,  destWord);
    337             
    338             memory.storeWord_ofObject_withValue(destIndex, destBits,
    339                                                 (mergeMask & mergeWord) |
    340                                                 (~mergeMask & destWord));
    341             sourceIndex = sourceIndex + hDir;
    342             destIndex = destIndex + hDir;
    343  
    344             
    345             if (word == (nWords - 1))
    346                 mergeMask = mask2;
    347             else
    348                 mergeMask = AllOnes;
    349         }
    350         
    351         sourceIndex = sourceIndex + sourceDelta;
    352         destIndex = destIndex + destDelta;
    353 
    354     }
    355 }
    356 
    357 
    358 // calculateOffsets
    359 void BitBlt::calculateOffsets()
    360 {
    361    /* "source"
    362    	"check if need to preload buffer
    363    	(i.e., two words of source needed for first word of destination)"
    364    	preload <- (sourceForm notNil) and:
    365    			[skew ~= 0 and: [skew <= (sx bitAnd: 15)]].
    366    	hDir < 0 ifTrue: [preload <- preload == false].
    367    	"calculate starting offsets"
    368    	sourceIndex <- sy * sourceRaster + (sx // 16).
    369    	destIndex <- dy * destRaster + (dx // 16).
    370    	"calculate increments from end of 1 line to start of next"
    371    	sourceDelta <-
    372    		(sourceRaster * vDir) -
    373    			(nWords + (preload ifTrue: [1] ifFalse: [0]) * hDir).
    374    	destDelta <- (destRaster * vDir) - (nWords * hDir)
    375    */
    376    
    377     // check if need to preload buffer
    378     // (i.e., two words of source needed for first word of destination)
    379     preload = (sourceForm != NilPointer) && skew != 0 && skew <= (sx & 15);
    380     if (hDir < 0) preload = !preload;
    381     
    382     // calculate starting offsets
    383     sourceIndex = sy * sourceRaster + (sx / 16);
    384     destIndex = dy * destRaster + (dx / 16);
    385     // calculate increments from end of 1 line to start of next
    386     // dbanay - note operator precedence change!
    387     sourceDelta =
    388         (sourceRaster * vDir) - ((nWords + (preload ? 1 : 0)) * hDir);
    389     destDelta = (destRaster * vDir) - (nWords * hDir);
    390 }
    391 
    392 
    393 // computeMasks
    394 void BitBlt::computeMasks()
    395 {
    396    int startBits;
    397    int endBits;
    398 
    399    /* "source"
    400    	"calculate skew and edge masks"
    401    	destBits <- destForm bits.
    402    	destRaster <- destForm width - 1 // 16 + 1.
    403    	sourceForm notNil
    404    		ifTrue: [sourceBits <- sourceForm bits.
    405    			sourceRaster <- sourceForm width - 1 // 16 + 1].
    406    	halftoneForm notNil
    407    		ifTrue: [halftoneBits <- halftoneForm bits].
    408    	skew <- (sx - dx) bitAnd: 15.
    409    	"how many bits source gets skewed to right"
    410    	startBits <- 16 - (dx bitAnd: 15).
    411    	" how many bits in first word"
    412    	mask1 <- RightMasks at: startBits + 1.
    413    	endBits <- 15 - ((dx + w - 1) bitAnd: 15).
    414    	"how many bits in last word"
    415    	mask2 <- (RightMasks at: endBits + 1) bitInvert.
    416    	skewMask <- 
    417    		(skew = 0
    418    			ifTrue: [0]
    419    			ifFalse: [RightMasks at: 16 - skew + 1]).
    420    	"determine number of words stored per line; merge masks if necessary"
    421     "ERROR dbanay : nWords <-  (w - startBits + 15) // 16 + 1 for False case"
    422    	w < startBits
    423    		ifTrue: [mask1 <- mask1 bitAnd: mask2.
    424    			mask2 <- 0.
    425    			nWords <- 1]
    426    		ifFalse: [nWords <- (w - startBits - 1) // 16 + 2]
    427    */
    428     
    429     const int BitsInForm = 0;
    430 
    431     
    432     // calculate skew and edge masks
    433     destBits = memory.fetchPointer_ofObject(BitsInForm, destForm);
    434     destBitsWordLength = memory.fetchWordLengthOf(destBits);
    435 
    436     destRaster = (destFormWidth - 1) / 16 + 1;
    437     if (sourceForm != NilPointer)
    438     {
    439         sourceBits = memory.fetchPointer_ofObject(BitsInForm, sourceForm);
    440         sourceBitsWordLength = memory.fetchWordLengthOf(sourceBits);
    441         sourceRaster = (sourceFormWidth - 1) / 16 + 1;
    442     }
    443     else
    444         sourceBitsWordLength = 0;
    445     
    446     if (halftoneForm != NilPointer)
    447     {
    448        halftoneBits = memory.fetchPointer_ofObject(BitsInForm, halftoneForm);
    449     }
    450     
    451     // how many bits source gets skewed to right
    452     skew = (sx - dx) & 15;
    453     
    454     // how many bits in first word
    455     startBits = 16 - (dx & 15);
    456     
    457     mask1 = RightMasks[startBits]; // +1 removed - dbanay -- C/C++ arrays start at 0
    458     
    459     // how many bits in last word
    460     endBits = 15 - ((dx + w - 1) & 15);
    461     
    462     mask2 = ~RightMasks[endBits]; // +1 removed - dbanay -- C/C++ arrays start at 0
    463     skewMask = skew == 0 ? 0 : RightMasks[16 - skew];
    464     // determine number of words stored per line; merge masks if necessary
    465     // Bluebook had the nWords calculation wrong when w == startBits
    466     if (w < startBits)
    467     {
    468         mask1 = mask1 & mask2;
    469         mask2 = 0;
    470         nWords = 1;
    471     }
    472     else
    473     {
    474         nWords = (w - startBits + 15) / 16 + 1;
    475     }
    476 }
    477 
    478 
    479 // checkOverlap
    480 void BitBlt::checkOverlap()
    481 {
    482    int t;
    483 
    484    /* "source"
    485    	"check for possible overlap of source and destination"
    486    	hDir <- vDir <- 1. "defaults for no overlap"
    487    	(sourceForm == destForm and: [dy >= sy])
    488    		ifTrue: [dy > sy "have to start at bottom"
    489    				ifTrue: [vDir <- -1.
    490    					sy <- sy + h - 1.
    491    					dy <- dy + h - 1]
    492    				ifFalse: [dx > sx "y's are equal, but x's are backward"
    493    						ifTrue: [hDir <- -1.
    494    							sx <- sx + w - 1.
    495    							"start at right"
    496    							dx <- dx + w - 1.
    497    							"and fix up masks"
    498    							skewMask <- skewMask bitInvert.
    499    							t <- mask1.
    500    							mask1 <- mask2.
    501    						mask2 <- t]]]
    502    */
    503 
    504     // check for possible overlap of source and destination
    505     hDir = vDir = 1; // defaults for no overlap
    506     if (sourceForm == destForm && (dy >= sy))
    507     {
    508         if (dy > sy) // have to start at bottom
    509         {
    510             vDir = -1;
    511             sy = sy + h - 1;
    512             dy = dy + h - 1;
    513         }
    514         else
    515         {
    516             if (dx > sx)
    517             {
    518                 // y's are equal, but x's are backward
    519                 hDir = -1;
    520                 // start at right
    521                 sx = sx + w - 1;
    522                 dx = dx + w - 1;
    523                 // and fix up masks
    524                 skewMask = ~skewMask;
    525                 t = mask1;
    526                 mask1 = mask2;
    527                 mask2 = t;
    528             }
    529         }
    530     }
    531 }
    532 
    533 
    534 
    535 // CharacterScanner
    536 
    537 int CharacterScanner::scanCharactersFrom_to_in_rightX_stopConditions_displaying(
    538                                                                                 int startIndex,
    539                                                                                 int stopIndex,
    540                                                                                 int sourceString,
    541                                                                                 int rightX,
    542                                                                                 int stops,
    543                                                                                 bool displaying)
    544 {
    545     const int EndOfRun = 257; // TextConstants at: #EndOfRun put: 257.
    546     const int CrossedX = 258; // TextConstants at: #CrossedX put: 258.    
    547     
    548     /* "source"
    549      | ascii nextDestX |
    550      lastIndex <- startIndex.
    551      [lastIndex <= stopIndex]
    552          whileTrue:
    553              [ascii <- (sourceString at: lastIndex) asciiValue.
    554              (stopConditions at: ascii + 1) ~~ nil ifTrue: [^stops at: ascii + 1].
    555              sourceX <- xTable at: ascii + 1.
    556              nextDestX <- destX + (width _ (xTable at: ascii + 2) - sourceX).
    557              nextDestX > rightX ifTrue: [^stops at: CrossedX].
    558              display ifTrue: [self copyBits].
    559              destX <- nextDestX.
    560              lastIndex <- lastIndex + 1].
    561      lastIndex <- stopIndex.
    562      ^stops at: EndOfRun
    563 
    564      
    565      */
    566     int ascii;
    567     int nextDestX;
    568         
    569     lastIndex = startIndex;
    570     while (lastIndex <= stopIndex)
    571     {
    572         ascii = memory.fetchByte_ofObject(lastIndex-1, sourceString);
    573 
    574         if (memory.fetchPointer_ofObject(ascii, stopConditions) != NilPointer)
    575         {
    576             return memory.fetchPointer_ofObject(ascii, stops);
    577         }
    578 
    579         sourceX = memory.integerValueOf(memory.fetchPointer_ofObject(ascii, xTable));
    580         width =  memory.integerValueOf(memory.fetchPointer_ofObject(ascii+1, xTable)) - sourceX;
    581         nextDestX = destX + width;
    582         if (nextDestX > rightX)
    583         {
    584             return memory.fetchPointer_ofObject(CrossedX-1, stops);
    585         }
    586         if (displaying) copyBits();
    587         destX = nextDestX;
    588         lastIndex = lastIndex + 1;
    589     }
    590     
    591     lastIndex = stopIndex;
    592     return memory.fetchPointer_ofObject(EndOfRun-1, stops);
    593 }
    594