C + + Read the RGB value of the image (two methods, have been tested)

Source: Internet
Author: User

1, the use of magick++ read, applicable to a variety of picture formats (magick++ support)

Related information:

Http://www.imagemagick.org/Magick++/Image.html

Http://www.imagemagick.org/Magick++/Documentation.html

http://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=16625

int _tmain (int argc, _tchar* argv[]) {Geometry g (800, 533); Image thei; File* FH; float *floatpixelmap; try {thei.read (g, "./bluejay.jpg"); int height = thei.rows (); int width = Thei.columns (); int size = Height * Width * 4; pixelpacket* mymap = thei.getpixels (0, 0, width, height); Floatpixelmap = new Float[size]; for (int counter = 0, k=0 counter < size; counter++, ++k) {floatpixelmap[counter++] = (float) mymap[k].red/255.0f; f loatpixelmap[counter++] = (float) mymap[k].green/255.0f; floatpixelmap[counter++] = (float) mymap[k].blue/255.0f; Floatpixelmap[counter] = (float) mymap[k].opacity/255.0f; }//end for loop fh = fopen ("./bluejay32float.procd.rgba", "WB"); Fwrite (void *) Floatpixelmap, sizeof (float), size, FH); }//end try block catch (exception& ex) {cout << "imagicktstr,error," << ex.what () << Endl;} End catch blocks return 0; }//end Main

2, directly parse BMP file format, can only read BMP format

#include <string.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include < Malloc.h> #define Widthbytes (BITS) ((BITS)/32*4) typedef unsigned char byte;//character typedef unsigned short word;// integer typedef unsigned long dword;//-shape typedef A. Bitmap file header information structure definition//which does not contain file type information (due to the structure of the memory structure of the decision, if added will not read the correct file information) typedef struct TAGBITMAPFILEHEADER {//word bftype; DWORD bfsize; File size WORD bfReserved1; reserved word, do not consider word bfReserved2; reserved word, ibid. DWORD bfoffbits; The number of offset bytes of the actual bitmap data, that is, the first three-part length and} Bitmapfileheader; Information header Bitmapinfoheader, also a structure, is defined as follows: typedef struct tagbitmapinfoheader{//public:dword bisize;//Specify the length of this structure, for long Biwidth; Bitmap wide LONG biheight; Bitmap high WORD biplanes; Number of planes, for 1 WORD bibitcount; The use of color number, can be 1,2,4,8,16,24, the new can be a DWORD bicompression; Compression mode, can be 0,1,2, where 0 means not compressing the DWORD bisizeimage; The actual bitmap data occupies a number of bytes LONG bixpelspermeter; X direction resolution LONG Biypelspermeter; Y-directional resolution DWORD biclrused; The number of colors used, if 0, the default value (2^ color digits) DWORD biclrimportant; Number of important colors, if 0, all colorsare important} Bitmapinfoheader; Palette palette, of course, is for those bitmap files that require a palette. 24-bit and 32-bit are not required for palettes. (The number of colors that appear to be the palette structure equals that used.) ) typedef struct TAGRGBQUAD {//public:byte rgbblue;///blue component of the color byte rgbgreen;//green component of the color byte rgbred;//red component of the color byte RG breserved; Reserved value} Rgbquad; void Showrgbquan (tagrgbquad* prgb) {printf (%-3d,%-3d,%-3d), Prgb->rgbred,prgb->rgbgreen,prgb->rgbblue) ; int main () {char ch,pd; int b,c,m,d,w,e,f; int x,y; unsigned char *q; int i,j; unsigned char a[2]; char strfile[50]; Bitmapfileheader Bithead; Bitmapinfoheader Bitinfohead; Tagrgbquad *prgb; FILE *FP; FILE *p; q= (unsigned char*) malloc (1); printf ("mkx** in C language programming to read BMP file a pixel of data **/n Please enter a BMP file:/n"); scanf ("%s", strfile); Fp=fopen (strfile, "RB"); if (fp!=null) {printf ("File Open success!/n"); WORD FileType; Fread (&filetype,1,sizeof (WORD), FP); if (FileType!= 0x4d42) {printf ("file isn't. bmp file!"); System ("pause"); return 0;}} else {printf ("File Open fail!/n"); System ("pause"); return 0}//Read file header information and print the value fread of the file header information (&amP;bithead,1,sizeof (Tagbitmapfileheader), FP); printf ("BMP header information/n File size:%d/n reserved word:%d/n reserved word:%d/n offset bytes of actual bitmap data:%d/n/n", bithead.bfsize,bithead.bfreserved1, Bithead.bfreserved2,bithead.bfoffbits); Read the file information header and print the value of the file information header fread (&bitinfohead,1,sizeof (Tagbitmapinfoheader), FP); printf ("BMP File Information header/n struct Length:%d/n bitmap width:%d/n bitmap height:%d/nbiplanes plane number:%d/nbibitcount using color digits:%d/n compression mode:%d/ Nbisizeimage the number of bytes in the actual bitmap data:%d/nx Direction Resolution:%d/ny Direction Resolution:%d/n The number of colors used:%d/n Important colors:%d/n ", Bitinfohead.bisize, Bitinfohead.biwidth,bitinfohead.biheight,bitinfohead.biplanes,bitinfohead.bibitcount,bitinfohead.bicompression , bitinfohead.bisizeimage,bitinfohead.bixpelspermeter,bitinfohead.biypelspermeter,bitinfohead.biclrused, Bitinfohead.biclrimportant); if (Bitinfohead.bibitcount <) {printf ("The file has a palette, that is, the bitmap is a non true Color/n/n"); m=1 if (Bitinfohead.bibitcount =8) {long Nplantnum = Long (Pow (2,double (bitinfohead.bibitcount))); Mix Color plant number; prgb= (Tagrgbquad *) malloc (nplantnum*sizeof (Tagrgbquad)); memset (prgb,0,nplantnum*sizeof (Tagrgbquad)); int num = freAD (PRGB,4,NPLANTNUM,FP); printf ("Color Plate Number:%d/n", nplantnum); printf ("Color board information:/n"); for (int i =0; i<nplantnum;i++) {if (i%5==0) {printf ("/n");} Showrgbquan (&prgb[i]); printf ("n"); } else{printf ("This bitmap is a 24-bit true Color/n/n"); m=3;} fclose (FP); while (ch!= ' N ') {P=fopen (strfile, "RB"); ch=null; f=bitinfohead.biheight; e=bitinfohead.biwidth; b=bithead.bfoffbits; printf ("* * Enter the data for the specified pixel * * (row: 1~%d column 1~%d)/n", f,e); printf ("Please enter line I:"); scanf ("%d", &x); printf ("Please enter column J:"); scanf ("%d", &y); c=b+ (x-1) *e*m+m* (y-1); Fseek (p,c,0); printf ("%d/n", m); if (m<2) {fread (&a[0],1,1,p); printf ("Data of this pixel point (hexadecimal) is:%x, a[0]);" printf ("In decimal notation:%d/n", a[0]); D=A[0]; printf ("its corresponding RGB value is:"); Showrgbquan (&prgb[d]); printf ("n"); else {for (i=0;i<3;i++) {fread (&a[i],1,1,p);//printf ("%x/n", A[i]); printf ("Hexadecimal:/n blue:%x/n green:%x/n Red:%x/n", a [0],a[1],a[2]); printf ("Decimal representation:/n Blue:%d/n green:%d/n Red:%d/n", a[0],a[1],a[2)); } rewind (P); W=ftell (P); printf ("%d", W); Fclose (P); printf ("* * Continue Please enter any character, if you want to exit Please enter n/n"); GetChar (); scanf ("%c", &ch); printf ("Thank you for using ~/n"); System ("pause"); return 0; } 

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.