📄 drawline.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: drawline.cpp,v 1.1.1.1.42.1 2004/07/09 01:59:28 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */#include <stdlib.h>#include <math.h>#include "hxtypes.h"#include "drawline.h"#ifdef _DEBUG#include "region.h" //just for _DumpString#include <stdio.h>#endif#define BLURALPHA(x,color) \ {\ int alphap = 255-(((x)&0xff000000)>>24);\ int alpha = 255-(((color)&0xff000000)>>24);\ ULONG32 newalp = (255-((alphap*alpha)/255))<<24;\ (x) = ((x)&0x00ffffff)|newalp;\ }void WriteBits(UCHAR *pBuffer, UINT8 bpp, UINT8 red, UINT8 green, UINT8 blue);BOOL DrawOffColor(UINT32 ulStyle, UINT32 nPixelCount);//color is now interpreted as a color if it is has no alpha channel.//if the color does have an alpha channel value(non-zero) then we//are blending borders and will only write the alphavalue into the//destination pixels. If the destination has alpha values then we//will void DrawLine( int* bits, int width, int x0, int y0, int x1, int y1, ULONG32 color ){ int temp, adjUp, adjDown, errorTerm, xAdvance, xDelta, yDelta, wholeStep, initialPixelCount, finalPixelCount, i, j; BOOL bBlended = FALSE; if( color & 0xff000000) { //This is a blended border line. Just mess with the alpha channel. bBlended = TRUE; } // We'll always draw top to bottom to reduce the number of cases we have to // handle, and to make lines between the same endpoints draw the same pixels if (y0 > y1) { temp = y0; y0 = y1; y1 = temp; temp = x0; x0 = x1; x1 = temp; } int* p = bits + x0 + y0 * width; // Figure out if we're going left or right, and how far we're going horizontally if ((xDelta = x1 - x0) < 0) { xAdvance = -1; xDelta = -xDelta; } else { xAdvance = 1; } // Figure out how far we're going vertically yDelta = y1 - y0; // Special-case horizontal, vertical and diagonal lines, for speed and to avoid // boundary conditions and divide by 0 if (!xDelta) { // vertical line for (i = 0; i <= yDelta; ++i) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += width; } return; } if (!yDelta) { // horizontal line for (i = 0; i <= xDelta; ++i) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += xAdvance; } return; } if (xDelta == yDelta) { // diagonal line for (i = 0; i <= xDelta; ++i) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += width + xAdvance; } return; } // determins whether line is X or Y major if (xDelta > yDelta) { // X major line // minimum number of pixels in a run wholeStep = xDelta / yDelta; // Error term adjust each time Y steps by 1; used to tell when // one extra pixel should be drawn as part of a run, to account for // fractional steps along the X axis per 1 pixel steps along Y adjUp = (xDelta % yDelta) * 2; // Error term adjust when the error term turns over, used to factor // out the X step made at that time adjDown = yDelta * 2; // Initial error term reflects an initial step of 0.5 along the Y axis errorTerm = (xDelta % yDelta) - (yDelta * 2); // The initial and last runs are partial, because Y advances only 0.5 // for these runs, rather than 1. Divide one full run, plus the // initial pixel, between the initail and last runs initialPixelCount = (wholeStep / 2) + 1; finalPixelCount = initialPixelCount; // If the basic run length is even and there's no fractional // advance, we have one pixel that could go to either the initial // or last partial run, which we'll arbitrarily allocate to the last run if ((!adjUp) && ((wholeStep & 1) == 0)) { --initialPixelCount; } // If ther are an odd number of pixels per run, we have 1 pixel that can't // be allocated to either the initial or last partial run, so we'll add 0.5 // to the error term so this pixel will be handled by the normal full-run loop if (wholeStep & 1) { errorTerm += yDelta; } // Draw the first partial run of pixels for (j = 0; j < initialPixelCount; ++j) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += xAdvance; } p += width; // Draw all full runs for (i = 0; i < yDelta - 1; ++i) { // advance the error term and add an extra pixel if indicated if ((errorTerm += adjUp) > 0) { errorTerm -= adjDown; // Draw this scan lines run for (j = 0; j < wholeStep + 1; ++j) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += xAdvance; } } else { // Draw this scan lines run for (j = 0; j < wholeStep; ++j) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += xAdvance; } } p += width; } // Draw final run for (j = 0; j < finalPixelCount; ++j) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += xAdvance; } return; } else { // Y major line // for comments see above section wholeStep = yDelta / xDelta; adjUp = (yDelta % xDelta) * 2; adjDown = xDelta * 2; errorTerm = (yDelta % xDelta) - (xDelta * 2); initialPixelCount = (wholeStep / 2) + 1; finalPixelCount = initialPixelCount; if ((!adjUp) && ((wholeStep & 1) == 0)) { --initialPixelCount; } if (wholeStep & 1) { errorTerm += xDelta; } for (j = 0; j < initialPixelCount; ++j) { if( !bBlended ) *p = color; else BLURALPHA(*p, color); p += width; } p += xAdvance;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -