private void applyStroke (Shape shape) {
Stroke stroke = getStroke ();
Transform t = getTransform ();
if (stroke! = null) {
if (stroke instanceof BasicStroke) {
BasicStroke currStroke = (BasicStroke) stroke;
float [] ff = currStroke.getDashArray ();
if (ff! = null) setdash (ff.length, ff, currStroke.getDashPhase ());
float width = (float) (currStroke.getLineWidth () * Math.max (t.getScaleX (), t.getScaleY ()));
if (width <0.7f) width = 0.7f;
setlinewidth (width);
}
else {
setlinewidth (1.0f);
Shape strokedShape = stroke.createStrokedShape (shape);
fill (strokedShape);
}
}
else
setlinewidth (1.0f);
}
Go
The code in general does not contain anything complicated, just a parsing of BasicStroke. If it is something else, then it can be represented in the form of a Shape and can be drawn like this. private void applyAlpha () {
float alpha = 1.0f;
Composite comp = getComposite ();
if (comp! = null && comp instanceof AlphaComposite) {
AlphaComposite alcomp = (AlphaComposite) comp;
alpha = alcomp.getAlpha ();
}
setopacity (alpha, true);
} Go private boolean applyStyles (Shape shape) {
Paint paint = getPaint ();
if (paint instanceof Color) {
Color cc = (Color) paint;
setrgbcolor (cc.getRed () / 255.0f, cc.getGreen () / 255.0f, cc.getBlue () / 255.0f);
if (cc.getAlpha ()! = 0 && cc.getAlpha ()! = 255) setopacity (cc.getAlpha () / 255.0f, true);
else applyAlpha ();
}
else if (paint! = null && shape! = null) {
applyAlpha ();
drawGradientAsBackground (shape, paint);
return false;
}
Composite comp = getComposite ();
if (comp! = null &&! (comp instanceof AlphaComposite) && shape! = null) {
drawCustomComposite (shape, comp);
return false;
}
return true;
} Go private void drawGradientAsBackground (Shape shape, Paint paint) {
GraphicsConfiguration conf = getDeviceConfiguration ();
PaintContext pctx = paint.createContext (
conf.getColorModel (), conf.getBounds (), shape.getBounds2D (),
getTransform (), getRenderingHints ());
Rectangle r = getTransform (). CreateTransformedShape (shape) .getBounds ();
Raster raster = pctx.getRaster (rx, ry, r.width, r.height);
ColorModel cmodel = pctx.getColorModel ();
BufferedImage bfi = new BufferedImage (cmodel, raster.createCompatibleWritableRaster (),
cmodel.isAlphaPremultiplied (), null);
bfi.setData (raster);
int [] argbBuf = getBuf (bfi, r.width, r.height);
if (argbBuf! = null) {
applyClip (shape);
drawImage (argbBuf, rx, ry, r.width, r.height, new AffineTransform ());
restoreClip ();
}
pctx.dispose ();
} Go private void drawCustomComposite (Shape shape, Composite comp) {
Rectangle r = getTransform (). CreateTransformedShape (shape) .getBounds ();
BufferedImage bfi = new BufferedImage (r.width, r.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) bfi.getGraphics ();
g2.setTransform (getTransform ());
g2.setComposite (comp);
g2.fill (shape);
g2.dispose ();
int [] argbBuf = getBuf (bfi, r.width, r.height);
if (argbBuf! = null) {
applyClip (shape);
drawImage (argbBuf, rx, ry, r.width, r.height, new AffineTransform ());
restoreClip ();
}
} Go
Finally we got to the most interesting. If for other cases the native implementation is essentially a single-line code, in the case of pictures this is not the case. First, here’s the code for drawing the image: public boolean drawImage (Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer) {
int [] argbBuf = getBuf (img, width, height);
if (argbBuf == null) return false;
applyClip (getClip ());
applyAlpha ();
drawImage (argbBuf, x, y, width, height, getTransform ());
restoreClip ();
return true;
} Go private int [] getBuf (Image img, int width, int height) {
int [] argbBuf = new int [width * height];
PixelGrabber pg = new PixelGrabber (img, 0, 0, width, height, argbBuf, 0, width);
try {
if (! pg.grabPixels ()) return null;
}
catch (InterruptedException e) {return null; }
if ((pg.getStatus () & ImageObserver.ABORT)! = 0) return null;
return argbBuf;
} Go private void drawImage (int [] argbBuf, int x, int y, int width, int height, AffineTransform transform) {
double [] transMatr = new double [6];
transform.getMatrix (transMatr);
this.image (argbBuf, x, y, width, height, transMatr);
} Go JNIEXPORT void JNICALL Java _..._ image
(JNIEnv * env, jobject obj,
jintArray imageARGBBuff, jint x, jint y, jint width, jint height,
jdoubleArray transformMatrix) {
K2 :: scoped_array <uint8> maskBuffer (new uint8 [imageARGBBuff.length]);
K2 :: scoped_array <uint8> pictBuffer (new uint8 [3 * imageARGBBuff.length]);
GoUtils <IXPUtils> xpUtils; InterfacePtr <IXPManager> xpManager (xpUtils-> QueryXPManager (db)); InterfacePtr <IDocument> doc (db, db-> GetRootUID (), UseDefaultIID ()); AGMColorSpace * blendingSpace = xpManager-> GetDocumentBlendingSpace ();Go
uint8 * outBuff = nil;
int16 compCnt = 3, space = kRGBColorSpace;
if (IsBeingFlattened (env, obj)) {
TransformRGB2CMYKifNeeded (doc, blendingSpace, pictBuffer.get (), & outBuff, imageARGBBuff.length);
compCnt = 4; space = kCMYKColorSpace;
pictBuffer.reset (outBuff);
}
GoAGMImageRecord imgR; memset (& imgR, 0, sizeof (AGMImageRecord)); imgR.bounds.xMin = x; imgR.bounds.yMin = y; imgR.bounds.xMax = x + width; imgR.bounds.yMax = y + height; imgR.byteWidth = compCnt * width; imgR.colorSpace = space; imgR.bitsPerPixel = compCnt * 8; imgR.baseAddr = (void *) pictBuffer.get (); AGMImageRecord maskR; memset (& maskR, 0, sizeof (AGMImageRecord)); maskR.bounds.xMin = x; maskR.bounds.yMin = y; maskR.bounds.xMax = x + width; maskR.bounds.yMax = y + height; maskR.byteWidth = width; maskR.colorSpace = kGrayColorSpace; maskR.bitsPerPixel = 8; maskR.baseAddr = (void *) maskBuffer.get ();Go
PMMatrix transform (nativeTransformData [0], nativeTransformData [1],
nativeTransformData [2],
nativeTransformData [3],
nativeTransformData [4],
nativeTransformData [5]);
GoAGMPaint * alphaServer = xpUt-> CreateImagePaintServer (& maskR, & transform, 0, nil); PMRect bounds (x, y, width + x, height + y);
port-> SetAlphaServer (alphaServer, kTrue, stub);
port-> starttransparencygroup (bounds, blendingSpace, kFalse, kFalse);
port-> image (& imgR, transform, drawFlags);Go
In this way, it was still possible for the good fellow to overcome the three-headed snake, and for the author to Source: https://habr.com/ru/post/127574/
All Articles