OpenGL: Image pixel manipulation

Source: Internet
Author: User
#define Glut_disable_atexit_hack #include <Windows.h> #include <stdio.h> #include <gl/glut.h> #
Include <gl/glaux.h> #pragma comment (lib, "Glaux.lib") #pragma comment (lib, "Glut32.lib") aux_rgbimagerec* Image;
Glint iwidth,iheight,imode=1;

Glubyte* Pimage;
	void Menucreate (void) {Glutaddmenuentry ("normal display", 1);
	Glutaddmenuentry ("Flip Image", 2);
	Glutaddmenuentry ("Reduced image", 3);
	Glutaddmenuentry ("Red Channel", 4);
	Glutaddmenuentry ("Green channel", 5);
	Glutaddmenuentry ("Blue channel", 6);
	Glutaddmenuentry ("Grayscale image", 7);

	Glutaddmenuentry ("Invert color", 8);
Glutattachmenu (Glut_right_button);

	} void initialization (void) {Glclearcolor (0,0,0,1);

	Menucreate ();
	Glpixelstorei (gl_unpack_alignment, 1);
	Image = Auxdibimageload ("11.bmp");
	Iwidth = image->sizex;
	Iheight = image->sizey;
Pimage = image->data;

	} void Ondisplay (void) {glclear (gl_color_buffer_bit);
	Glbyte *pmodified = NULL;
	Glfloat invert[256];

	Glint I;

	Glrasterpos2i (0, 0); Switch (iMode) {case 2:glpixelzoom ( -1.0f, -1.0f);
		Glrasterpos2i (IWidth-1, iHeight-1);

	Break
		Case 3:glpixelzoom (0.5f, 0.5f);

	Break
		Case 4:glpixeltransferf (Gl_red_scale, 1.0f);
		Glpixeltransferf (Gl_green_scale, 0.0f);
		Glpixeltransferf (Gl_blue_scale, 0.0f);

	Break
		Case 5:glpixeltransferf (Gl_red_scale, 0.0f);
		Glpixeltransferf (Gl_green_scale, 1.0f);
		Glpixeltransferf (Gl_blue_scale, 0.0f);

	Break
		Case 6:glpixeltransferf (Gl_red_scale, 0.0f);
		Glpixeltransferf (Gl_green_scale, 0.0f);
		Glpixeltransferf (Gl_blue_scale, 1.0f);

	Break
		Case 7:gldrawpixels (Iwidth,iheight, Gl_rgb, Gl_unsigned_byte, pimage);

		Pmodified = (glbyte*) New glbyte[iwidth * iheight];
		Glpixeltransferf (Gl_red_scale, 0.3f);
		Glpixeltransferf (Gl_green_scale, 0.59f);

		Glpixeltransferf (Gl_blue_bits, 0.11f);
		Glreadpixels (0, 0, iwidth, iheight, Gl_luminance, Gl_unsigned_byte, pmodified);
		Glpixeltransferf (Gl_red_scale, 1.0f);
		Glpixeltransferf (Gl_green_scale, 1.0f);
		Glpixeltransferf (Gl_blue_scale, 1.0f); Break;
		Case 8:invert[0] = 1.0f;
		for (i = 1; i < i++) {Invert[i] = 1.0f-(1.0f/255.0f * glfloat (i));
		} GLPIXELMAPFV (Gl_pixel_map_r_to_r, 255, invert);
		GLPIXELMAPFV (Gl_pixel_map_g_to_g, 255, invert);
		GLPIXELMAPFV (Gl_pixel_map_b_to_b, 255, invert);
		Glpixeltransferf (gl_map_color,gl_true);

	Break
	Case 1:default:break;
	} if (pmodified = = NULL) {gldrawpixels (iwidth, Iheight, Gl_rgb,gl_unsigned_byte, pimage);
		} else {gldrawpixels (iwidth, Iheight, Gl_luminance, Gl_unsigned_byte, pmodified);
	delete [] pmodified;
	} glpixeltransferi (Gl_map_color, Gl_false);
	Glpixeltransferf (Gl_red_scale, 1.0f);
	Glpixeltransferf (Gl_green_scale, 1.0f);
	Glpixeltransferf (Gl_blue_scale, 1.0f);

	Glpixelzoom (1.0f, 1.0f);
Glutswapbuffers ();

	} void Onreshape (int w,int h) {glfloat aspectratio= (glfloat) w/(Glfloat) h;//calculates the aspect ratio of the window glfloat winwidth,winheight;

	Glviewport (0, 0, W, h);
	Glmatrixmode (gl_projection);


	Glloadidentity ();              if (w <= h) {                 Calculates the width and height of the drawing area according to aspect ratio winwidth = 400;
	winheight = * Aspectratio;
		} else {winwidth = Aspectratio *;
	Winheight= 400;


	} gluortho2d (0, winwidth, 0, winheight);
	Glmatrixmode (Gl_modelview);
Glloadidentity ();

	} void OnMenu (int value) {IMode = value;
Glutpostredisplay ();
	} void Main (int argc,char* argv[]) {glutinit (&AMP;ARGC,ARGV); Glutinitdisplaymode (glut_double | Glut_rgb |
	Glut_depth);
	Glutinitwindowsize (400, 400);

	Glutcreatewindow ("EXAM703");
	Glutcreatemenu (OnMenu);
	Glutdisplayfunc (Ondisplay);

	Glutreshapefunc (Onreshape);
	Initialization ();
Glutmainloop (); }

Operation Result:


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.