bitblt.h (11289B)
1 // 2 // bitblt.h 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 29 #pragma once 30 #include "objmemory.h" 31 #include <cstdint> 32 33 34 //BItBlt 35 static const int DestFormIndex = 0; 36 static const int SourceFormIndex = 1; 37 static const int HalftoneFormIndex = 2; 38 static const int CombinationRuleIndex = 3; 39 static const int DestXIndex = 4; 40 static const int DestYIndex = 5; 41 static const int WidthIndex = 6; 42 static const int HeightIndex = 7; 43 static const int SourceXIndex = 8; 44 static const int SourceYIndex = 9; 45 static const int ClipXIndex = 10; 46 static const int ClipYIndex = 11; 47 static const int ClipWidthIndex = 12; 48 static const int ClipHeightIndex = 13; 49 50 51 // BitBltSimulation 52 class BitBlt 53 { 54 public: 55 56 BitBlt(ObjectMemory &memory, 57 int destForm, 58 int sourceForm, 59 int halftoneForm, 60 int combinationRule, 61 int destX, 62 int destY, 63 int width, 64 int height, 65 int sourceX, 66 int sourceY, 67 int clipX, 68 int clipY, 69 int clipWidth, 70 int clipHeight): 71 memory(memory), 72 destForm(destForm), 73 sourceForm(sourceForm), 74 halftoneForm(halftoneForm), 75 combinationRule(combinationRule), 76 destX(destX), 77 destY(destY), 78 width(width), 79 height(height), 80 sourceX(sourceX), 81 sourceY(sourceY), 82 clipX(clipX), 83 clipY(clipY), 84 clipWidth(clipWidth), 85 clipHeight(clipHeight) 86 87 { 88 const int WidthInForm = 1; 89 const int HeightInForm = 2; 90 91 if (sourceForm != NilPointer) 92 { 93 sourceFormWidth = memory.integerValueOf(memory.fetchWord_ofObject(WidthInForm, sourceForm)); 94 sourceFormHeight = memory.integerValueOf(memory.fetchWord_ofObject(HeightInForm, sourceForm)); 95 } 96 else 97 { 98 sourceX = sourceY = 0; 99 sourceFormHeight = sourceFormWidth = 0; 100 } 101 destFormWidth = memory.integerValueOf(memory.fetchWord_ofObject(WidthInForm, destForm)); 102 destFormHeight = memory.integerValueOf(memory.fetchWord_ofObject(HeightInForm, destForm)); 103 updatedX = 0; 104 updatedY = 0; 105 updatedWidth = 0; 106 updatedHeight = 0; 107 } 108 109 110 bool copyBits(); 111 112 void getUpdatedBounds(int *boundsX, int *boundsY, int *boundsWidth, int *boundsHeight) 113 { 114 *boundsX = updatedX; 115 *boundsY = updatedY; 116 *boundsWidth = updatedWidth; 117 *boundsHeight = updatedHeight; 118 } 119 120 protected: 121 122 123 static const std::uint16_t AllOnes = 0xFFFF; 124 125 // clipRange 126 void clipRange(); 127 128 // merge:with: 129 inline std::uint16_t merge_with(std::uint16_t sourceWord, std::uint16_t destinationWord) 130 { 131 /* "source" 132 "These are the 16 combination rules:" 133 combinationRule = 0 134 ifTrue: [^0]. 135 combinationRule = 1 136 ifTrue: [^sourceWord bitAnd: destinationWord]. 137 combinationRule = 2 138 ifTrue: [^sourceWord bitAnd: destinationWord bitInvert]. 139 combinationRule = 3 140 ifTrue: [^sourceWord]. 141 combinationRule = 4 142 ifTrue: [^sourceWord bitInvert bitAnd: destinationWord]. 143 combinationRule = 5 144 ifTrue: [^destinationWord]. 145 combinationRule = 6 146 ifTrue: [^sourceWord bitXor: destinationWord]. 147 combinationRule = 7 148 ifTrue: [^sourceWord bitOr: destinationWord]. 149 combinationRule = 8 150 ifTrue: [^sourceWord bitInvert bitAnd: destinationWord bitInvert]. 151 combinationRule = 9 152 ifTrue: [^sourceWord bitInvert bitXor: destinationWord]. 153 combinationRule = 10 154 ifTrue: [^destinationWord bitInvert]. 155 combinationRule = 11 156 ifTrue: [^sourceWord bitOr: destinationWord bitInvert]. 157 combinationRule = 12 158 ifTrue: [^sourceWord bitInvert]. 159 combinationRule = 13 160 ifTrue: [^sourceWord bitInvert bitOr: destinationWord]. 161 combinationRule = 14 162 ifTrue: [^sourceWord bitInvert bitOr: destinationWord bitInvert]. 163 combinationRule = 15 164 ifTrue: [^AllOnes] 165 */ 166 167 // These are the 16 combination rules: 168 switch (combinationRule) 169 { 170 case 0 171 : return 0; 172 case 1 // [^sourceWord bitAnd: destinationWord] 173 : return sourceWord & destinationWord; 174 175 case 2 // [^sourceWord bitAnd: destinationWord bitInvert] 176 : return sourceWord & (~destinationWord); 177 178 case 3 // [^sourceWord] 179 : return sourceWord; 180 181 case 4 // [^sourceWord bitInvert bitAnd: destinationWord] 182 : return (~sourceWord) & destinationWord; 183 184 case 5 // [^destinationWord] 185 : return destinationWord; 186 187 case 6 // [^sourceWord bitXor: destinationWord] 188 : return sourceWord ^ destinationWord; 189 190 case 7 // [^sourceWord bitOr: destinationWord] 191 : return sourceWord | destinationWord; 192 193 case 8 // [^sourceWord bitInvert bitAnd: destinationWord bitInvert]. 194 : return (~sourceWord) & (~destinationWord); 195 196 case 9 // [^sourceWord bitInvert bitXor: destinationWord]. 197 : return (~sourceWord) ^ destinationWord; 198 199 case 10 // [^destinationWord bitInvert] 200 : return ~destinationWord; 201 202 case 11 // [^sourceWord bitOr: destinationWord bitInvert]. 203 : return sourceWord | (~destinationWord); 204 205 case 12 // [^sourceWord bitInvert] 206 : return ~sourceWord; 207 208 case 13 // [^sourceWord bitInvert bitOr: destinationWord] 209 : return (~sourceWord) | destinationWord; 210 211 case 14 // [^sourceWord bitInvert bitOr: destinationWord bitInvert]. 212 : return (~sourceWord) | (~destinationWord); 213 214 case 15 215 : return AllOnes; 216 default: 217 assert(0); 218 } 219 220 return 0; 221 } 222 223 // copyLoop 224 void copyLoop(); 225 226 // calculateOffsets 227 void calculateOffsets(); 228 229 // computeMasks 230 void computeMasks(); 231 232 // checkOverlap 233 void checkOverlap(); 234 235 ObjectMemory& memory; 236 int destForm; 237 int sourceForm; 238 int halftoneForm; 239 int combinationRule; 240 int destX; 241 int destY; 242 int width; 243 int height; 244 int sourceX; 245 int sourceY; 246 int clipX; 247 int clipY; 248 int clipWidth; 249 int clipHeight; 250 251 // instance variables (pg. 356 G&R) 252 int sourceBits, sourceRaster, destBits, destRaster, halftoneBits, 253 skew, 254 preload, nWords, 255 hDir, vDir, 256 sourceIndex, sourceDelta, destIndex, destDelta, 257 sx, sy, dx, dy, w, h; 258 259 int sourceBitsWordLength, destBitsWordLength; 260 261 //sourceRaster is the source pitch, and destRaster the destination pitch. 262 //I guess these terms were unknown to the implementors. 263 264 std::uint16_t mask1, mask2, skewMask; 265 266 // Additional info 267 int sourceFormWidth; 268 int sourceFormHeight; 269 int destFormWidth; 270 int destFormHeight; 271 272 // Actual area affected by last copyBots 273 int updatedX, updatedY, updatedWidth, updatedHeight; 274 protected: 275 inline int formWordCount(int width, int height) 276 { 277 return (width + 15) / 16 * height; 278 } 279 280 }; 281 282 //Character Scanner 283 static const int LastIndexIndex = 14; 284 static const int XTableIndexIndex = 15; 285 static const int StopConditionsIndex = 16; 286 static const int TextIndex = 17; 287 static const int TextStyleIndex = 18; 288 static const int LeftMarginIndex = 19; 289 static const int RightMarginIndex = 20; 290 static const int FontIndex = 21; 291 static const int LineIndex = 22; 292 static const int RunStopIndexIndex = 23; 293 static const int SpaceCountIndex = 24; 294 static const int SpaceWidthIndex = 25; 295 static const int OutputMediumIndex = 26; 296 297 class CharacterScanner: public BitBlt 298 { 299 public: 300 CharacterScanner(ObjectMemory &memory, 301 int destForm, 302 int sourceForm, 303 int halftoneForm, 304 int combinationRule, 305 int destX, 306 int destY, 307 int width, 308 int height, 309 int sourceX, 310 int sourceY, 311 int clipX, 312 int clipY, 313 int clipWidth, 314 int clipHeight, 315 316 317 int xTable, 318 int lastIndex, 319 int stopConditions) : 320 BitBlt(memory, 321 destForm, 322 sourceForm, 323 halftoneForm, 324 combinationRule, 325 destX, 326 destY, 327 width, 328 height, 329 sourceX, 330 sourceY, 331 clipX, 332 clipY, 333 clipWidth, 334 clipHeight), 335 xTable(xTable), 336 lastIndex(lastIndex), 337 stopConditions(stopConditions) 338 { 339 } 340 341 342 // After scanning, internal state is updated for 3 variables which must be 343 // written back to the object store. Here they are: 344 int updateDestX() { return destX; } 345 int updatedWidth() { return width; } 346 int updatedSourceX() { return sourceX; } 347 int updatedLastIndex() { return lastIndex; } 348 349 int scanCharactersFrom_to_in_rightX_stopConditions_displaying( 350 int startIndex, 351 int stopIndex, 352 int sourceString, 353 int rightX, 354 int stop, 355 bool displaying); 356 357 int stopConditions; 358 int xTable; 359 int lastIndex; 360 361 362 }; 363