eet: encode lossy image with AGRY88 or GRY8 whenever possible.
authorCedric BAIL <cedric@osg.samsung.com>
Tue, 17 Mar 2015 07:50:28 +0000 (08:50 +0100)
committerCedric BAIL <cedric@osg.samsung.com>
Tue, 17 Mar 2015 08:58:19 +0000 (09:58 +0100)
src/lib/eet/eet_image.c

index 1e06cf6..f94bc59 100644 (file)
@@ -1160,8 +1160,43 @@ _eet_data_image_rgb_encode(const void *data,
    void *d = NULL;
    size_t sz = 0;
    JSAMPROW *jbuf;
+   unsigned int *grey;
    unsigned char *buf;
 
+   /* Try to encode this buffer as GRY8 or AGRY88.
+      It is difficult to know if a different approach
+      would have payed of, but for now I do walk and
+      copy the pixels as long as they are grey. If I
+      manage to copy them up to the end, I will be
+      able to encode them as a GRY8 texture.
+    */
+   grey = malloc(sizeof (int) * w * h);
+   if (grey)
+     {
+        const unsigned int *pixels = data;
+        unsigned int i;
+
+        for (i = 0; i < w * h; i++)
+          {
+             uint8_t r, g, b;
+
+             r = R_VAL(&pixels[i]);
+             g = G_VAL(&pixels[i]);
+             b = B_VAL(&pixels[i]);
+             if (!(r == g && g == b))
+               break ;
+             grey[i] = r << 24;
+          }
+
+        if (i == w * h)
+          {
+             d = _eet_data_image_grey_encode(grey, w, h, quality, size);
+             free(grey);
+             return d;
+          }
+        free(grey);
+     }
+
    buf = alloca(3 * w);
 
    memset(&cinfo, 0, sizeof (struct jpeg_compress_struct));