Monotouch: Generating UIImage from PDF leaking entire document size - pdf

In a Monotouch application the following code converts a page from a PDF document to a UIImage. It works, but it doesn't correctly dispose the PDF document.
public class KillerLeak
{
public static void RunOnce()
{
using (CGPDFDocument doc = CGPDFDocument.FromFile("DMSWorkbook.pdf"))
{
Console.WriteLine("Opened: pages={0}", doc.Pages);
UIImage img = GetThumbImage(doc, 1000, 1);
img.Dispose();
}
GC.Collect(GC.MaxGeneration);
GC.WaitForPendingFinalizers();
}
private static UIImage GetThumbImage(CGPDFDocument document, float thumbContentSize, int pageNumber)
{
if ((pageNumber <= 0) || (pageNumber > document.Pages)) {
return null;
}
// Calc page view size
SizeF pageSize = new SizeF(768, 1024);
if (pageSize.Width % 2 > 0) {
pageSize.Width--;
}
if (pageSize.Height % 2 > 0) {
pageSize.Height--;
}
// Calc target size
var targetSize = new Size((int)pageSize.Width, (int)pageSize.Height);
// Draw page on CGImage
CGImage pageImage;
using (CGColorSpace rgb = CGColorSpace.CreateDeviceRGB()) {
using (CGBitmapContext context = new CGBitmapContext(null, targetSize.Width, targetSize.Height, 8, 0, rgb, CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.NoneSkipFirst)) {
using (CGPDFPage pdfPage = document.GetPage(pageNumber)) {
RectangleF thumbRect = new RectangleF(0.0f, 0.0f, targetSize.Width, targetSize.Height);
context.SetFillColor(1.0f, 1.0f, 1.0f, 1.0f);
context.FillRect(thumbRect);
context.ConcatCTM(pdfPage.GetDrawingTransform(CGPDFBox.Crop, thumbRect, 0, true));
context.SetRenderingIntent(CGColorRenderingIntent.Default);
context.InterpolationQuality = CGInterpolationQuality.Default;
context.DrawPDFPage(pdfPage);
pageImage = context.ToImage();
}
}
}
UIImage result = UIImage.FromImage(pageImage);
pageImage.Dispose();
return result;
}
}
If I comment out the DrawPDFPage(...) line, the code doesn't leak. Is this a Monotouch bug or am I doing something wrong?

Turns out the leak is not related to Monotouch as I previously thought. The leak does not occur on IOS 5, but does leak under the next version of IOS which is still under beta. I've tested using the same code written in Monotouch and Objective-C and they both exhibit the same behavior.

Related

com.itextpdf.kernel.PdfException: Document was closed. It is impossible to execute action

I have the following method that shrink the page to 50% :
private void shrinkDocuments(List<PdfDocument> documentsToBeMerged){
for (PdfDocument pdfDoc : documentsToBeMerged) {
for (int p = 1; p <= pdfDoc.getNumberOfPages(); p++) {
PdfPage page = pdfDoc.getPage(p);
Rectangle media = page.getCropBox();
if (media == null) {
media = page.getMediaBox();
}
// Shrink the page to 50%
Rectangle crop = new Rectangle(0, 0, media.getWidth() / 2, media.getHeight() / 2);
page.setMediaBox(crop);
page.setCropBox(crop);
}
pdfDoc.close();
}
}
Debugging I see that the program throws a com.itextpdf.kernel.PdfException: Document was closed. It is impossible to execute action .
How can I solve this ? How can I open again this documents?

QOpenGLFramebufferObject's image not updating

The following is the code for my widget which subclasses QOpenGLWidget. What should be happening is that the framebuffer object flashes between white and black between each frame, but it only displays white, which tells me it's rendering once and then never updating.
void GLWidget::paintEvent(QPaintEvent *event)
{
QRect sourceRect(0, 0, 320, 240);
QSize sourceSize = sourceRect.size();
QOpenGLFramebufferObject sourceBuffer(sourceSize);
sourceBuffer.bind();
QOpenGLPaintDevice device(sourceSize);
QPainter sourcePainter;
sourcePainter.begin(&device);
static bool flash = true;
if (flash)
{
sourcePainter.fillRect(sourceRect, QBrush(Qt::white));
}
else
{
sourcePainter.fillRect(sourceRect, QBrush(Qt::black));
}
flash = !flash;
sourcePainter.end();
sourceBuffer.release();
QPainter destinationPainter;
destinationPainter.begin(this);
destinationPainter.setRenderHint(QPainter::Antialiasing);
destinationPainter.fillRect(event->rect(), QBrush(Qt::black));
int destinationWidth = event->rect().width();
int destinationHeight = event->rect().height();
QImage sourceImage = sourceBuffer.toImage();
//sourceImage.save(&process, "bmp");
QImage scaledSourceImage = sourceImage.scaled(destinationWidth, destinationHeight, Qt::KeepAspectRatio);
int translationX = destinationWidth - scaledSourceImage.width();
int translationY = destinationHeight - scaledSourceImage.height();
destinationPainter.translate(translationX / 2, translationY / 2);
destinationPainter.drawImage(0, 0, scaledSourceImage);
destinationPainter.end();
}
Am I using the framebuffer object correctly here? Also is there a more appropriate way to draw something offscreen and then display it in a QOpenGLWidget?

First Person Camera controls

I have recently started up a 3d first person shooter game in Monogame and I am having some issues with the camera controls, I am unable to figure out how I make the camera slowly turn on it's X axis when I hold down the left/right arrow keys.
At the minute, the code I have is as follows:
Matrix view = Matrix.CreateLookAt(new Vector3(60, 20, 10), new Vector3(0, 0, 0), Vector3.UnitZ);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
And then down in the update section I have this:
if (kb.IsKeyDown(Keys.Left))
{
view = Matrix.CreateLookAt(new Vector3(60, 20, 10), new Vector3(-2, -2, -2), Vector3.UnitZ);
}
The issue is at the minute this code simply moves the camera to the side a little then stops. I am unsure on how to keep having it move until I let go of the key?
The entire of my code will be shown below incase I forgot something (the floor verts currently don't work and the names related to a ship is due to me working from a tutorial):
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace Game1
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Model model;
Vector3 ship1Location = new Vector3(40, 0, 0);
Vector3 ship2Location = new Vector3(20, 0, 0);
Matrix view = Matrix.CreateLookAt(new Vector3(60, 20, 10), new Vector3(0, 0, 0), Vector3.UnitZ);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 600f, 0.1f, 100f);
VertexPositionTexture[] floorVerts;
BasicEffect effect;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
floorVerts = new VertexPositionTexture[6];
floorVerts[0].Position = new Vector3(-20, -20, 0);
floorVerts[1].Position = new Vector3(-20, 20, 0);
floorVerts[2].Position = new Vector3(20, -20, 0);
floorVerts[3].Position = floorVerts[1].Position;
floorVerts[4].Position = new Vector3(20, 20, 0);
floorVerts[5].Position = floorVerts[2].Position;
effect = new BasicEffect(graphics.GraphicsDevice);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
model = Content.Load<Model>("health2");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
Matrix ship1WorldMatrix = Matrix.CreateTranslation(ship1Location);
Matrix ship2WorldMatrix = Matrix.CreateTranslation(ship2Location);
if (IsCollision(model, ship1WorldMatrix, model, ship2WorldMatrix))
{
ship1Location = new Vector3(0, -20, 0);
}
KeyboardState kb = Keyboard.GetState();
if (kb.IsKeyDown(Keys.A))
{
ship1Location += new Vector3(-0.1f, 0, 0);
}
if (kb.IsKeyDown(Keys.Left))
{
view = Matrix.CreateLookAt(new Vector3(60, 20, 10), new Vector3(-2, -2, -2), Vector3.UnitZ);
}
ship2Location += new Vector3(0, 0, 0);
base.Update(gameTime);
}
private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);
for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
{
BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
sphere2 = sphere2.Transform(world2);
if (sphere1.Intersects(sphere2))
return true;
}
}
return false;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawGround();
Matrix ship1WorldMatrix = Matrix.CreateTranslation(ship1Location);
Matrix ship2WorldMatrix = Matrix.CreateTranslation(ship2Location);
DrawModel(model, ship1WorldMatrix, view, projection);
DrawModel(model, ship2WorldMatrix, view, projection);
base.Draw(gameTime);
}
void DrawGround()
{
// The assignment of effect.View and effect.Projection
// are nearly identical to the code in the Model drawing code.
float aspectRatio =
graphics.PreferredBackBufferWidth / (float)graphics.PreferredBackBufferHeight;
float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4;
float nearClipPlane = 1;
float farClipPlane = 200;
effect.Projection = Matrix.CreatePerspectiveFieldOfView(
fieldOfView, aspectRatio, nearClipPlane, farClipPlane);
foreach (var pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
graphics.GraphicsDevice.DrawUserPrimitives(
// We’ll be rendering two trinalges
PrimitiveType.TriangleList,
// The array of verts that we want to render
floorVerts,
// The offset, which is 0 since we want to start
// at the beginning of the floorVerts array
0,
// The number of triangles to draw
2);
}
}
private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach (ModelMesh mesh in model.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.AmbientLightColor = new Vector3(2f, 0, 0);
effect.World = world;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
}
}
Since the camera's position and the point it is looking at are necessary parameters to create a view matrix, you can simply rotate (think orbit) the LookAt camLookAt around the camPosition like this:
//declare class scope variables
Vector3 camPosition = new Vector3(60, 20, 10);//your starting camera position
Vector3 camLookAt = Vector3.Zero;//your starting camera focus point (look at)
Vector2 camUp = Vector3.Up;
float camYawRate = 0.004f;//set to taste
//in the Update method
float elapsed = gameTime.ElapsedGameTime.TotalSeconds;
//later in the method...
if (kb.IsKeyDown(Keys.Left))
{
camLookAt = Vector3.Transform(camLookAt - camPosition,Matrix.CreateRotationY(-camYawRate * elapsedTime)) + camPosition;);//remove the - sign from camYawRate to rotate to the right (or vice versa)
view = Matrix.CreateLookAt(camPosition, camLookAt, camUp);
}
And that's it, give it a shot. Add another similar block to rotate to the right.

Using Angus J Clipper library to outline GraphicsPath text in vb.net

I am creating text in a GDI+ GraphicsPath, using DrawString, and then outputting this to PDF as a path.
This is all working perfectly at the moment.
The time I have an issue is when the chosen font causes the outlines to overlap each other. I have an image example though being a new user i can't upload it... (seems pointless..?..)
I found a library and also someone who has achieved the same as what I am looking for in this blog
I have converted the code snippet to vb.net though I keep getting an empty solution from the library.
Has anybody else managed to pass in a graphicsPath containing a string and retrieve outlined text using this or a similar library?
Here's some C# code that works ...
using ClipperLib;
static public void PathToPolygon(GraphicsPath path, Polygons polys, Single scale)
{
GraphicsPathIterator pathIterator = new GraphicsPathIterator(path);
pathIterator.Rewind();
polys.Clear();
PointF[] points = new PointF[pathIterator.Count];
byte[] types = new byte[pathIterator.Count];
pathIterator.Enumerate(ref points, ref types);
int i = 0;
while (i < pathIterator.Count)
{
Polygon pg = new Polygon();
polys.Add(pg);
do {
IntPoint pt = new IntPoint((int)(points[i].X * scale), (int)(points[i].Y * scale));
pg.Add(pt);
i++;
}
while (i < pathIterator.Count && types[i] != 0);
}
}
static private PointF[] PolygonToPointFArray(Polygon pg, float scale)
{
PointF[] result = new PointF[pg.Count];
for (int i = 0; i < pg.Count; ++i)
{
result[i].X = (float)pg[i].X / scale;
result[i].Y = (float)pg[i].Y / scale;
}
return result;
}
private void DrawBitmap()
{
Font f = new Font("Arial", 90);
Pen myPen = new Pen(Color.FromArgb(196, 0xC3, 0xC9, 0xCF), (float)0.6);
SolidBrush myBrush = new SolidBrush(Color.FromArgb(127, 0xDD, 0xDD, 0xF0));
path.Reset();
Polygons polys;
path.AddString("ABC", f.FontFamily, (int)f.Style, f.Size, new Point(100, 100), null);
path.Flatten();
//scale all points up by 100 because Clipper uses integer coordinates
PathToPolygon(path, polys, 100);
path.Reset();
//offset polys remembering to multiply delta by scaling amount ...
polys = Clipper.OffsetPolygons(polys, 7 * 100, JoinType.jtRound);
for (int i = 0; i < polys.Count(); i++)
{
//reverses scaling ...
PointF[] pts2 = PolygonToPointFArray(polys[i], 100);
path.AddPolygon(pts2);
}
newgraphic.FillPath(myBrush, path);
newgraphic.DrawPath(myPen, path);
}

Change text fill colour when rect is overlayed - Processing

I have this little animation here that animates in a repeated seamless pattern.
I have text, colored white, in the middle of the canvas that the looped shapes pass over. What I am trying to work out is when the white bars pass over the text it turns black. So as half the white bar goes over the T of "Text", half the T would be black and the half not covered would be still white on a diagonal.
Would this be done with splitting up the letters? By doing masking, or using vector images?
Here is a graphic example of what I'm trying to achieve.
http://imm.io/2Qsb
drawLine wob1;
drawLine wob2;
drawLine wob3;
drawLine wob4;
drawLine wob5;
PFont helv;
drawText title;
void setup() {
//frame.setResizable(true);
size(320, 480);
smooth();
frameRate(50);
wob1 = new drawLine(0);
wob2 = new drawLine(200);
wob3 = new drawLine(400);
wob4 = new drawLine(600);
wob5 = new drawLine(800);
title = new drawText();
}
void draw() {
background(#000000);
wob1.increment();
wob1.display(#ffffff);
wob1.pos();
wob1.boundary();
wob2.increment();
wob2.display(#ffffff);
wob2.boundary();
wob3.increment();
wob3.display(#ffffff);
wob3.boundary();
wob4.increment();
wob4.display(#ffffff);
wob4.boundary();
wob5.increment();
wob5.display(#ffffff);
wob5.boundary();
title.rendertitle(#ffffff;
}
class drawLine {
int x = -480;
int y = 0;
int count;
drawLine(int offset) {
this.x = this.x + offset;
}
void increment() {
this.x += 3;
this.y += 4;
}
void display(int col) {
//noStroke();
fill(col);
//translate(0,0);
rectMode(CENTER);
rotate(-PI/3.0);
rect(x,y,100,3000);
rotate(PI/3.0);
}
void pos() {
println(this.x);
//if(this.x >= -218 && this.x <= 207){ colr = #000000; } else { colr = #ffffff; }
}
void boundary() {
if(this.x > 520) {
this.x = -480; this.y = 0;
}
}
}
class drawText {
drawText() {
helv = loadFont("Helvetica-Bold.vlw");
}
void rendertitle(int colr) {
fill(colr);
textFont(helv, 30);
text("Text goes here", width/2, height/2, 240, 50);
}
}
I worked out a solution using two generated images. The first one imgText contains only text (white) in front of black background. The second one imgMaskfunctions as mask and therefore contains the screen line pattern. It's fine to generate the first (text image) only once within the setup() part; because your type doesn't change/move or transform. The mask image has to be updated every frame if you want to achieve the effect of "scanning" lines. This happens every draw() loop through the shift of the offset parameter.
The rest is pretty basic; clear the background every frame and draw the inverted version of imgText before you display the actual masked image.
PImage imgText;
PImage imgMask;
PFont font;
int barHeight = 20;
float offset = 0;
float offsetTick = 0.3;
void setup () {
size (320, 240);
noStroke ();
smooth ();
font = createFont ("Helvetica-Bold.vlw", 18);
// Create new image containing only
// the white text infront of an empty
// black sketch background
background (0);
fill (255);
textFont (font);
textAlign (CENTER);
text ("Text goes here", width/2, height/2);
imgText = createImage (width, height, ARGB);
copySketchIntoImage (imgText);
// Create empty mask image
imgMask = createImage (width, height, ARGB);
}
void draw () {
// Move the scan lines further down
// by increasing offset
offset += offsetTick;
if (offset > barHeight * 2) {
offset = 0;
}
// Update the text mask image
updateMask (offset);
imgText.mask (imgMask);
// Draw the inverted background+text
background (255);
fill (0);
text ("Text goes here", width/2, height/2);
// Display the masked version of the
// text image above the inverted background
image (imgText, 0, 0);
}
void updateMask (float theOffset) {
background (0);
fill (255);
for (float i=theOffset - barHeight; i < height; i += barHeight * 2) {
rect (0, i, width, barHeight);
}
copySketchIntoImage (imgMask);
}
// Method to copy all sketch pixels
// into an existing PImage.
void copySketchIntoImage (PImage theDestImage) {
loadPixels ();
theDestImage.loadPixels ();
for (int i=0; i < pixels.length; i++) {
theDestImage.pixels[i] = pixels[i];
}
theDestImage.updatePixels ();
}