gimage.h
来自「一个由Mike Gashler完成的机器学习方面的includes neural」· C头文件 代码 · 共 481 行
H
481 行
/* Copyright (C) 2006, Mike Gashler This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. see http://www.gnu.org/copyleft/lesser.html*/#ifndef __GIMAGE_H__#define __GIMAGE_H__#include <stdio.h>#include "GMacros.h"class GBezier;class GIntArray;// Note: This is a 4-channel color of the form 0xAARRGGBB.// 0 is completely transparent.// 0xff000000 is opaque black.// 0xffffffff is opaque white.typedef unsigned int GColor;#define gBlue(c) ((c) & 0xff)#define gGreen(c) (((c) >> 8) & 0xff)#define gRed(c) (((c) >> 16) & 0xff)#define gAlpha(c) (((c) >> 24) & 0xff)#define gGray(c) (77 * gRed(c) + 150 * gGreen(c) + 29 * gBlue(c))// hue, saturation, and value all range from 0 to 1void GColorToHSV(GColor c, float* pHue, float* pSaturation, float* pValue);// alpha ranges from 0 to 255. hue, saturation, and value range from 0 to 1GColor HSVToGColor(int alpha, float hue, float saturation, float value);inline int ClipChan(int n){ return MAX(0, MIN(255, n));}inline GColor gRGB(int r, int g, int b){ return ((b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 16) | 0xff000000);}inline GColor gARGB(int a, int r, int g, int b){ return ((b & 0xff) | ((g & 0xff) << 8) | ((r & 0xff) << 16) | ((a & 0xff) << 24));}inline GColor gFromGray(int gray){ gray = gray >> 8; return gRGB(gray, gray, gray);}inline GColor MixColors(GColor a, GColor b, int nRatio){ int n2 = 256 - nRatio; return gARGB( (gAlpha(a) * nRatio + gAlpha(b) * n2) >> 8, (gRed(a) * nRatio + gRed(b) * n2) >> 8, (gGreen(a) * nRatio + gGreen(b) * n2) >> 8, (gBlue(a) * nRatio + gBlue(b) * n2) >> 8 );}inline GColor GetSpectrumColor(float f){ int n = ((int)(f * 768)) % 768; int r = MIN(MAX(0, 512 - (n + n)) + MAX(0, n + n - 1024), 255); int g = MIN(MAX(0, 512 - (ABS(n - 256) << 1)), 255); int b = MIN(MAX(0, 512 - (ABS(n - 512) << 1)), 255); return gARGB(0xff, r, g, b);}inline GColor MultiplyBrightness(GColor c, float f){ return gARGB(gAlpha(c), ClipChan((int)(f * gRed(c))), ClipChan((int)(f * gGreen(c))), ClipChan((int)(f * gBlue(c))) );}struct GRect{ int x; int y; int w; int h; GRect() { } GRect(int _x, int _y, int _w, int _h) { x = _x; y = _y; w = _w; h = _h; } void Set(int _x, int _y, int _w, int _h) { x = _x; y = _y; w = _w; h = _h; } bool DoesInclude(int _x, int _y) { if(_x >= x && _y >= y && _x < x + w && _y < y + h) return true; else return false; }};struct GFloatRect{ float x, y, w, h; GFloatRect() { } GFloatRect(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } void Set(float _x, float _y, float _w, float _h) { x = _x; y = _y; w = _w; h = _h; } bool DoesInclude(float _x, float _y) { if(_x >= x && _y >= y && _x < x + w && _y < y + h) return true; else return false; }};// Represents an imageclass GImage{protected: GColor* m_pPixels; int m_nWidth; int m_nHeight; bool LoadPixMap(FILE* pFile, bool bTextData, bool bGrayScale); bool SavePixMap(FILE* pFile, bool bTextData, bool bGrayScale); void FloodFillRecurser(int nX, int nY, unsigned char nSrcR, unsigned char nSrcG, unsigned char nSrcB, unsigned char nDstR, unsigned char nDstG, unsigned char nDstB, int nTolerance);public: GImage(); virtual ~GImage(); // Load a file (determines the format from the extension--doesn't handle incorrect extensions) bool LoadFile(const char* szFilename); // Load the image from a PNG as raw data bool LoadPNGFile(const unsigned char* pRawData, int nBytes); // Load the image from a PNG file bool LoadPNGFile(const char* szFilename); // Load the image from a BMP file bool LoadBMPFile(const char* szFilename); // Load the image from a BMP stream bool LoadBMPFile(FILE* pFile); // Load the image from a BMP raw data bool LoadBMPFile(const unsigned char* pRawData, int nLen); // Save the image to a BMP file bool SaveBMPFile(const char* szFilename); // Save the image from a PPM file bool LoadPPMFile(const char* szFilename); // Save the image to a PPM file bool SavePPMFile(const char* szFilename); // Load the image from a PGM file bool LoadPGMFile(const char* szFilename); // Save the image to a PGM file bool SavePGMFile(const char* szFilename); // Save the image as a PNG to a stream bool SavePNGFile(FILE* pFile); // Save the image as a PNG to a file bool SavePNGFile(const char* szFilename); // Drawing Primitives inline void SetPixel(int nX, int nY, GColor color) { GAssert(nX >= 0 && nX < m_nWidth && nY >= 0 && nY < m_nHeight, "out of range"); m_pPixels[m_nWidth * nY + nX] = color; } // Get a pixel inline GColor GetPixel(int nX, int nY) const { GAssert(nX >= 0 && nX < m_nWidth && nY >= 0 && nY < m_nHeight, "out of range"); return m_pPixels[m_nWidth * nY + nX]; } // Get a pixel reference inline GColor* GetPixelRef(int nX, int nY) { GAssert(nX >= 0 && nX < m_nWidth && nY >= 0 && nY < m_nHeight, "out of range"); return &m_pPixels[m_nWidth * nY + nX]; } // Set a pixel (may be out of range of the image) void SafeSetPixel(int nX, int nY, GColor color); // Draw a translucent pixel void SoftSetPixel(int nX, int nY, GColor color, double dOpacity); // Get a pixel (may be out of range of the image) GColor SafeGetPixel(int nX, int nY) const; // Returns an interpolated pixel GColor InterpolatePixel(float dX, float dY); // Draw a line void DrawLine(int nX1, int nY1, int nX2, int nY2, GColor color); // Draw a line (may include parts outside the bounds of the image) void SafeDrawLine(int nX1, int nY1, int nX2, int nY2, GColor color); // Draw an anti-aliassed line void DrawLineAntiAlias(int nX1, int nY1, int nX2, int nY2, GColor color); // Draws a filled-in box void FillBox(int x, int y, int w, int h, GColor c); // Draw a hollow box void DrawBox(int nX1, int nY1, int nX2, int nY2, GColor color); // Draw a translucent box void SoftDrawBox(int nX1, int nY1, int nX2, int nY2, GColor color, double dOpacity); // Draw a circle void DrawCircle(int nX, int nY, float dRadius, GColor color); // Draw an ellipse void DrawEllipse(int nX, int nY, double dRadius, double dHeightToWidthRatio, GColor color); // Tolerant flood fill void FloodFill(int nX, int nY, GColor color, int nTolerance); // Tolerant boundary fill void BoundaryFill(int nX, int nY, unsigned char nBoundaryR, unsigned char nBoundaryG, unsigned char nBoundaryB, unsigned char nFillR, unsigned char nFillG, unsigned char nFillB, int nTolerance); // Draws a filled-in triangle void FillTriangle(int x1, int y1, int x2, int y2, int x3, int y3, GColor c); // Draws a polygon void DrawPolygon(int nPoints, int* pnPointArray, GColor color); // Determines how many pixels of width are required to print a line of text int MeasureHardTextWidth(int height, const char* szText, float width); // Counts the number of characters that can be printed in the given horizArea static int CountHardTextChars(int horizArea, int height, const char* szText, float width); // Draws some text (using a built-in hard-coded font) void DrawHardText(GRect* pRect, const char* szText, GColor col, float width); // Draw a line based on 3D coordinates and the specified camera void Draw3DLine(const struct Point3D* pA, const struct Point3D* pB, struct Transform* pCamera, GColor color); // Draw a bezier curve based on 3D coordinates and the specified camera void DrawBezier(GBezier* pCurve, GColor color, double dStart, double dEnd, double dStep, struct Transform* pCamera); // Fill the entire image with a single color void Clear(GColor color); // Flip the image horizontally void FlipHorizontally(); // Flip the image vertically void FlipVertically(); // Rotate the image around the specified point void Rotate(GImage* pSourceImage, int nX, int nY, double dAngle); // Erase the image and resize it void SetSize(int nWidth, int nHeight); // Scale the image void Scale(unsigned int nNewWidth, unsigned int nNewHeight); // Crop. You can crop bigger by using values outside the picture bool Crop(int nLeft, int nTop, int nRight, int nBottom); // Converts the image to gray scale void ConvertToGrayScale(); // Equalizes the color histogram void EqualizeColorSpread(); // Locally equalize the color histogram void LocallyEqualizeColorSpread(int nLocalSize, float fExtent = 1); // Blur the image void Blur(double dRadius); // Blurs by averaging uniformly over a square, plus optimized to work // fast even when the radius is big void QuickBlur(int nRadius); // Sharpen the image void Sharpen(double dFactor); // Inverts the pixels in the image void Invert(); // Inverts the pixels in a particular rect void InvertRect(GRect* pRect); // Finds edges and makes them glow void MakeEdgesGlow(float fThresh, int nThickness, int nOpacity, GColor color); void Convolve(GImage* pKernel); void ConvolveKernel(GImage* pKernel); void HorizDifferenceize(); void HorizSummize(); void SwapData(GImage* pSwapImage); // Returns the raw array of pixels that represent this image GColor* GetPixelArray() { return m_pPixels; } void CopyImage(GImage* pSourceImage); void CopyImage(GImage* pSourceImage, int nLeft, int nTop, int nRight, int nBottom); // Returns the width of the image in pixels int GetWidth() const { return m_nWidth; } // Returns the height of the image in pixels int GetHeight() const { return m_nHeight; } // Blit an image into this image. The source rect must be within the source image. // The dest area can be out of the dest image. The alpha channel is ignored. void Blit(int x, int y, GImage* pSource, GRect* pSourceRect); // Blit an image into this image. The source rect must be within the source image. // The dest area can be out of the dest image. Also performs alpha blending. void AlphaBlit(int x, int y, GImage* pSource, GRect* pSourceRect); // Analysis Tools void CreateBrightnessHistogram(GImage* pOutImage); // Munges the image. nStyle should be 0, 1, 2, or 3. Each value munges a different way. // fExtent should be between 0 and 1, where 0 doesn't change much and 1 totally munges it. // You're responsible to delete the munged image this returns GImage* Munge(int nStyle, float fExtent); // dRadians ranges from 0 to 2PI // fAmount ranges from 0 to about 4, but small values (like .1) seem to look best void MoveLight(double dRadians, float fAmount); // This uses a Gaussian to interpolate between the original image and a translated // image. The result appears as though the start point were stretched to the end point. void Stretch(int nXStart, int nYStart, int nXEnd, int nYEnd); // Make a string of text that would be difficult for non-humans to read void MakeCaptcha(const char* szText); // Generates a discrete kernel to approximate a Gaussian. nWidth is both the // width and height of the kernel. (Usually an odd number is desireable so there // is a bright center pixel.) fDepth is the value of the center of the kernel. // 1 <= fDepth <= 255. The values are computed such that the center of each edge // has a value of 1. void MakeGaussianKernel(int nWidth, float fDepth); // Values for dExtent range from 0 to 1. In most cases, a small value (like .1) // is desireable void HighPassFilter(double dExtent); // Computes the grascale histogram of the image, regresses two gaussians to fit // the histogram, and returns the intersection point in grayscale values. int ComputeOptimalGrayscaleThreshold(); // Thresholds the image (to black and white) at the specified grayscale value. // (0 <= nGrayscaleValue < 65281). (65281 = 255 * 256 + 1) void Threshold(int nGrayscaleValue); // Sets every pixel to the grayscale median of the neighborhood that fits within // the specified radius. (A radius of 1 will include only 4 neighbors. A radius // of 1.5 will include 8 neighbors.) void MedianFilter(float fRadius); // a basic morphological operator void Dialate(GImage* pStructuringElement); // A basic morphological operator void Erode(GImage* pStructuringElement); // if nPixels > 0, opens the image by the specified number of pixels // if nPixels < 0, closes the image by the specified number of pixels void Open(int nPixels); // This finds the biggest "nBlobs" connected component regions, and // makes a mask that enumerates each of the connected component, // and reports the number of pixels in each blob. nTolerance is // in grayscale values, so it ranges from 0 to 65280. pBlobSizes // can be NULL if the area is not needed. Returns the number of // connected components found. Any pixels with a value of cBackground // will be marked as part of region 0. int MakeConnectedComponentMask(GImage* pThat, int nTolerance, int nBlobs, int* pBlobSizes, bool bHack); // todo: remove bHack // This clusters pixels with all neighbors that have a similar grayscale // value (within "nTolerance") to form regions. The biggest region is // assumed to be the background and is made black. The next biggest // "nRegions" regions are made white. If bLabel is true, it will colorize // the regions, put cross-hairs on the center of mass, draw an arrow to // indicate orientation, and display the number of pixels in the region. void ThresholdByRegion(int nTolerance, int nRegions, bool bLabel); // Sets the alpha channel value for all pixels in the image void SetGlobalAlpha(int alpha); double ComputeMoment(double centerX, double centerY, double i, double j); void ComputeMeanAndOrientation(double* pMeanX, double* pMeanY, double* pRadians); void MakeGradientMagnitudeImage(const GImage* pIn); void ColorizeRegionMap(); // Determines whether the point (x, y) is inside the triangle // specified by (x0, y0), (x1, y1), (x2, y2). On any edge is considered // to be within the triangle static bool IsPointInside2DTriangle(float x, float y, float x0, float y0, float x1, float y1, float x2, float y2); // Given the three vertices of a triangle, this computes the weights for // each vertex that linearly interpolates to the point (x, y) static void Compute2DTriangleWeights(float* pW0, float* pW1, float* pW2, float x, float y, float x0, float y0, float x1, float y1, float x2, float y2); // Given three weights and three points, interpolate the point, and // interpolate the color at that point GColor InterpolateWithinTriangle(float w0, float w1, float w2, float x0, float y0, float x1, float y1, float x2, float y2);};#endif // __GIMAGE_H__
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?