Download Source Code

CWinImg Class Reference

Provides image manipulation, JPEG, PNG, BMP, WMF, EMF, etc... More...

#include <WinImg.h>

Collaboration diagram for CWinImg:

Collaboration graph
[legend]
List of all members.

Public Member Functions

BOOL OpenUserImage (HWND hOwner=NULL, LPCTSTR pTitle="Open Image", LPCTSTR pDefaultExt="jpg")
 Opens a user specified image.
BOOL SaveUserImage (HWND hOwner=NULL, LPCTSTR pFile=NULL, LPCTSTR pTitle="Open Image", LPCTSTR pDefaultExt="jpg")
 Save image to user specified file.
BOOL GetUserImage (LPSTR pFile, HWND hOwner=NULL, BOOL bOpen=TRUE, LPCTSTR pTitle="Open Image", LPCTSTR pDefaultExt="jpg")
 Opens a user image.
DWORD GetImageSize ()
 Returns the size, in bytes, of the raw image buffer.
DWORD Encode (LPBYTE pBuf, DWORD dwSize, LPCTSTR pType)
 Encodes the specified file to a buffer.
BOOL Tile (HDC hDC, LPRECT pRect)
 Tiles the image into the specified device context.
BOOL AlphaDelete ()
 Deletes the alpha layer.
BOOL HasAlpha ()
 Returns non-zero if the image has an alpha layer.
BOOL ToClipboard (HWND hWnd)
 Copies the image to the clipboard.
BOOL FromClipboard (HWND hWnd)
 Grabs an image from the clipboard.
BOOL DecreaseBpp (DWORD bpp, bool bErrorDiffusion, RGBQUAD *ppal=NULL)
 Decreases the bits per pixel used to represent the image.
BOOL IncreaseBpp (DWORD bpp)
 Increases the bits per pixel used to represent the image.
BYTE GetNearestIndex (RGBQUAD rgb)
 Gets the nearest value from the palette to the specified color.
BOOL SetTransIndex (long index)
 Set the palette index of the transparent color.
long GetTransIndex ()
 Returns the palette index of the transparent color.
BOOL SetPixel (long x, long y, RGBQUAD rgb)
 Sets the specified pixel color value.
BOOL SetPixel (long x, long y, COLORREF rgb)
 Sets the specified pixel color value.
RGBQUAD GetPixel (long x, long y)
 Returns the color of the specified pixel.
COLORREF GetRgbPixel (long x, long y)
 Returns the color of the specified pixel.
DWORD GetBpp ()
 Returns the number of bits per pixel used to represent the image.
RGBQUAD GetTransColor ()
 Returns the RGB value of the current transparent color.
BOOL SetTransColor (RGBQUAD rgb)
 Sets the transparent color.
BOOL IsTransparent ()
 Returns non-zero if transparency is enabled.
BOOL Resample (LPRECT pRect, int fast=1)
 Resamples the image to the new size.
BOOL Copy (CWinImg *pImg)
 Copies the specified image.
BOOL Create (long w, long h, long bpp=24)
 Creates an image of the specified size and color depth.
DWORD GetEffWidth ()
 Returns the scan width of a row of image data.
LPBYTE GetBits ()
 Returns a pointer to the raw image buffer.
BOOL LoadFromResource (LPCTSTR pResource, LPCTSTR pResType, LPCTSTR pImgType, HMODULE hModule=NULL)
 Loads an image from a resource.
long Histogram (long *red, long *green=0, long *blue=0, long *gray=0, long colorspace=0)
 Generates a historgram for the current image.
BOOL Dilate (long Ksize=2)
 Dialates the image.
BOOL Erode (long Ksize=2)
 Erodes the image.
BOOL Light (long level, long contrast=100)
 Lightens the image.
BOOL Colorize (BYTE hue, BYTE sat)
 Colorizes the image.
BOOL Threshold (BYTE level)
 Thresholds the image.
BOOL ShiftRGB (long r, long g, long b)
 Changes the RGB component levels.
BOOL Gamma (float gamma)
 Gamma correction effect.
BOOL Median (long Ksize=3)
 Median image effect.
BOOL Noise (long level)
 Adds noise to the image.
BOOL Crop (long left, long top, long right, long bottom, CWinImg *pDst=NULL)
 Crops an area of the image.
BOOL Dither (long method=0)
 Dithers the image.
BOOL Resample (long newx, long newy, int fast=1)
 Resamples the image.
BOOL Rotate (float angle, CWinImg *pDst=NULL)
 Rotates the image by the specified ammount.
BOOL Negative ()
 Generates negative for image.
BOOL GrayScale ()
 Converts image to gray scale.
BOOL Flip ()
 Flips the current image vertically.
BOOL Mirror ()
 Mirrors image horizontally.
HICON CreateIcon (long w, long h, COLORREF bck=RGB(255, 255, 255), HDC hDC=NULL)
 Creates an icon from the image.
BOOL SetQuality (DWORD q)
 Sets the quality level for the JPEG encoder.
BOOL CreateFromHBITMAP (HBITMAP hBmp)
 Creates an image from an HBITMAP handle.
BOOL IsValid ()
 Returns non-zero if there is a valid image.
long GetHeight ()
 Returns the image height.
long GetWidth ()
 Returns the image width.
BOOL Draw (HDC hDC, LPRECT pRect, BOOL bStretch=TRUE)
 Draws the image to the specified device context.
LPCTSTR GetLastError ()
 Returns a string pointer describing the last error.
BOOL Decode (LPBYTE buf, DWORD size, LPCTSTR pType=NULL)
 Decodes an image from a memory buffer.
void ReleaseEncodeMemory ()
 Releases temporary memory used by CxImage when encoding an image to memory.
BOOL InitEncodeJpg (long lWidth, long lHeight, long lQuality)
DWORD EncodeJpg (LPBYTE pSrc, long lWidth, long lHeight, LPBYTE pDst, DWORD dwDst, long lQuality)
 Encodes the specified image buffer as a JPEG in memory.
void ReleaseEncodeJpg ()
 Releases memory allocated by InitEncodeJpg().
BOOL Save (LPCTSTR pFile, LPCTSTR pType=NULL)
 Saves encoded image to disk.
BOOL Load (LPCTSTR pFile, LPCTSTR pType=NULL)
 Loads the specified file from disk.
void Destroy ()
 Releases the image and all associated resources.
 CWinImg ()
 Default constructor.
virtual ~CWinImg ()
 Destructor.
LPCTSTR GetFileName ()
 Returns the current image filename if available.
void SetTransparency (BOOL b)
 Enables / disables transparency.
BOOL GetTransparency ()
 Returns non-zero if transparency is enabled.
void SetTransparentColor (COLORREF rgb)
 Sets the transparent color.
COLORREF GetTransparentColor ()
 Returns the transparent color value.
 operator LPRECT ()
 Returns the size and position of the image.
LPRECT GetRect ()
 Returns the size and position of the image.
LPBYTE GetEncBuffer ()
 Returns a pointer to the internal encoded image buffer.
long GetEncBufferSize ()
 Returns the size of the internal encoded image buffer.
HBITMAP CreateHBITMAP (HDC hDC=NULL)
 Creates HBITMAP handle from the current image.
 operator HBITMAP ()
 Creates HBITMAP handle from the current image.
LPBYTE SwapRB (LPBYTE pBuf, UINT uLen)
 Swaps the specified buffer values from RGB to BGR or vice versa.

Static Public Member Functions

static DWORD GetFileType (LPCTSTR pFile)
 Examines the file extension and returns the image type code.

Detailed Description

Provides image manipulation, JPEG, PNG, BMP, WMF, EMF, etc...

Provides image encoding/decoding for JPEG, PNG, BMP, WMF, EMF, etc... This functionality is primarily achieved by encapsulating the CxImage implementation. You will need to download the CxImage library from http://www.xdp.it/cximage.htm. Many thanks to Davide Pizzolato for providing this library free of charge. If you get the chance, please stop in and give Davide a million dollars.


Member Function Documentation

BOOL CWinImg::Colorize BYTE  hue,
BYTE  sat
 

Colorizes the image.

Parameters:
[in] hue - Hue
[in] sat - Saturation
Returns:
Non-zero if success
See also:

BOOL CWinImg::Copy CWinImg pImg  ) 
 

Copies the specified image.

Parameters:
[in] pImg - Source image to copy
Returns:
Non-zero if success
See also:

BOOL CWinImg::Create long  w,
long  h,
long  bpp = 24
 

Creates an image of the specified size and color depth.

Parameters:
[in] w - Width of new image
[in] h - Height of new image
[in] bpp - Color depth of new image in bits per pixel
Returns:
Non-zero if success
See also:

BOOL CWinImg::CreateFromHBITMAP HBITMAP  hBmp  ) 
 

Creates an image from an HBITMAP handle.

Parameters:
[in] hBmp - HBITMAP handle
Returns:
Non-zero if success
See also:

HBITMAP CWinImg::CreateHBITMAP HDC  hDC = NULL  ) 
 

Creates HBITMAP handle from the current image.

You must pass the handle to DeleteObject() when it is no longer needed.

Returns:
Handle to bitmap or NULL if error.
See also:

HICON CWinImg::CreateIcon long  w,
long  h,
COLORREF  bck = RGB(255, 255, 255),
HDC  hDC = NULL
 

Creates an icon from the image.

Parameters:
[in] w - New icon width
[in] h - New icon height
[in] bck - Transparent color used to generate mask.
[in] hDC - Reference device context, NULL to use the desktop.
Returns:
Handle to new icon or NULL if failure
See also:

BOOL CWinImg::Crop long  left,
long  top,
long  right,
long  bottom,
CWinImg pDst = NULL
 

Crops an area of the image.

Parameters:
[in] left - Horizontal offset of upper left corner
[in] top - Vertical offset of upper left corner
[in] right - Horizontal offset of lower right corner
[in] bottom - Vertical offset of lower right corner
[in] pDst - CWinDc object that receives the cropped image. NULL to replace current image.
Returns:
Non-zero if success
See also:

BOOL CWinImg::Decode LPBYTE  buf,
DWORD  size,
LPCTSTR  pType = NULL
 

Decodes an image from a memory buffer.

Parameters:
[in] buf - Pointer to buffer containing the compressed image.
[in] size - Number of bytes in buf.
[in] pType - Image type, "jpg", "png", etc...
Returns:
Non-zero if success
See also:

BOOL CWinImg::DecreaseBpp DWORD  bpp,
bool  bErrorDiffusion,
RGBQUAD *  ppal = NULL
 

Decreases the bits per pixel used to represent the image.

Parameters:
[in] bpp - Desired bits per pixel
[in] bErrorDiffusion - Non-zero to enable error diffusion.
[in] ppal - Optional palette to use.
Returns:
Non-zero if success
See also:

BOOL CWinImg::Dilate long  Ksize = 2  ) 
 

Dialates the image.

Parameters:
[in] Ksize - Dialation weight
Returns:
Non-zero if success
See also:

BOOL CWinImg::Dither long  method = 0  ) 
 

Dithers the image.

Parameters:
[in] method - Dither method ( 0, 1 )
Returns:
Non-zero if success
See also:

BOOL CWinImg::Draw HDC  hDC,
LPRECT  pRect,
BOOL  bStretch = TRUE
 

Draws the image to the specified device context.

Parameters:
[in] hDC - Handle to device context
[in] pRect - Destination rectangle for image
[in] bStretch - Non-zero to stretch to pRect
Returns:
Non-zero if success
See also:

DWORD CWinImg::Encode LPBYTE  pBuf,
DWORD  dwSize,
LPCTSTR  pType
 

Encodes the specified file to a buffer.

Parameters:
[in] pBuf - Pointer to buffer that receives the encoded image
[in] dwSize - Size of the buffer in pBuf
[in] pType - File extension specifying type of encoding. "jpg", "png", etc...
pBuf must be large enough to hold the encoded image.

Returns:
Number of bytes written to pBuf
See also:

DWORD CWinImg::EncodeJpg LPBYTE  pSrc,
long  lWidth,
long  lHeight,
LPBYTE  pDst,
DWORD  dwDst,
long  lQuality
 

Encodes the specified image buffer as a JPEG in memory.

Parameters:
[in] pSrc - Uncompressed source image
[in] lWidth - Width of image
[in] lHeight - Height of image
[in] pDst - Receives JPEG image
[in] dwDst - Size of buffer in pDst
[in] lQuality - JPEG image quality
I couldn't figure out a way to use CxImage to encode a buffer in memory without a memcpy and didn't want to have to depend on a modified library. So this function does not use CxImage to encode.

The image in pSrc must be 24-bit BGR encoded. Scan lines must be DWORD aligned, but that's typical.

The buffer in pDst must be large enough to hold the image or the function fails returning zero. So it is not possible to figure out the required buffer size before calling. I would just make the image at least as large as the image buffer.

This function also reuses the same JPEG compression structures if subsequent calls are made with the same size image

Returns:
Number of bytes written to pDst, or zero if failure.
See also:
InitEncodeJpg(), ReleaseEncodeJpg()

BOOL CWinImg::Erode long  Ksize = 2  ) 
 

Erodes the image.

Parameters:
[in] Ksize - Erosion weight
Returns:
Non-zero if success
See also:

BOOL CWinImg::Flip  ) 
 

Flips the current image vertically.

Returns:
Non-zero if success

BOOL CWinImg::FromClipboard HWND  hWnd  ) 
 

Grabs an image from the clipboard.

Parameters:
[in] hWnd - Non-zero if success
Returns:
Non-zero if success
See also:

BOOL CWinImg::Gamma float  gamma  ) 
 

Gamma correction effect.

Parameters:
[in] gamma - Gamma correction value
Returns:
Non-zero if success
See also:

LPBYTE CWinImg::GetEncBuffer  )  [inline]
 

Returns a pointer to the internal encoded image buffer.

This buffer is only valid after a call to Encode()

Returns:
Pointer to the internal encoded image buffer
See also:

long CWinImg::GetEncBufferSize  )  [inline]
 

Returns the size of the internal encoded image buffer.

This buffer is only valid after a call to Encode()

Returns:
Size of the internal encoded image buffer
See also:

DWORD CWinImg::GetFileType LPCTSTR  pFile  )  [static]
 

Examines the file extension and returns the image type code.

Parameters:
[in] pFile - Filename to examine
Returns:
Image type code
See also:

BYTE CWinImg::GetNearestIndex RGBQUAD  rgb  ) 
 

Gets the nearest value from the palette to the specified color.

Parameters:
[in] rgb - Color value
Returns:
Returns the palette index of the nearest color
See also:

RGBQUAD CWinImg::GetPixel long  x,
long  y
 

Returns the color of the specified pixel.

Parameters:
[in] x - Horizontal offset of pixel
[in] y - Vertical offset of pixel
Returns:
Color value of pixel
See also:

COLORREF CWinImg::GetRgbPixel long  x,
long  y
[inline]
 

Returns the color of the specified pixel.

Parameters:
[in] x - Horizontal offset of pixel
[in] y - Vertical offset of pixel
Returns:
Color value of pixel
See also:

BOOL CWinImg::GetUserImage LPSTR  pFile,
HWND  hOwner = NULL,
BOOL  bOpen = TRUE,
LPCTSTR  pTitle = "Open Image",
LPCTSTR  pDefaultExt = "jpg"
 

Opens a user image.

Parameters:
[in] pFile - Receives the user filename.
[in] hOwner - Handle to parent window
[in] bOpen - Non-zero for open dialog, zero to display the save dialog.
[in] pTitle - Title of the dialog box
[in] pDefaultExt - Default image extension
Returns:
Number of bytes written to pBuf
See also:

BOOL CWinImg::GrayScale  ) 
 

Converts image to gray scale.

Returns:
Non-zero if success

long CWinImg::Histogram long *  red,
long *  green = 0,
long *  blue = 0,
long *  gray = 0,
long  colorspace = 0
 

Generates a historgram for the current image.

Parameters:
[out] red - Receives the red histogram
[out] green - Receives the green histogram
[out] blue - Receives the blue histogram
[out] gray - Receives the gray scale histogram
[in] colorspace - Colorspace
Returns:
Length of histogram
See also:

BOOL CWinImg::IncreaseBpp DWORD  bpp  ) 
 

Increases the bits per pixel used to represent the image.

Parameters:
[in] bpp - Desired bits per pixel
Returns:
Non-zero if success
See also:

BOOL CWinImg::InitEncodeJpg long  lWidth,
long  lHeight,
long  lQuality
 

Parameters:
[in] lWidth - Image width
[in] lHeight - Image height
[in] lQuality - JPEG encoding quality
This function is called automatically by EncodeJpg(), so it is not necessary to call this function directly.

Returns:
Non-zero if success
See also:
EncodeJpg(), ReleaseEncodeJpg()

BOOL CWinImg::Light long  level,
long  contrast = 100
 

Lightens the image.

Parameters:
[in] level - Lighten level
[in] contrast - Contrast
Returns:
Non-zero if success
See also:

BOOL CWinImg::Load LPCTSTR  pFile,
LPCTSTR  pType = NULL
 

Loads the specified file from disk.

Parameters:
[in] pFile - Filename
[in] pType - Image type "jpg", "png", etc... If pType is NULL, the image type is determined by examining the file extension in pFile.
Returns:
Non-zero if success
See also:

BOOL CWinImg::LoadFromResource LPCTSTR  pResource,
LPCTSTR  pResType,
LPCTSTR  pImgType,
HMODULE  hModule = NULL
 

Loads an image from a resource.

Parameters:
[in] pResource - Resource name
[in] pResType - Resource type
[in] pImgType - Image type, "jpg", "png", etc...
[in] hModule - Handle to module containing the image resource
Returns:
Non-zero if success
See also:

BOOL CWinImg::Median long  Ksize = 3  ) 
 

Median image effect.

Parameters:
[in] Ksize - Median image weight
Returns:
Non-zero if success
See also:

BOOL CWinImg::Mirror  ) 
 

Mirrors image horizontally.

Returns:
Non-zero if success

BOOL CWinImg::Negative  ) 
 

Generates negative for image.

Returns:
Non-zero if success

BOOL CWinImg::Noise long  level  ) 
 

Adds noise to the image.

Parameters:
[in] level - Noise level
Returns:
Non-zero if success
See also:

BOOL CWinImg::OpenUserImage HWND  hOwner = NULL,
LPCTSTR  pTitle = "Open Image",
LPCTSTR  pDefaultExt = "jpg"
 

Opens a user specified image.

Parameters:
[in] hOwner - Handle to parent window
[in] pTitle - Title of the dialog box
[in] pDefaultExt - Default image extension
Returns:
Non-zero if image opened.
See also:

CWinImg::operator HBITMAP  )  [inline]
 

Creates HBITMAP handle from the current image.

You must pass the handle to DeleteObject() when it is no longer needed.

Returns:
Handle to bitmap or NULL if error.
See also:

void CWinImg::ReleaseEncodeJpg  ) 
 

Releases memory allocated by InitEncodeJpg().

This function will be called when this CWinDc object is destroyed, so it is not necessary to call this function.

See also:
InitEncodeJpg(), EncodeJpg()

BOOL CWinImg::Resample long  newx,
long  newy,
int  fast = 1
 

Resamples the image.

Parameters:
[in] newx - New horizontal image size
[in] newy - New vertical image size
[in] fast - Resampling algorithm to use ( 0, 1 )
Returns:
Non-zero if success
See also:

BOOL CWinImg::Resample LPRECT  pRect,
int  fast = 1
 

Resamples the image to the new size.

Parameters:
[in] pRect - New size for image
[in] fast - Resampling algorithm to use ( 0, 1 )
Returns:
Non-zero if success
See also:

BOOL CWinImg::Rotate float  angle,
CWinImg pDst = NULL
 

Rotates the image by the specified ammount.

Parameters:
[in] angle - Rotation angle in degrees
[in] pDst - CWinDc object that receives rotated image. NULL to replace current image.
Returns:
Non-zero if success
See also:

BOOL CWinImg::Save LPCTSTR  pFile,
LPCTSTR  pType = NULL
 

Saves encoded image to disk.

Parameters:
[in] pFile - Filename
[in] pType - Image type "jpg", "png", etc... If pType is NULL, the image type is determined by examining the file extension in pFile.
Returns:
Non-zero if success.
See also:

BOOL CWinImg::SaveUserImage HWND  hOwner = NULL,
LPCTSTR  pFile = NULL,
LPCTSTR  pTitle = "Open Image",
LPCTSTR  pDefaultExt = "jpg"
 

Save image to user specified file.

Parameters:
[in] hOwner - Handle to parent window
[in] pFile - Initial file name
[in] pTitle - Title of the dialog box
[in] pDefaultExt - Default image extension
Returns:
Non-zero if image saved
See also:

BOOL CWinImg::SetPixel long  x,
long  y,
COLORREF  rgb
[inline]
 

Sets the specified pixel color value.

Parameters:
[in] x - Horizontal offset of pixel
[in] y - Vertical offset of pixel
[in] rgb - New color value for pixel
Returns:
Non-zero if success
See also:

BOOL CWinImg::SetPixel long  x,
long  y,
RGBQUAD  rgb
 

Sets the specified pixel color value.

Parameters:
[in] x - Horizontal offset of pixel
[in] y - Vertical offset of pixel
[in] rgb - New color value for pixel
Returns:
Non-zero if success
See also:

BOOL CWinImg::SetQuality DWORD  q  ) 
 

Sets the quality level for the JPEG encoder.

Parameters:
[in] q - JPEG quality level
Returns:
Non-zero if success
See also:

BOOL CWinImg::SetTransColor RGBQUAD  rgb  ) 
 

Sets the transparent color.

Parameters:
[in] rgb - Transparent color value
Returns:
Non-zero if success
See also:

BOOL CWinImg::SetTransIndex long  index  ) 
 

Set the palette index of the transparent color.

Parameters:
[in] index - Palette index of transparent color
Returns:
Non-zero if success
See also:

void CWinImg::SetTransparency BOOL  b  )  [inline]
 

Enables / disables transparency.

Parameters:
[in] b - Non-zero to enable transparency. Zero disables transparency.

void CWinImg::SetTransparentColor COLORREF  rgb  )  [inline]
 

Sets the transparent color.

Parameters:
[in] rgb - Transparent color

BOOL CWinImg::ShiftRGB long  r,
long  g,
long  b
 

Changes the RGB component levels.

Parameters:
[in] r - Red component level
[in] g - Green component level
[in] b - Blue component level
Returns:
Non-zero if success
See also:

LPBYTE CWinImg::SwapRB LPBYTE  pBuf,
UINT  uLen
[inline]
 

Swaps the specified buffer values from RGB to BGR or vice versa.

Parameters:
[in] pBuf - Input color values
[in] uLen - Length of color values
Returns:
The value in pBuf

BOOL CWinImg::Threshold BYTE  level  ) 
 

Thresholds the image.

Parameters:
[in] level - Threshold level
Converts the image to a monochrome image, converting all values above the threshold value to white. All values below the threshold are converted to black.

Returns:
Non-zero if success
See also:

BOOL CWinImg::Tile HDC  hDC,
LPRECT  pRect
 

Tiles the image into the specified device context.

Parameters:
[in] hDC - Handle to device context
[in] pRect - Portion of hDC to tile
Returns:
Non-zero if success
See also:

BOOL CWinImg::ToClipboard HWND  hWnd  ) 
 

Copies the image to the clipboard.

Parameters:
[in] hWnd - Window handle
Returns:
Non-zero if success
See also:


The documentation for this class was generated from the following files:
  • inc/WinImg.h
  • WinImg.cpp
Copyright Robert Umbehant
This documentation is covered by the LGPL