⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pngrtran.c

📁 一套图像处理程序,支持三种图像文件格式,我调试过了,很好用
💻 C
📖 第 1 页 / 共 5 页
字号:
                  sp = row;
                  shift = 4;
                  for (i = 0; i < row_info->width; i++)
                  {
                     if (((*sp >> shift) & 0xf) ==
                        trans_values->gray)
                     {
                        *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                        *sp |= (png_byte)(background->gray << shift);
                     }
                     if (!shift)
                     {
                        shift = 4;
                        sp++;
                     }
                     else
                        shift -= 4;
                  }
                  break;
               }
               case 8:
               {
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_table)
                  {

                     for (i = 0, sp = row;
                        i < row_info->width; i++, sp++)
                     {
                        if (*sp == trans_values->gray)
                        {
                           *sp = background->gray;
                        }
                        else
                        {
                           *sp = gamma_table[*sp];
                        }
                     }
                  }
                  else
#endif
                  {
                     for (i = 0, sp = row;
                        i < row_info->width; i++, sp++)
                     {
                        if (*sp == trans_values->gray)
                        {
                           *sp = background->gray;
                        }
                     }
                  }
                  break;
               }
               case 16:
               {
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_16)
                  {
                     for (i = 0, sp = row;
                        i < row_info->width; i++, sp += 2)
                     {
                        png_uint_16 v;

                        v = (png_uint_16)(((png_uint_16)(*sp) << 8) +
                           (png_uint_16)(*(sp + 1)));
                        if (v == trans_values->gray)
                        {
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
                        }
                        else
                        {
                           v = gamma_16[
                              *(sp + 1) >> gamma_shift][*sp];
                           *sp = (png_byte)((v >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(v & 0xff);
                        }
                     }
                  }
                  else
#endif
                  {
                     for (i = 0, sp = row;
                        i < row_info->width; i++, sp += 2)
                     {
                        png_uint_16 v;

                        v = (png_uint_16)(((png_uint_16)(*sp) << 8) +
                           (png_uint_16)(*(sp + 1)));
                        if (v == trans_values->gray)
                        {
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
                        }
                     }
                  }
                  break;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
#if defined(PNG_READ_GAMMA_SUPPORTED)
               if (gamma_table)
               {
                  for (i = 0, sp = row;
                     i < row_info->width; i++, sp += 3)
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
                        *sp = background->red;
                        *(sp + 1) = background->green;
                        *(sp + 2) = background->blue;
                     }
                     else
                     {
                        *sp = gamma_table[*sp];
                        *(sp + 1) = gamma_table[*(sp + 1)];
                        *(sp + 2) = gamma_table[*(sp + 2)];
                     }
                  }
               }
               else
#endif
               {
                  for (i = 0, sp = row;
                     i < row_info->width; i++, sp += 3)
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
                        *sp = background->red;
                        *(sp + 1) = background->green;
                        *(sp + 2) = background->blue;
                     }
                  }
               }
            }
            else if (row_info->bit_depth == 16)
            {
#if defined(PNG_READ_GAMMA_SUPPORTED)
               if (gamma_16)
               {
                  for (i = 0, sp = row;
                     i < row_info->width; i++, sp += 6)
                  {
                     png_uint_16 r, g, b;

                     r = (png_uint_16)(((png_uint_16)(*sp) << 8) +
                        (png_uint_16)(*(sp + 1)));
                     g = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
                        (png_uint_16)(*(sp + 3)));
                     b = (png_uint_16)(((png_uint_16)(*(sp + 4)) << 8) +
                        (png_uint_16)(*(sp + 5)));
                     if (r == trans_values->red &&
                        g == trans_values->green &&
                        b == trans_values->blue)
                     {
                        *sp = (png_byte)((background->red >> 8) & 0xff);
                        *(sp + 1) = (png_byte)(background->red & 0xff);
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(background->green & 0xff);
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
                     }
                     else
                     {
                        png_uint_16 v;
                        v = gamma_16[
                           *(sp + 1) >> gamma_shift][*sp];
                        *sp = (png_byte)((v >> 8) & 0xff);
                        *(sp + 1) = (png_byte)(v & 0xff);
                        v = gamma_16[
                           *(sp + 3) >> gamma_shift][*(sp + 2)];
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(v & 0xff);
                        v = gamma_16[
                           *(sp + 5) >> gamma_shift][*(sp + 4)];
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(v & 0xff);
                     }
                  }
               }
               else
#endif
               {
                  for (i = 0, sp = row;
                     i < row_info->width; i++, sp += 6)
                  {
                     png_uint_16 r, g, b;

                     r = (png_uint_16)(((png_uint_16)(*sp) << 8) +
                        (png_uint_16)(*(sp + 1)));
                     g = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
                        (png_uint_16)(*(sp + 3)));
                     b = (png_uint_16)(((png_uint_16)(*(sp + 4)) << 8) +
                        (png_uint_16)(*(sp + 5)));
                     if (r == trans_values->red &&
                        g == trans_values->green &&
                        b == trans_values->blue)
                     {
                        *sp = (png_byte)((background->red >> 8) & 0xff);
                        *(sp + 1) = (png_byte)(background->red & 0xff);
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(background->green & 0xff);
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
                     }
                  }
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_GRAY_ALPHA:
         {
            switch (row_info->bit_depth)
            {
               case 8:
               {
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_to_1 && gamma_from_1 && gamma_table)
                  {
                     for (i = 0, sp = row,
                        dp = row;
                        i < row_info->width; i++, sp += 2, dp++)
                     {
                        png_uint_16 a;

                        a = *(sp + 1);
                        if (a == 0xff)
                        {
                           *dp = gamma_table[*sp];
                        }
                        else if (a == 0)
                        {
                           *dp = background->gray;
                        }
                        else
                        {
                           png_uint_16 v;

                           v = gamma_to_1[*sp];
                           v = (png_uint_16)(((png_uint_16)(v) * a +
                              (png_uint_16)background_1->gray *
                              (255 - a) + 127) / 255);
                           *dp = gamma_from_1[v];
                        }
                     }
                  }
                  else
#endif
                  {
                     for (i = 0, sp = row,
                        dp = row;
                        i < row_info->width; i++, sp += 2, dp++)
                     {
                        png_uint_16 a;

                        a = *(sp + 1);
                        if (a == 0xff)
                        {
                           *dp = *sp;
                        }
                        else if (a == 0)
                        {
                           *dp = background->gray;
                        }
                        else
                        {
                           *dp = (png_byte)(((png_uint_16)(*sp) * a +
                              (png_uint_16)background_1->gray *
                              (255 - a) + 127) / 255);
                        }
                     }
                  }
                  break;
               }
               case 16:
               {
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_16 && gamma_16_from_1 && gamma_16_to_1)
                  {
                     for (i = 0, sp = row,
                        dp = row;
                        i < row_info->width; i++, sp += 4, dp += 2)
                     {
                        png_uint_16 a;

                        a = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
                           (png_uint_16)(*(sp + 3)));
                        if (a == (png_uint_16)0xffff)
                        {
                           png_uint_32 v;

                           v = gamma_16[
                              *(sp + 1) >> gamma_shift][*sp];
                           *dp = (png_byte)((v >> 8) & 0xff);
                           *(dp + 1) = (png_byte)(v & 0xff);
                        }
                        else if (a == 0)
                        {
                           *dp = (png_byte)((background->gray >> 8) & 0xff);
                           *(dp + 1) = (png_byte)(background->gray & 0xff);
                        }
                        else
                        {
                           png_uint_32 g, v;

                           g = gamma_16_to_1[
                              *(sp + 1) >> gamma_shift][*sp];
                           v = (g * (png_uint_32)a +
                              (png_uint_32)background_1->gray *
                              (png_uint_32)((png_uint_16)65535L - a) +
                              (png_uint_16)32767) / (png_uint_16)65535L;
                           v = gamma_16_from_1[(size_t)(
                              (v & 0xff) >> gamma_shift)][(size_t)(v >> 8)];
                           *dp = (png_byte)((v >> 8) & 0xff);
                           *(dp + 1) = (png_byte)(v & 0xff);
                        }
                     }
                  }
                  else
#endif
                  {
                     for (i = 0, sp = row,
                        dp = row;
                        i < row_info->width; i++, sp += 4, dp += 2)
                     {
                        png_uint_16 a;

                        a = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
                           (png_uint_16)(*(sp + 3)));
                        if (a == (png_uint_16)0xffff)
                        {
                           png_memcpy(dp, sp, 2);
                        }
                        else if (a == 0)
                        {
                           *dp = (png_byte)((background->gray >> 8) & 0xff);
                           *(dp + 1) = (png_byte)(background->gray & 0xff);
                        }
                        else
                        {
                           png_uint_32 g, v;

                           g = ((png_uint_32)(*sp) << 8) +
                              (png_uint_32)(*(sp + 1));
                           v = (g * (png_uint_32)a +
                              (png_uint_32)background_1->gray *
                              (png_uint_32)((png_uint_16)65535L - a) +
                              (png_uint_16)32767) / (png_uint_16)65535L;
                           *dp = (png_byte)((v >> 8) & 0xff);
                           *(dp + 1) = (png_byte)(v & 0xff);
                        }
                     }
                  }
                  break;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
#if defined(PNG_READ_GAMMA_SUPPORTED)
               if (gamma_to_1 && gamma_from_1 && gamma_table)
               {
                  for (i = 0, sp = row,
                     dp = row;
                     i < row_info->width; i++, sp += 4, dp += 3)
                  {
                     png_uint_16 a;

                     a = *(sp + 3);
                     if (a == 0xff)
                     {
                        *dp = gamma_table[*sp];
                        *(dp + 1) = gamma_table[*(sp + 1)];
                        *(dp + 2) = gamma_table[

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -