How can we check whether uploaded image is compressed or not?
I want to draw an image on canvas, then compress it using canvasContext.toDataUrl(type, quality) but this compression will only be applicable to that image if it is not already compressed.
Do you have any suggestion?
You can pretty much determine if the file is compressed by looking at the file type. If you inspect the string toDataURL() produces, you will see a mime-type defining either a PNG or JPEG file - in some cases where browsers support other file formats you can also see BMP and ICO file formats.
We know that a PNG file is always compressed as the PNG standard only support compression type 0 which is LZ77 compression (on top of line filters which affects the final compressed size).
JPEG always compresses as it uses DCT.
Compression for BMP is optional as well as for TIFF, though no browsers I know of support TIFF out of the box. It's reasonable to assume BMP and ICO files are uncompressed. They do exist in compression forms such as RLE but these are rare and can cause problems for some BMP parsers. To be absolutely sure though, you would have to parse the binary data to look in the header for compression flags.
Notice that toDataURL() always work on a raw uncompressed bitmap. It does not matter if the original image drawn to the canvas was compressed or not - the original image is always converted to a raw bitmap before drawn (actually when it's loaded).
After calling toDataURL() however, the binary image that it produces internally is converted to a Base-64 string. This means an increase of size by 33% due to how Base-64 works. On top of that: each char in JavaScript occupies 2 bytes (this is not a problem of course when in a JavaScript environment). So the length of the string is not a good indicator as it may possibly exceed the raw size (width x height x 4) in some cases (toBlob() is in any case a better alternative than toDataURL() due to its higher performance and reduced size as well as being async/non-blocking).
Related
I have an image stored as a png (I could convert it to bmp too). I want to open it in JavaScript and get the raw RGB bytes. It is enough if this works locally in Chrome. So I open ./index.html in the browser which loads an image in the same directory, e.g. with <img src=myimage.png>. However, I need the proper original data, without any compression or artifacts. I can't use NodeJS.
I saw a similar question, Get image data in JavaScript, but it requires that the image is hosted somewhere. I'm also not sure how to get the raw RGB bytes, the results I got from trying those examples looked like they were still encoded as png.
EDIT: As one of the answers to the other SO question mentions, a canvas will re-encode the data and reading from it won't give me exactly the same values as in the original.
I am developing a DAM which is hosted in AWS. The user is able to upload heavy files to the system. Under the hood, when an image is uploaded, there is an AWS Lambda function creating a thumbnail for each image.
Obviously files with format .psd and .eps cannot be displayed on the browser with the typical HTML img item. That is why I will need to convert those file formats to .png or .jpg.
Maybe another solution would be to take a "screenshot on the fly" directly in .png. I do not know if this is possible.
The Node.js code running on the Lambda function is very similar the one here: http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html
Thanks in advance for your help!!
I do not know much about AWS, Lambda and Node.js but can maybe help somewhat with the ImageMagick aspects...
To convert an image from one format to another with ImageMagick, you basically use the convert program with appropriate filename extensions like this in the Terminal, or at the command-line:
convert input.jpg output.png # convert a JPEG to a PNG
EPS files
With EPS, which is a vector format, you generally should set the density first, else ImageMagick will use 72 dpi which makes for horrible quality, so for EPS try something like:
convert -density 144 input.eps output.png
PSD files
With Photoshop PSD files, there is generally a preview image and all the multiple layers following afterwards, so, if you are looking to get a Preview, you should use this style of command to address the layer 0 preview in the PSD file:
convert input.psd[0] output.png
If you want to reduce the size of an image, you would resize it after loading like this:
convert input.png -resize 512x256 output.png
to make it no larger than 512 pixels wide or 256 pixels tall.
Another thing you may like to do is to strip the metadata (time/date, camera model, creating application, GPS position of camera) out of the images, for that, add in -strip just before the output filename.
Not sure what else I can help with, but hope that gets you started.
I have a (mostly) offline webapp where users can sign off with a digital signature (using this library: https://github.com/szimek/signature_pad)
The image size of a signature is about 50K, and is sent to the server as a base64 encoded json string.
Since this data is sent over satellite, I am looking to minimize the bandwidth used for each signature.
Is there any JavaScript library to do a lossy compression of the PNG to reduce the file size?
PNG is inherently lossless. If the destination can accept it, use a JPEG instead.
If not, you could try to decimate the image yourself, and then losslessly compress it with PNG. You can also try the PNG-8 mode to compress to a palette of 256 or fewer colors (which might require a lossy step), which should result in a smaller file.
I know this is a very old question, but I have something that may help. I am assuming that the signature pad library uses the native Canvas.toDataURL function.
I looked a png saved from a canvas with the pngcheck utility and it uses RGBA - 4 bytes per pixel. For your purposes you could use either a greyscale or palette color png that will be a much smaller size, but still lossless.
Large (2192 x 2800) greyscale PNG: 39KB
Same PNG drawn to Canvas (via DrawImage) and then saved back to an image via toDataURL: 184 KB.
Furthermore, the data inside a PNG is stored using the DEFLATE method - may libraries, like this one take a compression level as an argument, basically trading speed for size.
I need to alter the palette data in PNG images using JavaScript. I would like to do this without using WebGL and drawing to canvass, since this can be... inefficient, and cause slowdown. However, I'm not experienced with working with data compression and I know PNGs use compression.
I have three questions. One, do I need to fully decompress the image, or is it possible to only decompress parts up to the PLTE chunk(s) then re-compress the image?
Two, can JavaScript even work with raw binary data, or will I need to get really creative with base64 and string manipulation?
Third, since I'm working with palette and not truecolor images and so don't need to handle IDAT chunks... are the previous chunks actually compressed? Is this going to even require me to decompress the image?
I need to download a BMP with JavaScript and render it to the screen, in Internet Explorer. First off, yes, I know this is insane, I'm not going to get into why, let's just accept for a moment that img src is not working because of security constraints, but an ajax request with the proper authentication in the post will pull back the image. This example bypasses all the security for the sake of simplicity and just proves we can render something.
The best idea I could come up with was to fetch the stream via ajax, decode the bitmap, and then render it with canvas. Internet Explorer obviously doesn't support canvas, but luckily Google provided a wrapper to SVG called excanvas that I can use for that.
My code (drawing code appears to work, bmp decoding not so much)
http://gist.github.com/614328
Future support for other images besides BMP is plausable, and because of how the canvas works it's easiest to draw pixels in RGBA. Texture2D is essentially the wrapper class for an RGBA byte array, plus the drawing code. ByteStream makes it a bit easier on the eyes dealing with the byte array, and BitmapDecoder contains the method to translate the BGR format to RGBA texture2d for drawing.
Is it possible the bytes are getting mis-translated along the way or is there something the matter with my decoding logic?
FYI, I got the file spec from wikipedia:
http://en.wikipedia.org/wiki/BMP_file_format#Bitmap_Information_.28DIB_header.29
Any idea what's going on in the decoding logic or drawing logic that's causing my BMP to draw incorrectly?
XMLHttpRequest (aka AJAX) was primarily designed for text content, so it's possible that binary data (especially null characters) aren't translated correctly. The first check would be to compare the size of retrieved data with the actual file size.
At least on Firefox, there seems to be a way to specifically retrieve binary data, as described here: Handling binary data.
Here's a much easier (and vastly more performant) approach: base64 encode the BMP data (you can do this either on the server or the client) and then embed it in the page using a data URI:
<script type="text/javascript">
function fetchBmp() {
$.get('http://localhost:3168/experimental/imgrender/beta.bmp', function (data) {
var base64Data = $.base64.encode(data); // *
$('#my-image').attr('src', 'data:image/bmp;base64,' + base64Data);
});
}
// * Lots of plugins for this, e.g. http://github.com/carlo/jquery-base64
</script>
<img id="my-image" />
All modern browsers support data URIs (including IE8 and up--for IE7 workarounds exist) as well as the BMP format.
As casablanca points out, there may be issues with loading binary data via Ajax, so you may have to google around for workarounds.
The fix was a combination of two things
a bit of VBScript to read the raw bytes of responseBody
decoding the byte data properly, each pixel is not padded as the wikipedia article suggests, it's actually each scanline that is padded to dword size.
Working code:
http://gist.github.com/616240