想用作图像处理,求助

来自中国的朋友,欢迎您在本版面使用中文讨论问题。请注意,如果您想得到不懂中文的人的帮助,请同时提供英文译文。
Help and discussions in Chinese.
Post Reply
swordrong
Posts: 4
Joined: Sun May 29, 2011 10:58 am

想用作图像处理,求助

Post by swordrong »

Dear jerome:
我想用picture打开一幅图像(上次您已解答),把每个像素值读取出来,实现一定算法后又保存为图像文件(想直接用,不是用C重新编写打开图像的代码)用picture可以吗?另我用surface显示图像时,显示没问题,想得到每个像素值,但surface.getpixel(x,y)老出错,不知何故?能有好办法吗?
盼您回音

yours swordrong
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: 想用作图像处理,求助

Post by jerome »

Dear swordrong,

I'm assuming you want to do something like this:

Code: Select all

import "ecere"
 
class Form1 : Window
{
   text = "Form1";
   background = activeBorder;
   borderStyle = sizable;
   hasMaximize = true;
   hasMinimize = true;
   hasClose = true;
   size = { 640, 480 };
   hasMenuBar = true;
 
   char fileName[MAX_LOCATION];
   Bitmap bitmap { };
 
   Menu fileMenu { menu, "File", f };
   MenuItem fileOpen
   {
      fileMenu, "Open", o, ctrlO;
      bool NotifySelect(MenuItem selection, Modifiers mods)
      {
         FileDialog dialog { type = open };
         if(dialog.Modal() == ok)
         {
            strcpy(fileName, dialog.filePath);
            OnUnloadGraphics();
            OnLoadGraphics();
         }
         return true;
      }
   };
 
   bool OnLoadGraphics()
   {
      if(bitmap.Load(fileName, null, null))
      {
         if(bitmap.pixelFormat != pixelFormat888)
            bitmap.Convert(null, pixelFormat888, null);
         if(bitmap.pixelFormat == pixelFormat888)
         {
            int x, y;
            for(y = 0; y < bitmap.height; y++)
            {
               ColorAlpha * pic = ((ColorAlpha *)bitmap.picture) + y * bitmap.stride;
               for(x = 0; x < bitmap.width; x++, pic++)
               {
                  ColorAlpha src = *pic, dst;
                  int a = src.a, r = src.color.r, g = src.color.g, b = src.color.b;
 
                  r += 50;
                  if(r > 255) r = 255;
                  dst = ColorAlpha { (byte)a, { (byte)r, (byte)g, (byte)b } };
 
                  *pic = dst;
               }
            }
 
         }
         bitmap.MakeDD(displaySystem);
      }
      return true;
   }
 
   void OnUnloadGraphics()
   {
      bitmap.Free();
   }
 
   void OnRedraw(Surface surface)
   {
      surface.Blit(bitmap, 0,0,0,0, bitmap.width, bitmap.height);
   }
}
 
Form1 form1 {};
Regards,

Jerome
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: 想用作图像处理,求助

Post by jerome »

You could also do it with a surface and GetPixel/PutPixel, but that would be slower.
However, it would handle multiple pixel formats so you wouldn't need to convert it after loading.

Code: Select all

   bool OnLoadGraphics()
   {
      if(bitmap.Load(fileName, null, null))
      {
         int x, y;
         Surface surface = bitmap.GetSurface(0, 0, null);
         for(y = 0; y < bitmap.height; y++)
         {
            for(x = 0; x < bitmap.width; x++)
            {
               ColorAlpha src = surface.GetPixel(x, y);
               int a = src.a, r = src.color.r, g = src.color.g, b = src.color.b;
 
               r += 50;
               if(r > 255) r = 255;
               surface.foreground = ColorAlpha { (byte)a, { (byte)r, (byte)g, (byte)b } };
               surface.PutPixel(x, y);
            }
         }
         delete surface;
         bitmap.MakeDD(displaySystem);
      }
      return true;
   }
Post Reply