Yet Another Image Format: Google's "WebP"

Discuss various technical topics not related to Mozilla.
Post Reply
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

If you've got a sample APNG and GIF image I'll slap it all together in a working form over lunch - although I could probably find something on the web too.
Intelligent alien life does exist, otherwise they would have contacted us.
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

Code: Select all

<!DOCTYPE html>
<title>apng-detect.js image swapping Demo</title>
<script src="http://code.eligrey.com/apng-detect/apng-detect.js"></script>
<script>
//<![CDATA[
if (typeof apng_supported !== "boolean") {
   var apng_supported = false;
}
document.addEventListener('DOMContentLoaded',function(){
   if (apng_supported) {
      var images = document.images;
      for (var i=0; i<images.length; i++) {
         if (images[i].src.indexOf('clock.gif') != -1)
            images[i].src = 'http://animatedpng.com/wp-content/uploads/clock.png';
      }
   }
}, false);
//]]>
</script>
<img src="http://animatedpng.com/wp-content/uploads/clock.gif">

Note of course that you shouldn't hotlink the script (or the images) if you decide to use it somewhere.
Last edited by Frenzie on October 10th, 2010, 2:16 pm, edited 1 time in total.
Intelligent alien life does exist, otherwise they would have contacted us.
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

Is it possible to avoid hardcoding the filename into the script?

Lets say I have
<img src="image1_static.gif">
<img src="image2_anim.gif">
<img src="image3_static.gif">
<img src="image4_anim.gif">
<img src="image5_static.gif">

And I would like to replace only 2 animated images?
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

Sure, then you'll have to use the method I described earlier.

Code: Select all

      if (images[i].src.indexOf('anim.gif') != -1)
         images[i].src.replace(/\.gif/i, '.png');

(as noted before, untested)

Alternatively you could do something like this:

Code: Select all

<img src="any-name.gif">
<img src="any-name.gif">
<img src="any-name.gif" class="apng">
<img src="any-name.gif">
<img src="any-name.gif">
<img src="any-name.gif" class="apng">
<img src="any-name.gif">


Then you could do document.getElementsByClassName('apng') instead of document.images and you wouldn't have to do any other testing[1] prior to the source swap. There are many roads that lead to Rome. ;)

[1] You don't really need to do the indexOf() test, but my untested assumption is that this should be faster than the regexp in replace(). In practice it probably won't make much difference, so it should be fine to stick with:

Code: Select all

       images[i].src.replace(/anim\.gif/i, 'anim.png');
Last edited by Frenzie on October 10th, 2010, 2:19 pm, edited 1 time in total.
Intelligent alien life does exist, otherwise they would have contacted us.
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

I'm thinking of this:

Code: Select all

images[i].src = images[i].src.replace(/anim\.gif$/i,'anim.png');


I'll try class too.
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

Just depends on what's more convenient to you; functionally it's equivalent.
Intelligent alien life does exist, otherwise they would have contacted us.
User avatar
Gingerbread Man
Posts: 7735
Joined: January 30th, 2007, 10:55 am

Re: Yet Another Image Format: Google's "WebP"

Post by Gingerbread Man »

Frenzie wrote:document.getElementsByClassName

Requires Internet Explorer 9 (!), Firefox 3 (and probably Camino 2, Seamonkey 2 or K-Meleon 1.6), Safari 3.1 (and probably Chrome 1.0), or Opera 9.5.
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

If this replacement won't work in older browsers, then older browsers will be using GIFs, which is ok.

Yes, I think the class trick is simplest, no need to bother with "special" file names. It seems to be working fine.

I'll post it on animatedpng.com so other people could learn from it too. Thanks again.
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

Gingerbread Man wrote:
Frenzie wrote:document.getElementsByClassName

Requires Internet Explorer 9 (!), Firefox 3 (and probably Camino 2, Seamonkey 2 or K-Meleon 1.6), Safari 3.1 (and probably Chrome 1.0), or Opera 9.5.

As does canvas, which is used to determine APNG support, and APNG itself. If anything you should just throw a big if(document.getElementsByClassName) around everything. ;)
Intelligent alien life does exist, otherwise they would have contacted us.
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

I just realized I tested the indexOf results against 0. This should've been -1 since 0 is of course a proper position in the string. Whoopsie. Shouldn't affect anything (especially given the final method used), but of course I don't want to spread wrong information.
Intelligent alien life does exist, otherwise they would have contacted us.
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

Yes, I figured that out too, and used -1

Here's final version using class:

Code: Select all

<!DOCTYPE html>
<title>apng-detect.js image swapping Demo</title>
<script src="apng-detect.js"></script>
<script>
//<![CDATA[
if (typeof apng_supported !== "boolean") {
   var apng_supported = false;
}
document.addEventListener('DOMContentLoaded',function() {
   if (apng_supported) {
      var apng_elements = document.getElementsByClassName('apng');
      for (var i=0; i<apng_elements.length; i++) {
             apng_elements[i].src = apng_elements[i].src.replace(/gif$/i,'png');
      }
   }
}, false);
//]]>
</script>
<img src="image1.gif">
<img src="image2.gif" class="apng">
<img src="image3.gif">
User avatar
Frenzie
Posts: 2135
Joined: May 5th, 2004, 10:40 am
Location: Belgium
Contact:

Re: Yet Another Image Format: Google's "WebP"

Post by Frenzie »

Looks good to me.
Intelligent alien life does exist, otherwise they would have contacted us.
User avatar
bat999
Posts: 40
Joined: September 10th, 2010, 3:30 pm
Location: UK

Re: Yet Another Image Format: Google's "WebP"

Post by bat999 »

Hi
I don't know how antimatter15 makes a “RIFF encoded WebP image” from an image.
And then how he converts it into a “EBML/Matroska encoded single frame WebM video”.
I've asked the questions on his blog, but not got a reply yet.

Have you guys any ideas how to do this?
maxst
Posts: 112
Joined: February 11th, 2010, 5:04 am

Re: Yet Another Image Format: Google's "WebP"

Post by maxst »

WebP is already in RIFF container, by definition.

The conversion code is in weppy.js

I assume his code simply changes RIFF header to EBML header, or somehting like that.
Post Reply