Cimage access pixel and pixel Operation Summary

Source: Internet
Author: User
Msdn code
  1. Colorref pixel;
  2. Int Maxy = imgoriginal. getheight (), Maxx = imgoriginal. getwidth ();
  3. Byte R, G, B, AVG;
  4. For (INT y = 0; y <Maxy; y ++ ){
  5. For (INT x = 0; x <Maxx; X ++ ){
  6. Pixel = imgoriginal. getpixel (x, y );
  7. R = getrvalue (pixel );
  8. G = getgvalue (pixel );
  9. B = getbvalue (pixel );
  10. AVG = (R + G + B)/3;
  11. Imgoriginal. setpixelrgb (X, Y, AVG, AVG, avg );
  12. }}

This method is very inefficient, because every call to getpixel contains the program's stack entry and exit. Therefore, in the face of a large amount of data to be processed, the method of direct access to the memory address is used.

  1. Byte * prealdata;
  2. Prealdata = (byte *) imgoriginal. getbits ();
  3. Int Pit = imgoriginal. getpitch ();
  4. Int bitcount = imgoriginal. getbpp ()/8;
  5. For (INT y = 0; y <Maxy; y ++ ){
  6. For (INT x = 0; x <Maxx; X ++ ){
  7. Int grayval = (INT) (* (prealdata + pit * Y + x * bitcount) * 0.3
  8. + (INT) (* (prealdata + pit * Y + x * bitcount + 1) * 0.59
  9. + (INT) (* (prealdata + pit * Y + x * bitcount + 2) * 0.11;
  10. * (Prealdata + pit * Y + x * bitcount) = grayval;
  11. * (Prealdata + pit * Y + x * bitcount + 1) = grayval;
  12. * (Prealdata + pit * Y + x * bitcount + 2) = grayval;
  13. // If it is an 8-bit grayscale image, read a byte bit directly as the grayscale value.
  14. // For 24-bit RGB Images, read pixaddr, pixaddr + 1 in sequence, and pixaddr + 2 is the B, G, and r component values.
  15. }}

Two methods are used to process the same image (3264*2448 pixels). The former takes 1 minute, and the latter takes about 1 second.

Therefore, the latter is at least 60 times faster than the former.

 

Another method for direct access to the memory address:

Int I, j, temp; int pixel [4]; int width = yuantu. getwidth (); int Height = yuantu. getheight (); int widthbytes = yuantu. getpitch (); bianyuantu. create (width, height, yuantu. getbpp (); If (yuantu. isindexed () {yuantu. getcolortable (0,256, colortable); bianyuantu. setcolortable (0,256, colortable);} byte * pyuantudata = (byte *) yuantu. getbits (); byte * pbianyuantudata = (byte *) bianyuantu. getbits (); For (j = 0; j 

  

How to convert a color image to a grayscale image

// Convert a true color image to a grayscale image and directly modify the pixel value

 
  1. Void pixelschangedtogray (cimage * pimage)
  2. {
  3. Int nbyte, J, I, nwidth, nheight, nbytesperpixel;
  4. Byte * ppixelline, cnewpixelvalue;
  5. Nwidth = pimage-> getwidth (); nheight = pimage-> getheight ();
  6. Nbytesperpixel = pimage-> getbpp ()/8;
  7. For (I = 0; I <nheight; I ++ ){
  8. Ppixelline = (byte *) pimage-> getpixeladdress (0, I );
  9. Nbyte = 0;
  10. For (j = 0; j <nwidth; j ++) {cnewpixelvalue = (byte) (0.11 * ppixelline [nbyte]
  11. + 0.59 * ppixelline [nbyte + 1]
  12. + 0.30 * ppixelline [nbyte + 2]);
  13. Ppixelline [nbyte] = ppixelline [nbyte + 1] = ppixelline [nbyte + 2]
  14. = Cnewpixelvalue;
  15. Nbyte + = nbytesperpixel;
  16. }
  17. }
  18. }


// Changes a non-real color image to a grayscale image and modifies the color palette information.

  1. Void palettechangedtogray (cimage * pimage)
  2. {
  3. Rgbquad colortabs [256];
  4. Int I, ncolortableentries, nnewgraycolor;
  5. Ncolortableentries = pimage-> getmaxcolortableentries ();
  6. Pimage-> getcolortable (0, ncolortableentries, colortabs );
  7. For (I = 0; I <ncolortableentries; I ++ ){
  8. Nnewgraycolor = (INT) (0.11 * colortabs [I]. rgbblue
  9. + 0.59 * colortabs [I]. rgbgreen
  10. + 0.30 * colortabs [I]. rgbred );
  11. Colortabs [I]. rgbblue = (byte) nnewgraycolor;
  12. Colortabs [I]. rgbgreen = (byte) nnewgraycolor;
  13. Colortabs [I]. rgbred = (byte) nnewgraycolor;
  14. }
  15. Pimage-> setcolortable (0, ncolortableentries, colortabs );
  16. }

Cimage access pixel and pixel Operation Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.