[gimp-dds] / tags / release-2.0.7 / misc.c Repository:
ViewVC logotype

View of /tags/release-2.0.7/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (download) (as text) (annotate)
Fri Dec 12 19:25:26 2008 UTC (11 months, 1 week ago) by cocidius
File size: 5205 byte(s)
Release 2.0.7 tag
    1 #include <libgimp/gimp.h>
    2 
    3 static inline float saturate(float a)
    4 {
    5    if(a < 0) a = 0;
    6    if(a > 1) a = 1;
    7    return(a);
    8 }
    9 
   10 void decode_ycocg_image(gint32 drawableID)
   11 {
   12    GimpDrawable *drawable;
   13    GimpPixelRgn srgn, drgn;
   14    unsigned char *src, *dst;
   15    int x, y, w, h;
   16 
   17    const float offset = 0.5f * 256.0f / 255.0f;
   18    float Y, Co, Cg, R, G, B;
   19 
   20    drawable = gimp_drawable_get(drawableID);
   21 
   22    w = drawable->width;
   23    h = drawable->height;
   24 
   25    src = g_malloc(w * 4);
   26    dst = g_malloc(w * 4);
   27 
   28    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
   29    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
   30 
   31    gimp_progress_init("Decoding YCoCg pixels...");
   32 
   33    for(y = 0; y < h; ++y)
   34    {
   35       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
   36 
   37       for(x = 0; x < w; ++x)
   38       {
   39          Y  = (float)src[4 * x + 3] / 255.0f;
   40          Co = (float)src[4 * x + 0] / 255.0f;
   41          Cg = (float)src[4 * x + 1] / 255.0f;
   42 
   43          /* convert YCoCg to RGB */
   44          Co -= offset;
   45          Cg -= offset;
   46 
   47          R = saturate(Y + Co - Cg);
   48          G = saturate(Y + Cg);
   49          B = saturate(Y - Co - Cg);
   50 
   51          /* copy new alpha from blue */
   52          dst[4 * x + 3] = src[4 * x + 2];
   53 
   54          dst[4 * x + 0] = (unsigned char)(R * 255.0f);
   55          dst[4 * x + 1] = (unsigned char)(G * 255.0f);
   56          dst[4 * x + 2] = (unsigned char)(B * 255.0f);
   57       }
   58 
   59       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
   60 
   61       if((y & 31) == 0)
   62          gimp_progress_update((gdouble)y / (gdouble)h);
   63    }
   64 
   65    gimp_progress_update(1.0);
   66 
   67    gimp_drawable_flush(drawable);
   68    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
   69    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
   70    gimp_drawable_detach(drawable);
   71 
   72    g_free(src);
   73    g_free(dst);
   74 }
   75 
   76 void decode_ycocg_scaled_image(gint32 drawableID)
   77 {
   78    GimpDrawable *drawable;
   79    GimpPixelRgn srgn, drgn;
   80    unsigned char *src, *dst;
   81    int x, y, w, h;
   82 
   83    const float offset = 0.5f * 256.0f / 255.0f;
   84    float Y, Co, Cg, R, G, B, s;
   85 
   86    drawable = gimp_drawable_get(drawableID);
   87 
   88    w = drawable->width;
   89    h = drawable->height;
   90 
   91    src = g_malloc(w * 4);
   92    dst = g_malloc(w * 4);
   93 
   94    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
   95    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
   96 
   97    gimp_progress_init("Decoding YCoCg (scaled) pixels...");
   98 
   99    for(y = 0; y < h; ++y)
  100    {
  101       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
  102 
  103       for(x = 0; x < w; ++x)
  104       {
  105          Y  = (float)src[4 * x + 3] / 255.0f;
  106          Co = (float)src[4 * x + 0] / 255.0f;
  107          Cg = (float)src[4 * x + 1] / 255.0f;
  108          s  = (float)src[4 * x + 2] / 255.0f;
  109 
  110          /* convert YCoCg to RGB */
  111          s = 1.0f / ((255.0f / 8.0f) * s + 1.0f);
  112 
  113          Co = (Co - offset) * s;
  114          Cg = (Cg - offset) * s;
  115 
  116          R = saturate(Y + Co - Cg);
  117          G = saturate(Y + Cg);
  118          B = saturate(Y - Co - Cg);
  119 
  120          dst[4 * x + 0] = (unsigned char)(R * 255.0f);
  121          dst[4 * x + 1] = (unsigned char)(G * 255.0f);
  122          dst[4 * x + 2] = (unsigned char)(B * 255.0f);
  123 
  124          /* set alpha to 1 */
  125          dst[4 * x + 3] = 255;
  126       }
  127 
  128       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
  129 
  130       if((y & 31) == 0)
  131          gimp_progress_update((gdouble)y / (gdouble)h);
  132    }
  133 
  134    gimp_progress_update(1.0);
  135 
  136    gimp_drawable_flush(drawable);
  137    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
  138    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
  139    gimp_drawable_detach(drawable);
  140 
  141    g_free(src);
  142    g_free(dst);
  143 }
  144 
  145 void decode_alpha_exp_image(gint32 drawableID)
  146 {
  147    GimpDrawable *drawable;
  148    GimpPixelRgn srgn, drgn;
  149    unsigned char *src, *dst;
  150    int x, y, w, h;
  151    float R, G, B, A;
  152 
  153    drawable = gimp_drawable_get(drawableID);
  154 
  155    w = drawable->width;
  156    h = drawable->height;
  157 
  158    src = g_malloc(w * 4);
  159    dst = g_malloc(w * 4);
  160 
  161    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
  162    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
  163 
  164    gimp_progress_init("Decoding Alpha-exponent pixels...");
  165 
  166    for(y = 0; y < h; ++y)
  167    {
  168       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
  169 
  170       for(x = 0; x < w; ++x)
  171       {
  172          R = src[4 * x + 0];
  173          G = src[4 * x + 1];
  174          B = src[4 * x + 2];
  175          A = (float)src[4 * x + 3] / 255.0f;
  176 
  177          R *= A;
  178          G *= A;
  179          B *= A;
  180 
  181          R = MIN(R, 255);
  182          G = MIN(G, 255);
  183          B = MIN(B, 255);
  184 
  185          dst[4 * x + 0] = (unsigned char)R;
  186          dst[4 * x + 1] = (unsigned char)G;
  187          dst[4 * x + 2] = (unsigned char)B;
  188 
  189          /* set alpha to 1 */
  190          dst[4 * x + 3] = 255;
  191       }
  192 
  193       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
  194 
  195       if((y & 31) == 0)
  196          gimp_progress_update((gdouble)y / (gdouble)h);
  197    }
  198 
  199    gimp_progress_update(1.0);
  200 
  201    gimp_drawable_flush(drawable);
  202    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
  203    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
  204    gimp_drawable_detach(drawable);
  205 
  206    g_free(src);
  207    g_free(dst);
  208 }

ViewVC Help
Powered by ViewVC 1.0.4