How to use html canvas toDataURL on IOS without increasing memory? - ios

Firstly, the following thread discusses the issue that toDataURL may increase memory consumption, but doesn't offer a way to use toDataURL safely:
javascript memory leak with HTML5 getImageData
In my application, I need to call toDataURL. I have a PhoneGap application running on IOS that take photos using the native camera, tiles the images together into one collage image, and sends the final image as a binary 64 string via ajax post to a server. The part of my code that tiles the images uses an html canvas and toDataURL to accomplish that. This tiling occurs repeatedly over the lifetime of the software process. I am seeing the application memory increase until IOS aborts the process.
What would you recommend to do to be able to call toDataURL but not run out of memory? I don't see how to release this memory.
Thanks.

Instead of trying to memory manage, it may just be easier to send the independent images to the server and let the server create the collage for you.

Related

How to get the memory address of a rendered image?

I develop a simulator in Unity which has to communicate with another application. The problem is that I have to render a camera manually and send the obtained image to the other application, which takes too long.
My question: is there a way to get the memory address where the obtained image is? I need this to read the image directly from the other application in order to reduce the time of the transmission.

Access the whole video memory

I'm looking for a way to read the whole video memory that a video card outputs to a display. That includes also hardware accelerated output, video playback and output in fullscreen mode (that somehow I feel could be different from windowed mode).
In short: I want to be able to capture everything that is going to be represented on a display.
I suppose that IF that's possible it would be os-dependant. The targets I'm interested in are Windows OSX and Linux.
Do you have any hint?
For windows I guess you could take CamStudio, strip it down and use it to record the screen then do whatever you want with the output, other than that you could look into forensic kernel drivers for accessing RAM. It's not exactly as simple as a pointer pointing to the video memory anymore, haha.
Digital Rights Management, requested feature of Windows, attempts to block your access to blocks of graphics-card frame buffer memory. Using an open-source driver under Linux would seem to be the only way to access this memory, or as mentioned earlier, some 3rd party software that knows some back doors or hacks or ways to locate other program's frame buffer space.
Unless of course, you are trying to capture output from your own program (ie you are calling the video/graphics creation functions yourself), there are APIs to manipulate display frames in DirectX and OpenGL.
I think I found some resources that can help to capture the display memory in Windows
Fastest method of screen capturing
How to save backbuffer to file in DirectX 10?
http://betterlogic.com/roger/2010/07/fast-screen-capture/

load 5MB of images crashed iPad

I need to load 5MB of HD images (for iPad) before my game starts up. It crashes somewhere in the loading progress. Well, to confirm it was the 5MB problem, I then tried to load 2.5MB of LD images (iPhone quality) instead. It ran OK, no crashes.
Does it mean that I can't reserve 5MB or more memory? There should be a way to do it.
This happens more frequently when more apps pending in the background. And it doesn't crash if I run my game immediately after a clean reboot. Therefore, it must be a memory problem crashing my game.
Any idea how I can handle this?
5MB of compressed image data can easily explose to many (4x-10x) the memory requirements to have them uncompressed.
Any idea how I can handle this?
Use Apple's Instruments and look at memory usage.
Next ensure iOS itself is not caching images (look at the API you're using and their documentation, some will cache, others won't).
Then look if you can dispose of the image earlier. Images can be quite big (and a lot of memory for them is internal to iOS, which is why using Instruments will give you a better picture than MonoTouch's own HeapShot profiler) so disposing it manually (instead of waiting for the GC is generally a good idea).
Also look if you can delay load some images (or at least their decompressed versions), e.g. after the first one(s) are disposed.

values of levelOfDetail and levelsOfDetailBias to render pdf on CATiledLayer in ios

i am developing a project in which i render PDF on the CATiledLayers.I have Used the CGPdf class methods to render the pdf and succeeded too.
I would like to know the values to be used for levelsOfDetail and levelsOfDetailBias for avoiding any memory issues either in normal mode or zoom mode.
Right now i am setting the values a s below.
tiledLayer1.levelsOfDetail = 1;
tiledLayer1.levelsOfDetailBias = 30;
Am i using the appropriate values and does the memory get affected with these values?
I got this doubt since i am facing memory issues on zooming the page.I ensured there are no memory leaks and the code is effectively written.
my zoomScale ranges between 1.0 to 2.0.
Can anyone help me out to avoid the memory issue...and the values to be used for the above parameters.
Thanks in advance...
You can try reducing the levelsOfDetailBias. But one thing you should keep in mind is that whatever you do, memory warnings would certainly appear,we just need to handle it.
For instance, a simple pdf page may not trigger memory warning at all in any zoom level, whereas pdf page with high quality images may lead to memory warning. Also memory warning depends on the entire device on what is available for the application to run.

What is the fastest way of loading and re-sizing an image?

I need to display thumbnails of images in a given directory. I use TFileStream to read the image file before loading the image into an image component. The bitmap is then resized to the thumbnail size, and assigned to a TImage component on a TScrollBox.
It seems to work ok, but slows down quite a lot with larger images.
Is there a faster way of loading (image) files from disk and resizing them?
Thanks, Pieter
Not really. What you can do is resize them in a background thread, and use a "place holder" image until the resizing is done. I would then save these resized images to some sort of cache file for later processing (windows does this, and calls the cache thumbs.db in the current directory).
You have several options on the thread architecture itself. A single thread that does all images, or a thread pool where a thread only knows how to process a single image. The AsyncCalls library is even another way and can keep things fairly simple.
I'll complement the answer by skamradt with an attempt to design this for being as fast as possible. For this you should
optimize I/O
use multiple threads to make use of multiple CPU cores, and to keep even a single CPU core working while you read (or write) files
The use of multiple threads implies that using VCL classes for the resizing isn't going to work, as the VCL isn't thread-safe, and all hacks around that don't scale well. efg's Computer Lab has links for image processing code.
It's important to not cause several concurrent I/O operations when using multiple threads. If you choose to write the thumbnail images back to files, then once you have started reading a file you should read it completely, and once you have started writing a file you should also write it completely. Interleaving both operations will kill your I/O, because you potentially cause a lot of seeking operations of the hard disc head.
For best results the reading (and writing) of files should also not happen in the main (GUI) thread of your application. That would suggest the following design:
Have one thread read files into TGraphic objects, and put these into a thread-safe list.
Have a thread pool wait on the list of files in original size, and have one thread process one TGraphic object, resize it into another TGraphic object, and add this to another thread-safe list.
Notify the GUI thread for each thumbnail image added to the list, so it can be displayed.
If thumbnails are to be written to file, do this in the reading thread as well (see above for an explanation).
Edit:
On re-reading your question I notice that you maybe only need to resize one image, in which case a single background thread is of course enough. I'll leave my answer in place anyway, maybe it will be of use to someone else some time. It's what I learned from one of my latest projects, where the final program could have needed a little more speed but was only using about 75% of the quad core machine at peak times. Decoupling I/O from processing would have made the difference.
I often use TJPEGImage with Scale:=jsEighth (in Delphi 7). This is really fast because the JPEG de-compression can skip a lot of the data to fill a bitmap of only an eighth of width and height.
Another option is to use the shell's method to extract a thumbnail, which is pretty fast as well
I'm in the vision business, and I simply upload the images to the GPU using OpenGL. (typically 20x 2048x2000x8bpp per second), a bmp per texture, and let the videocard scale (win32, Mike Lischke's opengl headers)
Upload of such an image costs 5-10ms depending on exact videocard (if not integrated and nvidia 7300 series or newer. Very recent integrated GPUs might be doable also). Scaling and displaying costs 300us. Which means customers can pan and zoom like crazy without touching the app. I draw an overlay (which used to be a tmetafile but is now an own format) on top of it.
My biggest picture is 4096x7000x8bpp which shows and scales in under 30ms. (GF 8600)
A limitation of this technology is max texture size. It can be resolved by fragmenting the picture into multiple textures, but I haven't bothered yet because I deliver the systems with the software.
(some typical sizes:
nv6x00 series: 2k*2k but uploading is just about break even compared to GDI
nv7x00 series: 4k*4k For me the baseline cards. GF7300's are like $20-40
nv8x00 series: 8k*8k
)
Note that this might not be for everybody. But if you are in the lucky situation to specify hardware limits, it might work. The main problem are laptops like Thinkpads, the GPUs of which are older than the avg laptop, which are in turn often a generation behind Desktops.
I chose OpenGL over DirectX because it is more static in time, and easier to find non-game related examples.
Try to look at the Graphics32 library : it's very good at drawing things and works great with Bitmaps. They are Thread - Safe with good example, and it's totally free.
Exploit windows capacity to create thumbnails. Remember that hidden Thumbs.db files in folders that contain images?
I have implemented something like this feature but in VB. My software is able to build thumbnails of 100 files (mixed size) in around 10 seconds.
I am not able to convert it to Delphi though.

Resources