C ++ reads the RGB value of the image (both methods have been tested)

Source: Internet
Author: User

1. Use magick ++ to read images. It is applicable to various image formats (supported by magick ++)

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 []) {<br/> geometry g (800,533); <br/> image thei; <br/> file * FH; <br/> float * floatpixelmap; <br/> try {<br/> thei. read (G ,". /bluejay.jpg "); <br/> int Height = thei. rows (); <br/> int width = thei. columns (); <br/> int size = height * width * 4; <br/> pixelpacket * mymap = thei. getpixels (0, 0, width, height); <br/> floatpixelmap = new float [size]; <br/> for (INT counter = 0, K = 0; counter <size; counter ++, ++ K) {<br/> floatpixelmap [counter ++] = (float) mymap [K]. red/255.0f; <br/> floatpixelmap [counter ++] = (float) mymap [K]. green/255.0f; <br/> floatpixelmap [counter ++] = (float) mymap [K]. blue/255.0f; <br/> floatpixelmap [Counter] = (float) mymap [K]. opacity/255.0f; <br/>}// end for loop <br/> FH = fopen (". /bluejay32float. procd. rgba "," WB "); <br/> fwrite (void *) floatpixelmap, sizeof (float), size, FH ); <br/>}// end try block <br/> catch (exception & Ex) {<br/> cout <"imagicktstr, error," <ex. what () <Endl; <br/>}// end Catch Block <br/> return 0; <br/>}// end main 

2. parse the BMP file format directly and only read the BMP format.

# Include <string. h> <br/> # include <math. h> <br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <malloc. h> <br/> # define widthbytes (BITs) + 31)/32*4) <br/> typedef unsigned char byte; // struct type <br/> typedef unsigned short word; // short integer <br/> typedef unsigned long DWORD; // long integer <br/> typedef long; <br/> // structure definition of Bitmap File Header Information <br/> // It does not contain file type information (determined by the memory structure of the struct, if the file information is added, the file information cannot be correctly read. <br/> typedef struct tagbi Tmapfileheader {<br/> // word bftype; <br/> DWORD bfsize; // file size <br/> word bfreserved1; // reserved words, <br/> word bfreserved2; // reserved words, same as above <br/> DWORD bfoffbits; // The actual bitmap data offset bytes, that is, the sum of the length of the first three parts <br/>} bitmapfileheader; <br/> // The Information header bitmapinfoheader is also a structure, which is defined as follows: <br/> typedef struct tagbitmapinfoheader {<br/> // public: <br/> DWORD bisize; // specify the length of the struct, which is 40 <br/> long biwidth; // bitmap width <br/> long biheight; // bitmap height <br/> word biplan Es; // number of planes, 1 <br/> word bibitcount; // color digits, which can be 1, 2, 4, 8, 16, 24, the new one can be 32 <br/> DWORD bicompression; // compression mode, which can be, 2, where 0 indicates no compression <br/> DWORD bisizeimage; // number of bytes occupied by the actual bitmap data <br/> long bixpelspermeter; // X-direction resolution <br/> long biypelspermeter; // y-direction resolution <br/> DWORD biclrused; // The number of colors used. If it is 0, it indicates the default value (2 ^ number of colors) <br/> DWORD biclrimportant; // The number of important colors. If it is 0, it indicates that all colors are important <br/>} bitmapinfoheader; <br/> // palette. Of course, this is for bitmap files that require a palette.. A 24-bit or 32-bit color palette is not required. <Br/> // (it seems that the number of struct in the color palette is equal to the number of colors used .) <Br/> typedef struct tagrgbquad {<br/> // public: <br/> byte rgbblue; // The Blue weight of the color <br/> byte rgbgreen; // The green weight of the color <br/> byte rgbred; // The Red weight of the color <br/> byte rgbreserved; // reserved value <br/>} rgbquad; <br/> void showrgbquan (tagrgbquad * prgb) <br/> {<br/> printf ("(%-3D, %-3D, %-3d )", prgb-> rgbred, prgb-> rgbgreen, prgb-> rgbblue); <br/>}< br/> int main () {<br/> char CH, PD; <br/> int B, C, M, D, W, E, F; <br/> int X, Y; <br/> unsign Ed char * q; <br/> int I, j; <br/> unsigned char a [2]; <br/> char strfile [50]; <br/> bitmapfileheader bithead; <br/> bitmapinfoheader bitinfohead; <br/> tagrgbquad * prgb; <br/> file * FP; <br/> file * P; <br/> q = (unsigned char *) malloc (1 ); <br/> printf ("mkx ** uses C programming language to read data of a certain pixel in the BMP file **/n enter a BMP file:/N "); <br/> scanf ("% s", strfile); <br/> fp = fopen (strfile, "rb"); </P> <p> If (FP! = NULL) <br/>{< br/> printf ("file open success! /N "); <br/> word filetype; <br/> fread (& filetype, 1, sizeof (Word), FP); <br/> If (filetype! = 0x4d42) <br/>{< br/> printf ("file is not. BMP file! "); <Br/> system (" pause "); <br/> return 0; <br/>}< br/> else <br/> {<br/> printf ("file open fail! /N "); <br/> system (" pause "); <br/> return 0; <br/>}< br/> // read file header information, and print the values of the file header information <br/> fread (& bithead, 1, sizeof (tagbitmapfileheader), FP ); <br/> printf ("BMP file header information/n file size: % d/n reserved words: % d/n actual bitmap data offset Bytes: % d/n ", <br/> bithead. bfsize, bithead. bfreserved1, bithead. bfreserved2, bithead. bfoffbits); <br/> // read the file information header and print the values of each item in the file information header <br/> fread (& bitinfohead, 1, sizeof (tagbitmapinfoheader ), FP); <br/> printf ("BMP file information Header/length of N struct Degree: % d/n bitmap width: % d/n bitmap Height: % d/nbiplanes flat count: % d/nbibitcount adopts the color bits: % d/n compression mode: % d/nbisizeimage the number of bytes occupied by 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 ", <br/> bitinfohead. bisize, bitinfohead. biwidth, bitinfohead. biheight, bitinfohead. biplanes, bitinfohead. bibitcount, bitinfohead. bicompression, bitinfohead. bisizeimage, bitinfohead. bixpelspermeter, bitinfohead. biypelspermeter, bitinfohead. biclrused, bitinfohead. biclrimp Ortant); <br/> If (bitinfohead. bibitcount <24) {<br/> printf ("the file has a color palette, that is, the bitmap is non-real color/n"); <br/> M = 1; <br/> If (bitinfohead. bibitcount = 8) {<br/> long nplantnum = long (POW (2, double (bitinfohead. bibitcount); // mix color plant number; <br/> prgb = (tagrgbquad *) malloc (nplantnum * sizeof (tagrgbquad); <br/> memset (prgb, 0, nplantnum * sizeof (tagrgbquad); <br/> int num = fread (prgb, 4, nplantnum, FP); <br/> printf ("col Or plate number: % d/N ", nplantnum); <br/> printf (" color palette information:/N "); <br/> for (INT I = 0; I <nplantnum; I ++) <br/>{< br/> if (I % 5 = 0) <br/>{< br/> printf ("/N"); <br/>}< br/> showrgbquan (& prgb [I]); <br/>}< br/> printf ("/N "); <br/>}< br/> else {<br/> printf ("the bitmap is a 24-bit real color/n "); <br/> M = 3; <br/>}</P> <p> fclose (FP); </P> <p> while (Ch! = 'N') {<br/> P = fopen (strfile, "rb"); <br/> CH = NULL; <br/> F = bitinfohead. biheight; <br/> E = bitinfohead. biwidth; <br/> B = bithead. bfoffbits; <br/> printf ("** enter the data of the specified pixel ** (row: 1 ~ % D column 1 ~ % D)/n ", F, E); <br/> printf (" Enter line I: "); <br/> scanf (" % d ", & X); <br/> printf ("Enter column J:"); <br/> scanf ("% d", & Y ); <br/> C = B + (x-1) * E * m + M * (Y-1); <br/> fseek (p, C, 0 ); <br/> // printf ("% d/N", m); <br/> If (M <2) {<br/> fread (& A [0], p); <br/> printf ("the data of the pixel point (hexadecimal) is: % x ", A [0]); <br/> printf ("in decimal format: % d/N", a [0]); <br/> d = A [0]; <br/> printf ("the corresponding RGB value is:"); <br/> showrgbquan (& prgb [d]); <br/> printf ("/N"); <br/>}< br/> el Se {<br/> for (I = 0; I <3; I ++) {<br/> fread (& A [I], 1, 1, P ); <br/> // printf ("% x/N", a [I]); <br/>}< br/> printf ("hexadecimal representation: /n Blue: % x/n Green: % x/n red: % x/N ", a [0], a [1], a [2]); <br/> printf ("in decimal format:/n Blue: % d/n Green: % d/n red: % d/N", a [0], A [1], a [2]); <br/>}< br/> rewind (p); <br/> // W = ftell (P ); <br/> // printf ("% d", W); <br/> fclose (p); <br/> printf ("** enter any character to continue, to exit, enter n/n "); <br/> getchar (); <br/> scanf (" % C ", & Ch ); <br/>}< br/> printf ("Thank you for using it ~ /N "); <br/> system (" pause "); <br/> return 0; <br/>} 

 

 

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.