2011-10-08 106 views
0

我希望能够绘制形状并用各种填充图案(对角线,点点等)进行填充。用CImg中的图案填充形状

CImg库包含用轮廓绘制具有任意线型的各种形状的函数。但我没有看到关于填充模式的任何内容。

我认为这可以使用位运算符或数学运算符将模式掩盖到实体图像上,但我希望看到执行此操作的具体代码。

回答

0

是的,多边形填充图案可以通过首先以纯色绘制多边形然后使用带有预加载的黑白图案图像的& =操作符来实现。

// preload several pattern images (black and white versions of the desired fill patterns) 
CImg<unsigned char> *fillPatternImages[ NumFillPatterns ] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; 
fillPatternImages[ 0 ] = new CImg<unsigned char>("256x256_bw_dotted_fill.png"); 
... etc. for all patterns you want to use 

// create an empty image 
CImg<unsigned char> image(256, 256, 1, 4, 0); 

// draw the polygon (or in the case of my code, any number of polygons) on the image in a solid color 
if (nShapeType == SHPT_POLYGON && fillPattern != FILL_PATTERN_NONE) 
{ 
    for(int i = 0 ; i < nShapeCount ; i++) 
    { 
    SHPObject *psShape; 
    psShape = SHPReadObject(hSHP, panHits[ i ]); 

    for (int part = 0 ; part < psShape->nParts ; part++) 
    { 
     int numPoints; 
     if (part < (psShape->nParts - 1)) 
     { 
     numPoints = psShape->panPartStart[ part + 1 ] - psShape->panPartStart[ part ]; 
     } 
     else 
     { 
     numPoints = psShape->nVertices - psShape->panPartStart[ part ]; 
     } 
     CImg<int> pointImage(numPoints, 2, 1, 1, 0); 
     int s = psShape->panPartStart[ part ]; 
     for (int p = 0 ; p < numPoints ; p++) 
     { 
     int screenX; 
     int screenY; 
     GetTileXYFromMercatorLonLat((float)psShape->padfX[ s + p ], (float)psShape->padfY[ s + p ], x, y, z, &screenX, &screenY); 
      pointImage(p, 0) = screenX; 
      pointImage(p, 1) = screenY; 
     } 
     image.draw_polygon(pointImage, fillColor); 
    } 

    SHPDestroyObject(psShape); 
    } 
} 

// to achieve a non-solid pattern, & the image with a pre-loaded pattern image 
if (fillPattern > -1) 
{ 
    image &= *fillPatternImages[ fillPattern ]; 
}