Home | History | Annotate | Download | only in libgtk2ui
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/ui/libgtk2ui/skia_utils_gtk2.h"
      6 
      7 #include <gdk/gdk.h>
      8 
      9 #include "base/basictypes.h"
     10 #include "base/logging.h"
     11 #include "third_party/skia/include/core/SkBitmap.h"
     12 #include "third_party/skia/include/core/SkUnPreMultiply.h"
     13 
     14 namespace libgtk2ui {
     15 
     16 // GDK_COLOR_RGB multiplies by 257 (= 0x10001) to distribute the bits evenly
     17 // See: http://www.mindcontrol.org/~hplus/graphics/expand-bits.html
     18 // To get back, we can just right shift by eight
     19 // (or, formulated differently, i == (i*257)/256 for all i < 256).
     20 
     21 SkColor GdkColorToSkColor(GdkColor color) {
     22   return SkColorSetRGB(color.red >> 8, color.green >> 8, color.blue >> 8);
     23 }
     24 
     25 GdkColor SkColorToGdkColor(SkColor color) {
     26   GdkColor gdk_color = {
     27       0,
     28       static_cast<guint16>(SkColorGetR(color) * kSkiaToGDKMultiplier),
     29       static_cast<guint16>(SkColorGetG(color) * kSkiaToGDKMultiplier),
     30       static_cast<guint16>(SkColorGetB(color) * kSkiaToGDKMultiplier)
     31   };
     32   return gdk_color;
     33 }
     34 
     35 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) {
     36   // TODO(erg): What do we do in the case where the pixbuf fails these dchecks?
     37   // I would prefer to use our gtk based canvas, but that would require
     38   // recompiling half of our skia extensions with gtk support, which we can't
     39   // do in this build.
     40   DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf));
     41 
     42   int n_channels = gdk_pixbuf_get_n_channels(pixbuf);
     43   int w = gdk_pixbuf_get_width(pixbuf);
     44   int h = gdk_pixbuf_get_height(pixbuf);
     45 
     46   SkBitmap ret;
     47   ret.setConfig(SkBitmap::kARGB_8888_Config, w, h);
     48   ret.allocPixels();
     49   ret.eraseColor(0);
     50 
     51   uint32_t* skia_data = static_cast<uint32_t*>(ret.getAddr(0, 0));
     52 
     53   if (n_channels == 4) {
     54     int total_length = w * h;
     55     guchar* gdk_pixels = gdk_pixbuf_get_pixels(pixbuf);
     56 
     57     // Now here's the trick: we need to convert the gdk data (which is RGBA and
     58     // isn't premultiplied) to skia (which can be anything and premultiplied).
     59     for (int i = 0; i < total_length; ++i, gdk_pixels += 4) {
     60       const unsigned char& red = gdk_pixels[0];
     61       const unsigned char& green = gdk_pixels[1];
     62       const unsigned char& blue = gdk_pixels[2];
     63       const unsigned char& alpha = gdk_pixels[3];
     64 
     65       skia_data[i] = SkPreMultiplyARGB(alpha, red, green, blue);
     66     }
     67   } else if (n_channels == 3) {
     68     // Because GDK makes rowstrides word aligned, we need to do something a bit
     69     // more complex when a pixel isn't perfectly a word of memory.
     70     int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
     71     guchar* gdk_pixels = gdk_pixbuf_get_pixels(pixbuf);
     72     for (int y = 0; y < h; ++y) {
     73       int row = y * rowstride;
     74 
     75       for (int x = 0; x < w; ++x) {
     76         guchar* pixel = gdk_pixels + row + (x * 3);
     77         const unsigned char& red = pixel[0];
     78         const unsigned char& green = pixel[1];
     79         const unsigned char& blue = pixel[2];
     80 
     81         skia_data[y * w + x] = SkPreMultiplyARGB(255, red, green, blue);
     82       }
     83     }
     84   } else {
     85     NOTREACHED();
     86   }
     87 
     88   return ret;
     89 }
     90 
     91 GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap& bitmap) {
     92   if (bitmap.isNull())
     93     return NULL;
     94 
     95   SkAutoLockPixels lock_pixels(bitmap);
     96 
     97   int width = bitmap.width();
     98   int height = bitmap.height();
     99 
    100   GdkPixbuf* pixbuf =
    101       gdk_pixbuf_new(GDK_COLORSPACE_RGB,  // The only colorspace gtk supports.
    102                      TRUE,                // There is an alpha channel.
    103                      8,
    104                      width,
    105                      height);
    106 
    107   // SkBitmaps are premultiplied, we need to unpremultiply them.
    108   const int kBytesPerPixel = 4;
    109   uint8* divided = gdk_pixbuf_get_pixels(pixbuf);
    110 
    111   for (int y = 0, i = 0; y < height; y++) {
    112     for (int x = 0; x < width; x++) {
    113       uint32 pixel = bitmap.getAddr32(0, y)[x];
    114 
    115       int alpha = SkColorGetA(pixel);
    116       if (alpha != 0 && alpha != 255) {
    117         SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel);
    118         divided[i + 0] = SkColorGetR(unmultiplied);
    119         divided[i + 1] = SkColorGetG(unmultiplied);
    120         divided[i + 2] = SkColorGetB(unmultiplied);
    121         divided[i + 3] = alpha;
    122       } else {
    123         divided[i + 0] = SkColorGetR(pixel);
    124         divided[i + 1] = SkColorGetG(pixel);
    125         divided[i + 2] = SkColorGetB(pixel);
    126         divided[i + 3] = alpha;
    127       }
    128       i += kBytesPerPixel;
    129     }
    130   }
    131 
    132   return pixbuf;
    133 }
    134 
    135 }  // namespace libgtk2ui
    136