dib32bpp.c

来自「一个类似windows」· C语言 代码 · 共 835 行 · 第 1/2 页

C
835
字号
        }
    }

  return TRUE;
}

//NOTE: If you change something here, please do the same in other dibXXbpp.c files!
BOOLEAN ScaleRectAvg32(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
                       RECTL* DestRect, RECTL *SourceRect,
                       POINTL* MaskOrigin, POINTL BrushOrigin,
                       CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
                       ULONG Mode)
{
  int NumPixels = DestRect->bottom - DestRect->top;
  int IntPart = (((SourceRect->bottom - SourceRect->top) / (DestRect->bottom - DestRect->top)) * SourceSurf->lDelta) / 4;
  int FractPart = (SourceRect->bottom - SourceRect->top) % (DestRect->bottom - DestRect->top);
  int Mid = (DestRect->bottom - DestRect->top) >> 1;
  int E = 0;
  int skip;
  PIXEL *ScanLine, *ScanLineAhead;
  PIXEL *PrevSource = NULL;
  PIXEL *PrevSourceAhead = NULL;
  PIXEL *Target = (PIXEL *) ((PBYTE)DestSurf->pvScan0 + (DestRect->top * DestSurf->lDelta) + 4 * DestRect->left);
  PIXEL *Source = (PIXEL *) ((PBYTE)SourceSurf->pvScan0 + (SourceRect->top * SourceSurf->lDelta) + 4 * SourceRect->left);
  PSPAN ClipSpans;
  UINT ClipSpansCount;
  UINT SpanIndex;
  LONG DestY;

  if (! ClipobjToSpans(&ClipSpans, &ClipSpansCount, ClipRegion, DestRect))
    {
      return FALSE;
    }
  if (0 == ClipSpansCount)
    {
      /* No clip spans == empty clipping region, everything clipped away */
      ASSERT(NULL == ClipSpans);
      return TRUE;
    }
  skip = (DestRect->bottom - DestRect->top < SourceRect->bottom - SourceRect->top) ? 0 : ((DestRect->bottom - DestRect->top) / (2 * (SourceRect->bottom - SourceRect->top)) + 1);
  NumPixels -= skip;

  ScanLine = (PIXEL*)ExAllocatePool(PagedPool, (DestRect->right - DestRect->left) * sizeof(PIXEL));
  ScanLineAhead = (PIXEL *)ExAllocatePool(PagedPool, (DestRect->right - DestRect->left) * sizeof(PIXEL));

  DestY = DestRect->top;
  SpanIndex = 0;
  while (NumPixels-- > 0) {
    if (Source != PrevSource) {
      if (Source == PrevSourceAhead) {
        /* the next scan line has already been scaled and stored in
         * ScanLineAhead; swap the buffers that ScanLine and ScanLineAhead
         * point to
         */
        PIXEL *tmp = ScanLine;
        ScanLine = ScanLineAhead;
        ScanLineAhead = tmp;
      } else {
        ScaleLineAvg32(ScanLine, Source, SourceRect->right - SourceRect->left, DestRect->right - DestRect->left);
      } /* if */
      PrevSource = Source;
    } /* if */

    if (E >= Mid && PrevSourceAhead != (PIXEL *)((BYTE *)Source + SourceSurf->lDelta)) {
      int x;
      ScaleLineAvg32(ScanLineAhead, (PIXEL *)((BYTE *)Source + SourceSurf->lDelta), SourceRect->right - SourceRect->left, DestRect->right - DestRect->left);
      for (x = 0; x < DestRect->right - DestRect->left; x++)
        ScanLine[x] = average32(ScanLine[x], ScanLineAhead[x]);
      PrevSourceAhead = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
    } /* if */

    if (! FinalCopy32(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex, DestY, DestRect))
      {
        /* No more spans, everything else is clipped away, we're done */
        ExFreePool(ClipSpans);
        ExFreePool(ScanLine);
        ExFreePool(ScanLineAhead);
        return TRUE;
      }
    DestY++;
    Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
    Source += IntPart;
    E += FractPart;
    if (E >= DestRect->bottom - DestRect->top) {
      E -= DestRect->bottom - DestRect->top;
      Source = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
    } /* if */
  } /* while */

  if (skip > 0 && Source != PrevSource)
    ScaleLineAvg32(ScanLine, Source, SourceRect->right - SourceRect->left, DestRect->right - DestRect->left);
  while (skip-- > 0) {
    if (! FinalCopy32(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex, DestY, DestRect))
      {
        /* No more spans, everything else is clipped away, we're done */
        ExFreePool(ClipSpans);
        ExFreePool(ScanLine);
        ExFreePool(ScanLineAhead);
        return TRUE;
      }
    DestY++;
    Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
  } /* while */

  ExFreePool(ClipSpans);
  ExFreePool(ScanLine);
  ExFreePool(ScanLineAhead);

  return TRUE;
}

//NOTE: If you change something here, please do the same in other dibXXbpp.c files!
BOOLEAN DIB_32BPP_StretchBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
                            RECTL* DestRect, RECTL *SourceRect,
                            POINTL* MaskOrigin, POINTL BrushOrigin,
                            CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
                            ULONG Mode)
{
  
   LONG SrcSizeY;
   LONG SrcSizeX;
   LONG DesSizeY;
   LONG DesSizeX;      
   LONG sx;
   LONG sy;
   LONG DesX;
   LONG DesY;
   LONG color;
   PULONG DestBits;
   LONG DifflDelta;

      
   DPRINT("DIB_32BPP_StretchBlt: Source BPP: %u, srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
            BitsPerFormat(SourceSurf->iBitmapFormat), SourceRect->left, SourceRect->top, SourceRect->right, 
            SourceRect->bottom, DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);

    SrcSizeY = SourceRect->bottom - SourceRect->top;
    SrcSizeX = SourceRect->right - SourceRect->left;
  
    DesSizeY = DestRect->bottom - DestRect->top;
    DesSizeX = DestRect->right - DestRect->left;   


    switch(SourceSurf->iBitmapFormat)
    {
     case BMF_1BPP:
	  /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
      /* This is a reference implementation, it hasn't been optimized for speed */
                      
      DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 2) +
                  DestRect->top * DestSurf->lDelta);
       
	   DifflDelta = DestSurf->lDelta -  (DesSizeX << 2); 
	                
       for (DesY=0; DesY<DesSizeY; DesY++)
       {			 
           sy = ((DesY  * SrcSizeY) / DesSizeY) + SourceRect->top;
                     
            for (DesX=0; DesX<DesSizeX; DesX++, DestBits++ )
            {			
                 sx = ((DesX  * SrcSizeX) / DesSizeX) + SourceRect->left;  		
                   		
                  if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
				  {
					*DestBits =  XLATEOBJ_iXlate(ColorTranslation, 0);
                  } 
				  else 
				  {
                    *DestBits =  XLATEOBJ_iXlate(ColorTranslation, 1);
                  }
            }
            DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
       }		

	  break;

      case BMF_4BPP:		
      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
      /* This is a reference implementation, it hasn't been optimized for speed */
                      
      DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 2) +
                  DestRect->top * DestSurf->lDelta);
       
	   DifflDelta = DestSurf->lDelta -  (DesSizeX << 2); 
	                
       for (DesY=0; DesY<DesSizeY; DesY++)
       {			 
           sy = ((DesY  * SrcSizeY) / DesSizeY) + SourceRect->top;
                     
            for (DesX=0; DesX<DesSizeX; DesX++, DestBits++ )
            {			
                 sx = ((DesX  * SrcSizeX) / DesSizeX) + SourceRect->left;  		
                 color = DIB_4BPP_GetPixel(SourceSurf, sx, sy);
                 color = XLATEOBJ_iXlate(ColorTranslation, color);
                 *DestBits =  color;
            }
            
            DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
       }	  	   
      break;

      case BMF_8BPP:
      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
      /* This is a reference implementation, it hasn't been optimized for speed */
      
      DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 2) +
                  DestRect->top * DestSurf->lDelta);
       
	   DifflDelta = DestSurf->lDelta -  (DesSizeX << 2); 
	                
       for (DesY=0; DesY<DesSizeY; DesY++)
       {			 
           sy = ((DesY  * SrcSizeY) / DesSizeY) + SourceRect->top;
                     
            for (DesX=0; DesX<DesSizeX; DesX++, DestBits++)
            {			
                 sx = ((DesX  * SrcSizeX) / DesSizeX) + SourceRect->left;  		
                 color = DIB_8BPP_GetPixel(SourceSurf, sx, sy);
                 color = XLATEOBJ_iXlate(ColorTranslation, color);
                 *DestBits =  color;
            }
            DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
       }	  	   		
      break;

      case BMF_16BPP:		
      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
      /* This is a reference implementation, it hasn't been optimized for speed */
                      
      DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 2) +
                  DestRect->top * DestSurf->lDelta);
       
	   DifflDelta = DestSurf->lDelta -  (DesSizeX << 2); 
	                
       for (DesY=0; DesY<DesSizeY; DesY++)
       {			 
           sy = ((DesY  * SrcSizeY) / DesSizeY) + SourceRect->top;
                     
            for (DesX=0; DesX<DesSizeX; DesX++, DestBits++)
            {			
                 sx = ((DesX  * SrcSizeX) / DesSizeX) + SourceRect->left;  		
                 color = DIB_16BPP_GetPixel(SourceSurf, sx, sy);
                 color = XLATEOBJ_iXlate(ColorTranslation, color);
                 *DestBits = color;
            }
            DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
       }	  	   
	  break;

      case BMF_24BPP:
      /* FIXME :  MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
      /* This is a reference implementation, it hasn't been optimized for speed */
                      
      DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 2) +
                  DestRect->top * DestSurf->lDelta);
       
	   DifflDelta = DestSurf->lDelta -  (DesSizeX << 2); 
	                
       for (DesY=0; DesY<DesSizeY; DesY++)
       {			 
           sy = ((DesY  * SrcSizeY) / DesSizeY) + SourceRect->top;
                     
            for (DesX=0; DesX<DesSizeX; DesX++, DestBits++)
            {			
                 sx = ((DesX  * SrcSizeX) / DesSizeX) + SourceRect->left;  		
                 color = DIB_24BPP_GetPixel(SourceSurf, sx, sy);
                 color = XLATEOBJ_iXlate(ColorTranslation, color);
                 *DestBits =  color;
            }
            DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
       }	  
	   break;

      case BMF_32BPP:
        return ScaleRectAvg32(DestSurf, SourceSurf, DestRect, SourceRect, MaskOrigin, BrushOrigin,
                              ClipRegion, ColorTranslation, Mode);
      break;

      default:
      //DPRINT1("DIB_32BPP_StretchBlt: Unhandled Source BPP: %u\n", BitsPerFormat(SourceSurf->iBitmapFormat));
      return FALSE;
    }



  return TRUE;
}

BOOLEAN
DIB_32BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
                         RECTL*  DestRect,  POINTL  *SourcePoint,
                         XLATEOBJ *ColorTranslation, ULONG iTransColor)
{
  ULONG X, Y, SourceX, SourceY, Source, wd;
  ULONG *DestBits;

  SourceY = SourcePoint->y;
  DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 +
                      (DestRect->left << 2) +
                      DestRect->top * DestSurf->lDelta);
  wd = DestSurf->lDelta - ((DestRect->right - DestRect->left) << 2);

  for(Y = DestRect->top; Y < DestRect->bottom; Y++)
  {
    SourceX = SourcePoint->x;
    for(X = DestRect->left; X < DestRect->right; X++, DestBits++, SourceX++)
    {
      Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
      if(Source != iTransColor)
      {
        *DestBits = XLATEOBJ_iXlate(ColorTranslation, Source);
      }
    }

    SourceY++;
    DestBits = (ULONG*)((ULONG_PTR)DestBits + wd);
  }

  return TRUE;
}

typedef union {
   ULONG ul;
   struct {
      UCHAR red;
      UCHAR green;
      UCHAR blue;
      UCHAR alpha;
   } col;
} NICEPIXEL32;

static __inline UCHAR
Clamp8(ULONG val)
{
   return (val > 255) ? 255 : val;
}

BOOLEAN
DIB_32BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
                     RECTL* SourceRect, CLIPOBJ* ClipRegion,
                     XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
{
   INT Rows, Cols, SrcX, SrcY;
   register PULONG Dst;
   ULONG DstDelta;
   BLENDFUNCTION BlendFunc;
   register NICEPIXEL32 DstPixel, SrcPixel;
   UCHAR Alpha, SrcBpp;

   DPRINT("DIB_32BPP_AlphaBlend: srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
          SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
          DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);

   ASSERT(DestRect->bottom - DestRect->top == SourceRect->bottom - SourceRect->top &&
          DestRect->right - DestRect->left == SourceRect->right - SourceRect->left);

   BlendFunc = BlendObj->BlendFunction;
   if (BlendFunc.BlendOp != AC_SRC_OVER)
   {
      DPRINT1("BlendOp != AC_SRC_OVER\n");
      return FALSE;
   }
   if (BlendFunc.BlendFlags != 0)
   {
      DPRINT1("BlendFlags != 0\n");
      return FALSE;
   }
   if ((BlendFunc.AlphaFormat & ~AC_SRC_ALPHA) != 0)
   {
      DPRINT1("Unsupported AlphaFormat (0x%x)\n", BlendFunc.AlphaFormat);
      return FALSE;
   }
   if ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0 &&
       BitsPerFormat(Source->iBitmapFormat) != 32)
   {
      DPRINT1("Source bitmap must be 32bpp when AC_SRC_ALPHA is set\n");
      return FALSE;
   }

   Dst = (PULONG)((ULONG_PTR)Dest->pvScan0 + (DestRect->top * Dest->lDelta) +
                             (DestRect->left << 2));
   DstDelta = Dest->lDelta - ((DestRect->right - DestRect->left) << 2);
   SrcBpp = BitsPerFormat(Source->iBitmapFormat);

   Rows = DestRect->bottom - DestRect->top;
   SrcY = SourceRect->top;
   while (--Rows >= 0)
   {
      Cols = DestRect->right - DestRect->left;
      SrcX = SourceRect->left;
      while (--Cols >= 0)
      {
         SrcPixel.ul = DIB_GetSource(Source, SrcX++, SrcY, ColorTranslation);
         SrcPixel.col.red = SrcPixel.col.red * BlendFunc.SourceConstantAlpha / 255;
         SrcPixel.col.green = SrcPixel.col.green * BlendFunc.SourceConstantAlpha / 255;
         SrcPixel.col.blue = SrcPixel.col.blue * BlendFunc.SourceConstantAlpha / 255;
         SrcPixel.col.alpha = (SrcBpp == 32) ? (SrcPixel.col.alpha * BlendFunc.SourceConstantAlpha / 255) : BlendFunc.SourceConstantAlpha;

         Alpha = ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0) ?
                 SrcPixel.col.alpha : BlendFunc.SourceConstantAlpha;

         DstPixel.ul = *Dst;
         DstPixel.col.red = Clamp8(DstPixel.col.red * (255 - Alpha) / 255 + SrcPixel.col.red);
         DstPixel.col.green = Clamp8(DstPixel.col.green * (255 - Alpha) / 255 + SrcPixel.col.green);
         DstPixel.col.blue = Clamp8(DstPixel.col.blue * (255 - Alpha) / 255 + SrcPixel.col.blue);
         DstPixel.col.alpha = Clamp8(DstPixel.col.alpha * (255 - Alpha) / 255 + SrcPixel.col.alpha);
         *Dst++ = DstPixel.ul;
      }
      Dst = (PULONG)((ULONG_PTR)Dst + DstDelta);
      SrcY++;
   }

   return TRUE;
}

/* EOF */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?