📄 pixels.cpp
字号:
// Pixels.cpp : Implementation of CPixels
#include "stdafx.h"
#include "Picture.h"
#include "Pixels.h"
#include "Bitmap.h"
#include "Pixel.h"
/////////////////////////////////////////////////////////////////////////////
// CPixels
typedef CComEnum<IEnumVARIANT, &IID_IEnumVARIANT, VARIANT,
_Copy<VARIANT> > VarArrEnum;
STDMETHODIMP CPixels::get__NewEnum(IUnknown **pVal)
{
// Create an array of VT_DISPATCH for each pixel
HRESULT hr;
long height = m_pBitmap->m_height;
long width = m_pBitmap->m_width;
long iVariant = 0;
VARIANT* pVars = new VARIANT[width*height];
if (pVars == NULL) return E_OUTOFMEMORY;
// Create a pixel in a VT_DISPATCH for each pixel of the array
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
// Create this pixel object
VariantInit(&pVars[iVariant]);
pVars[iVariant].vt = VT_DISPATCH;
hr = get_Pixel(x, y,
(IPixel**) &pVars[iVariant++].pdispVal);
// Abort creation if anything failed
if (FAILED(hr))
{
delete pVars;
return hr;
}
}
}
// Create a pixel for each of the bitmap and add it to the
// collection
CComObject<VarArrEnum>* pPixelsColl;
hr = CComObject<VarArrEnum>::CreateInstance(&pPixelsColl);
if (SUCCEEDED(hr))
{
if (SUCCEEDED(hr = pPixelsColl->Init(
pVars, pVars + (width*height), NULL, AtlFlagCopy)))
{
hr = pPixelsColl->QueryInterface(pVal);
}
}
// Free our local copy of the VARIANT array
delete pVars;
return hr;
}
STDMETHODIMP CPixels::get_Count(long *pVal)
{
// Return the number of pixels in this bitmap
*pVal = m_pBitmap->m_width * m_pBitmap->m_height;
return S_OK;
}
STDMETHODIMP CPixels::get_Color(long x, long y, baseColors *pVal)
{
// Get a pixel color
HRESULT hr;
if (SUCCEEDED(hr = m_pBitmap->TestCoordinates(x, y)))
{
// Merge the 4 bits into the pixel's byte
DWORD dwOffset = y * CBROW(m_pBitmap->m_width) + x / 2;
if (x % 2)
*pVal = (baseColors)
(m_pBitmap->m_bitmap[dwOffset] & 0x0F);
else
*pVal = (baseColors)
((m_pBitmap->m_bitmap[dwOffset] & 0xF0) >> 4);
}
return S_OK;
}
STDMETHODIMP CPixels::put_Color(long x, long y, baseColors newVal)
{
// Change a pixel color
HRESULT hr = S_OK;
if (FAILED(hr = m_pBitmap->TestCoordinates(x, y)))
{
if (VARIANT_TRUE == m_pBitmap->m_bAutoSize)
{
// If we are in AutoSizeed mode, update bitmap size
hr = m_pBitmap->UpdateSize(
(x>=m_pBitmap->m_width ? x+1 : m_pBitmap->m_width),
(y>=m_pBitmap->m_height ? y+1 : m_pBitmap->m_height));
}
else return hr;
}
// Calculate the offset in the bitmap data where to place the
// pixel and set the baseColor into the existing byte
DWORD dwOffset = y * CBROW(m_pBitmap->m_width) + x / 2;
if (x % 2)
m_pBitmap->m_bitmap[dwOffset] =
(m_pBitmap->m_bitmap[dwOffset] & 0xF0) | newVal;
else
m_pBitmap->m_bitmap[dwOffset] =
(m_pBitmap->m_bitmap[dwOffset] & 0x0F) | (newVal << 4);
return S_OK;
}
STDMETHODIMP CPixels::get_Pixel(long x, long y, IPixel **pVal)
{
// Get a pixel pointer from coordinates
HRESULT hr;
CComObject<CPixel>* pPixel;
if (SUCCEEDED(hr=m_pBitmap->TestCoordinates(x,y)) &&
SUCCEEDED(hr=CComObject<CPixel>::CreateInstance(&pPixel)))
{
// Set pixel properties
pPixel->m_row = x;
pPixel->m_col = y;
if (SUCCEEDED(hr=get_Color(x, y, &pPixel->m_color)))
{
// Set the pixel in read-only
pPixel->m_bReadOnly = true;
// Return the object
hr = pPixel->QueryInterface(pVal);
}
}
return hr;
}
STDMETHODIMP CPixels::put_Pixel(long x, long y, IPixel *newVal)
{
// Set the pixel color at the specified coordinates
HRESULT hr;
baseColors color;
if (SUCCEEDED(hr = m_pBitmap->TestCoordinates(x, y)) &&
SUCCEEDED(hr = newVal->get_Color(&color)) )
hr = put_Color(x, y, color);
return hr;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -