Open main menu

CDOT Wiki β

Changes

User:Dhhodgin

19,376 bytes added, 21:18, 15 December 2009
Code Blocks
=== blit_resize() ===
blit_resize is a helper function for image manipulation used with blend() and copy()
  /** * Internal blitter/resizer/copier from toxi. * Uses bilinear filtering if smooth() has been enabled * 'mode' determines the blending mode used in the process. * ported from JAVA version */ p.blit_resize coming soon= function blit_resize(img, srcX1, srcY1, srcX2, srcY2, destPixels, screenW, screenH, destX1, destY1, destX2, destY2, mode) { if (srcX1 < 0) srcX1 = 0; if (srcY1 < 0) srcY1 = 0; if (srcX2 >= img.width) srcX2 = img.width - 1; if (srcY2 >= img.height) srcY2 = img.height - 1; var srcW = srcX2 - srcX1; var srcH = srcY2 - srcY1; var destW = destX2 - destX1; var destH = destY2 - destY1; var smooth = true; // may as well go with the smoothing these days if (!smooth) { srcW++; srcH++; } if (destW <= 0 || destH <= 0 || srcW <= 0 || srcH <= 0 || destX1 >= screenW || destY1 >= screenH || srcX1 >= img.width || srcY1 >= img.height) { return; } var dx = Math.floor(srcW / destW * p.PRECISIONF); var dy = Math.floor(srcH / destH * p.PRECISIONF); p.shared.srcXOffset = Math.floor(destX1 < 0 ? -destX1 * dx : srcX1 * p.PRECISIONF); p.shared.srcYOffset = Math.floor(destY1 < 0 ? -destY1 * dy : srcY1 * p.PRECISIONF); if (destX1 < 0) { destW += destX1; destX1 = 0; } if (destY1 < 0) { destH += destY1; destY1 = 0; } destW = Math.min(destW, screenW - destX1); destH = Math.min(destH, screenH - destY1); var destOffset = destY1 * screenW + destX1; p.shared.srcBuffer = img.pixels; if (smooth) { // use bilinear filtering p.shared.iw = img.width; p.shared.iw1 = img.width - 1; p.shared.ih1 = img.height - 1; switch (mode) { case p.BLEND: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_blend(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; p.shared.srcYOffset += dy; } break; case p.ADD: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_add_pin(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SUBTRACT: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_sub_pin(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.LIGHTEST: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_lightest(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.DARKEST: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_darkest(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.REPLACE: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.filter_bilinear(); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.DIFFERENCE: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_difference(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.EXCLUSION: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_exclusion(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.MULTIPLY: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_multiply(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SCREEN: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_screen(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.OVERLAY: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_overlay(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.HARD_LIGHT: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_hard_light(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SOFT_LIGHT: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_soft_light(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; // davbol - proposed 2007-01-09 case p.DODGE: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_dodge(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.BURN: for (var y = 0; y < destH; y++) { p.filter_new_scanline(); for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_burn(destPixels[destOffset + x], p.filter_bilinear()); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; } } else { // nearest neighbour scaling (++fast!) switch (mode) { case p.BLEND: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { // davbol - renamed old blend_multiply to blend_blend destPixels[destOffset + x] = p.modes.blend_blend(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.ADD: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_add_pin(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SUBTRACT: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_sub_pin(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.LIGHTEST: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_lightest(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.DARKEST: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_darkest(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.REPLACE: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = srcBuffer[sY + (sX >> p.PRECISIONB)]; sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.DIFFERENCE: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_difference(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.EXCLUSION: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_exclusion(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.MULTIPLY: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_multiply(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SCREEN: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_screen(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.OVERLAY: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_overlay(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.HARD_LIGHT: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_hard_light(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.SOFT_LIGHT: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_soft_light(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; // davbol - proposed 2007-01-09 case p.DODGE: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_dodge(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; case p.BURN: for (var y = 0; y < destH; y++) { sX = srcXOffset; sY = (srcYOffset >> p.PRECISIONB) * img.width; for (var x = 0; x < destW; x++) { destPixels[destOffset + x] = p.modes.blend_burn(destPixels[destOffset + x], srcBuffer[sY + (sX >> p.PRECISIONB)]); sX += dx; } destOffset += screenW; srcYOffset += dy; } break; } } };
=== blend() helper functions===
1
edit