A/C + + binary read and write PNG file

Source: Internet
Author: User

The following code has only the simplest read and write. The address is located what the individual notes have. If you want to change the format of PNG what you need to know about PNG data structure if you want to decimal, then change the comment:

/*! * \file cc++ binary read/write PNG file. cpp * * \author Ranjiewen * \date 2017/02/12 13:08 **/#include<iostream>#include<fstream>using namespacestd;typedef unsignedChar byte;//class Pngmsg//{//Private://unsigned char markmsg[8];//decimal, equivalent to 16 binary 89.50.4e.47.0d.0a.1a.0a;//Char Widthloc;//Char Heigtmsgloc;//Char Bitdepthloc;//Image Depth//Char Colortypeloc;//Char Compressionmethodloc;//compression method (LZ77 derivation algorithm)//Char Filtermethodloc;//Filter Method//Char Interlacemethodloc;//Public ://pngmsg ()//    {//markmsg[0] = 137; markmsg[1] = n; markmsg[2] = n; markmsg[3] =; Markmsg[4] = [markmsg[5] = ten; markmsg [6] = 26; MARKMSG[7] = ten;//widthloc = ' a ';//heigtmsgloc = ' B ';//bitdepthloc = ' C ';//Image Depth//colortypeloc = ' d ';//compressionmethodloc = ' e ';//compression method (LZ77 derivation algorithm)//Filtermethodloc = ' f ';//Filter Method//interlacemethodloc = ' g ';//    }//Long int getmsg (char loc)//    {//if (loc = = ' a ') return 0x10;//if (loc = = ' B ') return 0x14;//if (loc = = ' C ') return 0x15;//if (loc = = ' d ') return 0x16;//if (loc = = ' E ') return 0x17;//if (loc = = ' F ') return 0x18;//if (loc = = ' G ') return 0x19;//    }//unsigned char width[4];//image width, per pixel//unsigned char height[4];//image height, per pixel//unsigned char BitDepth;//    //Image Depth//    //indexed color 1.2.4.8; gray 1.2.4.8.16; True Color 8.16//unsigned char colortype;//    //0 Grayscale 1.2.4.8.16;2 True Color 8.16;3 index color 1.2.4.8//    //4 grayscale 8.16;6 with alpha channel data true Color 8.16 with alpha channel data//unsigned char compressionmethod;//compression method (LZ77 derivation algorithm)//unsigned char filtermethod;//Filter Method//unsigned char interlacemethod;//0: Non-interlaced scanning; 1:ADAM7//};//===============================  //===============  //Binary read-in. The book writes ASCII code read and binary read, if the object is a letter, then the same. If it's a number, it's inconsistent.//The book explains "the organization of data in a file, which is divided into ASCII files (one byte holds an ASCII code) and binary files (internal files, stored as-is on disk),"//characters, memory storage =ascii= binary form//numeric data, memory storage and ASCII code are different. //sample Memory integer 100000. //----------------------------------------------------------------  //memory address 0x00 in Geneva//Memory 00000000 00000000 00100111 00010000 "big-endian mode"//----------------------------------------------------------------  //Binary 00000000 00000000 00100111 00010000//----------------------------------------------------------------  //ASCII 00110001 00110000 00110000 00110000 00110000 00110000 "6 bytes"//The ASCII code corresponds to 1 of 49 0 of 48 0 of 48 0 for 48 0 of the 48 0//----------------------------------------------------------------  //only non-existent files with ' write ' will be created and others will be error-free.//R Read only; W write only; a tail increase (append/write); text ASCII//RB read; WB write; ab tail increase; binary//Read the following ↓//r+;w+;a+; text ASCII//rb+;wb+;ab+ BinaryvoidWriteimage (byte*imgbuf,intsize) {    //file* fp = fopen (Shaderfile, "WB"); //because of the security reasons for VS, do not let the use of fopen, with the following fopen_s instead;file*Imgpo; Fopen_s (&imgpo,"C_write_image.png","WB");//here is the binary read, Read-r;binary-b, because only the R result is wrong!!  Get the back one to see this again to find this is the problem!!     if(Imgpo = =NULL)return; Fwrite (Imgbuf,sizeof(Char), size, Imgpo); Fclose (IMGPO);}voidReadimagefile (Const Char*imgname) {    //file* fp = fopen (Shaderfile, "RB"); //because of the security reasons for VS, do not let the use of fopen, with the following fopen_s instead;file*IMGP; Fopen_s (&AMP;IMGP, Imgname,"RB");//here is the binary read, Read-r;binary-b, because only the R result is wrong!!  Get the back one to see this again to find this is the problem!!     if(IMGP = =NULL)return; Fseek (IMGP,0L, Seek_end); LongSize =Ftell (IMGP); byte* Imgbuf =New byte[Size +1]; Fseek (IMGP,0x0l, Seek_set);//Picture SourceFread (Imgbuf,sizeof(imgbuf[0]), size, IMGP); /*For (int j = 0; J < size; J + +) cout << (Imgbuf[j] & 0xff) << ":";*/fclose (IMGP); Writeimage (imgbuf, size);}//===========================================================  voidWriteimage (byte*imgbuf,intsize) {Ofstream Imgfo ("C++_write_image.png", ios::binary); if(!Imgfo) {Cerr<<"Open error!"<<Endl;    Abort (); } imgfo.write ((Char*) imgbuf, size);//a one-time write is followed by a circular write    /*for (int i = 0; i < size; i++) {Char ct = (Imgbuf[i] & 0xFF);    Imgfo.write (&ct, sizeof (char));    byte ct = (Imgbuf[i] & 0xFF);    Imgfo.write ((char*) &ct, sizeof (byte));    Try to output the correctness of this. Byte is the name of my own unsigned char, out of the right, with Char can also.    is a byte. }*/imgfo.close ();}voidReadimagefile (Const Char*imgname)    {Ifstream IMGF (imgname, ios::binary); if(!IMGF) {Cerr<<"Open error!"<<Endl;    Abort (); } imgf.seekg (0, Ios::end); intSize =Imgf.tellg (); //Check c++library reference only know how to get size.     /*int pixscnt;    Byte width[4], height[4];    IMGF.SEEKG (0x10);    Imgf.read ((char*) &width, sizeof (width));    IMGF.SEEKG (0x14);    Imgf.read ((char*) &height, sizeof (height));    for (int i = 0; i < 4; i++) cout << (Width[i] & 0xff) << ":";    for (int i = 0; i < 4; i++) cout << (Height[i] & 0xff) << ":";    pixscnt = (width[2] * (0x100) + width[3]) * (height[2] * (0x100) + height[3]); cout << pixscnt << endl;//pixels cout << size << Endl;*/    byte*imgbuf =New byte[size]; //imgf.seekg (0x10); IMGF.SEEKG (0, Ios::beg); Imgf.read ((Char*) imgbuf, size);//One-time reading, the book of the unknown is wrong or the old is not OK. The following comment is the loop read-in    /*for (int i = 0; i<size; i++) Imgf.read ((char*) &imgbuf[i], sizeof (byte));*/Imgf.close (); /*for (int i = 0; i < size; i++) {cout << hex << (imgbuf[i] & 0xff) << ":";    if (i% 4 = = 0) cout << Endl; } */writeimage (imgbuf, size);}intMain () {Readimagefile ("Mm.png");//C + + 'sReadimagefile ("Mm.png");//of C + +System"Pause"); return 0;}

A/C + + binary read and write PNG file

Related Article

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.