[gimp-dds] / trunk / misc.c Repository:
ViewVC logotype

View of /trunk/misc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 127 - (download) (as text) (annotate)
Mon Dec 15 17:32:47 2008 UTC (11 months ago) by cocidius
File size: 6081 byte(s)
Added copyright info to misc.c
    1 /*
    2    DDS GIMP plugin
    3 
    4    Copyright (C) 2004-2008 Shawn Kirst <skirst@insightbb.com>,
    5    with parts (C) 2003 Arne Reuter <homepage@arnereuter.de> where specified.
    6 
    7    This program is free software; you can redistribute it and/or
    8    modify it under the terms of the GNU General Public
    9    License as published by the Free Software Foundation; either
   10    version 2 of the License, or (at your option) any later version.
   11 
   12    This program is distributed in the hope that it will be useful,
   13    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15    General Public License for more details.
   16 
   17    You should have received a copy of the GNU General Public License
   18    along with this program; see the file COPYING.  If not, write to
   19    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   20    Boston, MA 02111-1307, USA.
   21 */
   22 
   23 #include <libgimp/gimp.h>
   24 
   25 static inline float saturate(float a)
   26 {
   27    if(a < 0) a = 0;
   28    if(a > 1) a = 1;
   29    return(a);
   30 }
   31 
   32 void decode_ycocg_image(gint32 drawableID)
   33 {
   34    GimpDrawable *drawable;
   35    GimpPixelRgn srgn, drgn;
   36    unsigned char *src, *dst;
   37    int x, y, w, h;
   38 
   39    const float offset = 0.5f * 256.0f / 255.0f;
   40    float Y, Co, Cg, R, G, B;
   41 
   42    drawable = gimp_drawable_get(drawableID);
   43 
   44    w = drawable->width;
   45    h = drawable->height;
   46 
   47    src = g_malloc(w * 4);
   48    dst = g_malloc(w * 4);
   49 
   50    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
   51    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
   52 
   53    gimp_progress_init("Decoding YCoCg pixels...");
   54 
   55    for(y = 0; y < h; ++y)
   56    {
   57       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
   58 
   59       for(x = 0; x < w; ++x)
   60       {
   61          Y  = (float)src[4 * x + 3] / 255.0f;
   62          Co = (float)src[4 * x + 0] / 255.0f;
   63          Cg = (float)src[4 * x + 1] / 255.0f;
   64 
   65          /* convert YCoCg to RGB */
   66          Co -= offset;
   67          Cg -= offset;
   68 
   69          R = saturate(Y + Co - Cg);
   70          G = saturate(Y + Cg);
   71          B = saturate(Y - Co - Cg);
   72 
   73          /* copy new alpha from blue */
   74          dst[4 * x + 3] = src[4 * x + 2];
   75 
   76          dst[4 * x + 0] = (unsigned char)(R * 255.0f);
   77          dst[4 * x + 1] = (unsigned char)(G * 255.0f);
   78          dst[4 * x + 2] = (unsigned char)(B * 255.0f);
   79       }
   80 
   81       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
   82 
   83       if((y & 31) == 0)
   84          gimp_progress_update((gdouble)y / (gdouble)h);
   85    }
   86 
   87    gimp_progress_update(1.0);
   88 
   89    gimp_drawable_flush(drawable);
   90    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
   91    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
   92    gimp_drawable_detach(drawable);
   93 
   94    g_free(src);
   95    g_free(dst);
   96 }
   97 
   98 void decode_ycocg_scaled_image(gint32 drawableID)
   99 {
  100    GimpDrawable *drawable;
  101    GimpPixelRgn srgn, drgn;
  102    unsigned char *src, *dst;
  103    int x, y, w, h;
  104 
  105    const float offset = 0.5f * 256.0f / 255.0f;
  106    float Y, Co, Cg, R, G, B, s;
  107 
  108    drawable = gimp_drawable_get(drawableID);
  109 
  110    w = drawable->width;
  111    h = drawable->height;
  112 
  113    src = g_malloc(w * 4);
  114    dst = g_malloc(w * 4);
  115 
  116    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
  117    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
  118 
  119    gimp_progress_init("Decoding YCoCg (scaled) pixels...");
  120 
  121    for(y = 0; y < h; ++y)
  122    {
  123       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
  124 
  125       for(x = 0; x < w; ++x)
  126       {
  127          Y  = (float)src[4 * x + 3] / 255.0f;
  128          Co = (float)src[4 * x + 0] / 255.0f;
  129          Cg = (float)src[4 * x + 1] / 255.0f;
  130          s  = (float)src[4 * x + 2] / 255.0f;
  131 
  132          /* convert YCoCg to RGB */
  133          s = 1.0f / ((255.0f / 8.0f) * s + 1.0f);
  134 
  135          Co = (Co - offset) * s;
  136          Cg = (Cg - offset) * s;
  137 
  138          R = saturate(Y + Co - Cg);
  139          G = saturate(Y + Cg);
  140          B = saturate(Y - Co - Cg);
  141 
  142          dst[4 * x + 0] = (unsigned char)(R * 255.0f);
  143          dst[4 * x + 1] = (unsigned char)(G * 255.0f);
  144          dst[4 * x + 2] = (unsigned char)(B * 255.0f);
  145 
  146          /* set alpha to 1 */
  147          dst[4 * x + 3] = 255;
  148       }
  149 
  150       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
  151 
  152       if((y & 31) == 0)
  153          gimp_progress_update((gdouble)y / (gdouble)h);
  154    }
  155 
  156    gimp_progress_update(1.0);
  157 
  158    gimp_drawable_flush(drawable);
  159    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
  160    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
  161    gimp_drawable_detach(drawable);
  162 
  163    g_free(src);
  164    g_free(dst);
  165 }
  166 
  167 void decode_alpha_exp_image(gint32 drawableID)
  168 {
  169    GimpDrawable *drawable;
  170    GimpPixelRgn srgn, drgn;
  171    unsigned char *src, *dst;
  172    int x, y, w, h;
  173    float R, G, B, A;
  174 
  175    drawable = gimp_drawable_get(drawableID);
  176 
  177    w = drawable->width;
  178    h = drawable->height;
  179 
  180    src = g_malloc(w * 4);
  181    dst = g_malloc(w * 4);
  182 
  183    gimp_pixel_rgn_init(&srgn, drawable, 0, 0, w, h, 0, 0);
  184    gimp_pixel_rgn_init(&drgn, drawable, 0, 0, w, h, 1, 1);
  185 
  186    gimp_progress_init("Decoding Alpha-exponent pixels...");
  187 
  188    for(y = 0; y < h; ++y)
  189    {
  190       gimp_pixel_rgn_get_row(&srgn, src, 0, y, w);
  191 
  192       for(x = 0; x < w; ++x)
  193       {
  194          R = src[4 * x + 0];
  195          G = src[4 * x + 1];
  196          B = src[4 * x + 2];
  197          A = (float)src[4 * x + 3] / 255.0f;
  198 
  199          R *= A;
  200          G *= A;
  201          B *= A;
  202 
  203          R = MIN(R, 255);
  204          G = MIN(G, 255);
  205          B = MIN(B, 255);
  206 
  207          dst[4 * x + 0] = (unsigned char)R;
  208          dst[4 * x + 1] = (unsigned char)G;
  209          dst[4 * x + 2] = (unsigned char)B;
  210 
  211          /* set alpha to 1 */
  212          dst[4 * x + 3] = 255;
  213       }
  214 
  215       gimp_pixel_rgn_set_row(&drgn, dst, 0, y, w);
  216 
  217       if((y & 31) == 0)
  218          gimp_progress_update((gdouble)y / (gdouble)h);
  219    }
  220 
  221    gimp_progress_update(1.0);
  222 
  223    gimp_drawable_flush(drawable);
  224    gimp_drawable_merge_shadow(drawable->drawable_id, 1);
  225    gimp_drawable_update(drawable->drawable_id, 0, 0, w, h);
  226    gimp_drawable_detach(drawable);
  227 
  228    g_free(src);
  229    g_free(dst);
  230 }

ViewVC Help
Powered by ViewVC 1.0.4