C/C++二进制读写png文件
以下代码只有最简单的读写。地址定位啥的,个别注释中有。如果要改动png的格式甚么的就要再了解一下png的数据结构如果要十进制的话就跟着注释改一下:
/*!
* \file CC++二进制读写png文件.cpp
*
* \author ranjiewen
* \date 2017/02/12 13:08
*
*/ #include<iostream>
#include<fstream>
using namespace std;
typedef unsigned char byte; //class PngMsg
//{
//private:
// unsigned char markMsg[8]; //十进制,相当于16进制89.50.4e.47.0d.0a.1a.0a;
// char widthloc;
// char heigtMsgloc;
// char BitDepthloc;//图像深度
// char ColorTypeloc;
// char CompressionMethodloc;//压缩方法(LZ77派生算法)
// char FilterMethodloc;//滤波器方法
// char InterlaceMethodloc;
//public:
// PngMsg()
// {
// markMsg[0] = 137; markMsg[1] = 80; markMsg[2] = 78; markMsg[3] = 71; markMsg[4] = 13; markMsg[5] = 10; markMsg[6] = 26; markMsg[7] = 10;
// widthloc = 'a';
// heigtMsgloc = 'b';
// BitDepthloc = 'c';//图像深度
// ColorTypeloc = 'd';
// CompressionMethodloc = 'e';//压缩方法(LZ77派生算法)
// FilterMethodloc = 'f';//滤波器方法
// 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];//图像宽度,单位像素
// unsigned char height[4];//图像高度,单位像素
// unsigned char BitDepth;
// //图像深度
// //索引彩色1.2.4.8;灰度1.2.4.8.16;真彩色8.16
// unsigned char ColorType;
// //0灰度1.2.4.8.16;2真彩色8.16;3索引彩色1.2.4.8
// //4带α通道数据的灰度8.16;6带α通道数据的真彩色8.16
// unsigned char CompressionMethod;//压缩方法(LZ77派生算法)
// unsigned char FilterMethod;//滤波器方法
// unsigned char InterlaceMethod;//0:非隔行扫描;1:Adam7
//}; //===============================
//===============
//二进制读入。书上写ASCII码读取和二进制读取,如果对象是字母,那么一致。如果是数字,那么不一致
//书中说明【文件中数据的组织形式,分为ASCII文件(一个字节存放一个ASCII代码)和二进制文件(内部文件,存储形式原样在磁盘上存放),】
//字符,内存存储=ASCII=二进制形式
//数值数据,内存存储和ASCII码不同。
//样例内存整数100000.
//----------------------------------------------------------------
//内存地址 0x00 01 02 03
//内存 00000000 00000000 00100111 00010000【大端模式下】
//----------------------------------------------------------------
//二进制 00000000 00000000 00100111 00010000
//----------------------------------------------------------------
//ASCII 00110001 00110000 00110000 00110000 00110000 00110000【6个字节】
//ASCII码对应 1的49 0的48 0的48 0的48 0的48 0的48
//---------------------------------------------------------------- //只有含‘写’的不存在的文件会新建,其他会报错 //r只读;w只写;a尾增(附加/写);文本ASCII
//rb读;wb写;ab尾增;二进制
//以下读写↓
//r+;w+;a+;文本ASCII
//rb+;wb+;ab+二进制
void writeImage(byte*imgbuf, int size)
{
//FILE* fp = fopen(shaderFile, "wb");
//由于vs甚么安全性的原因,不让使用fopen,用下面的fopen_s代替;
FILE* imgPo;
fopen_s(&imgPo, "C_write_image.png", "wb");//这里是用二进制读取,read-r;binary-b;因为只弄r结果出错!!弄了后面那个的再来看这个才发现是这个的问题!!
if (imgPo == NULL)
return;
fwrite(imgbuf, sizeof(char), size, imgPo);
fclose(imgPo);
}
void readImageFile(const char* Imgname)
{
//FILE* fp = fopen(shaderFile, "rb");
//由于vs甚么安全性的原因,不让使用fopen,用下面的fopen_s代替;
FILE* imgP;
fopen_s(&imgP, Imgname, "rb");//这里是用二进制读取,read-r;binary-b;因为只弄r结果出错!!弄了后面那个的再来看这个才发现是这个的问题!!
if (imgP == NULL)
return;
fseek(imgP, 0L, SEEK_END);
long size = ftell(imgP);
byte* imgbuf = new byte[size + ];
fseek(imgP, 0x0L, SEEK_SET);//图片源
fread(imgbuf, sizeof(imgbuf[]), size, imgP);
/*for (int j = 0; j < size; j++)
cout << (imgbuf[j] & 0xff) << ":";*/
fclose(imgP); writeImage(imgbuf, size);
} //=========================================================== void WriteImage(byte*imgbuf, int size)
{ ofstream imgFo("C++_write_image.png", ios::binary);
if (!imgFo)
{
cerr << "open error!" << endl;
abort();
}
imgFo.write((char*)imgbuf, size);//一次性写入后面注释的是循环写入 /* 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));
//尝试这样输出的是否正确.
//byte是我自己给名的unsigned char,出来的是对的,用char也可以。都是一个字节。
}*/
imgFo.close();
}
void ReadImageFile(const char* Imgname)
{
ifstream imgF(Imgname, ios::binary);
if (!imgF) {
cerr << "open error!" << endl;
abort();
}
imgF.seekg(, ios::end);
int size = imgF.tellg();
//查了C++Library Reference才知道怎么得到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;//像素
cout << size << endl;*/
byte*imgbuf = new byte[size];
//imgF.seekg(0x10);
imgF.seekg(, ios::beg);
imgF.read((char*)imgbuf, size);//一次性读入,书上的不知是错的还是旧的不可行。后面注释的是循环读入
/*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);
} int main()
{
readImageFile("mm.png");//C/C++的
ReadImageFile("mm.png");//C++的
system("pause");
return ;
}
C/C++二进制读写png文件的更多相关文章
- C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)
1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...
- 用Python读写Excel文件(转)
原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...
- C语言采用文本方式和二进制方式打开文件的区别分析
稍微了解C程序设计的人都知道,文本文件和二进制文件在计算机上面都是以0,1存储的,那么两者怎么还存在差别呢?对于编程人员来说,文本文件和二进制文件就是一个声明,指明了你应该以什么方式(文本方式/二进制 ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- 文本读写vs二进制读写
[文本读写vs二进制读写] 在学习C语言文件操作后,我们都会知道打开文件的函数是fopen,也知道它的第二个参数是 标志字符串.其中,如果字符串中出现'b',则表明是以打开二进制(binary)文件, ...
- 用Python读写Excel文件的方式比较
虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...
- c++ 二进制方式读取文件 读取特殊类型数据
#include <iostream> #include <fstream> using namespace std; /* 二进制方式进行读写文件,可以读写 各种各样数据类型 ...
- C语言程序经过编译链接后形成二进制映像文件的组成
C语言程序经过编译链接后形成二进制映像文件由栈,堆,数据段,代码段组成,其中数据段又分为:只读数据段,已经初始化读写数据段,未初始化数据段(BSS段).如下图所示: 1.栈区(stack):由编译器自 ...
- Numpy怎样将数组读写到文件
Numpy怎样将数组读写到文件 本文档介绍的是Numpy以自己内建二进制的方式,将数组写出到文件,以及从文件加载数组: 如果是文本.表格类数据,一般使用pandas这个类库做加载和处理,不用numpy ...
随机推荐
- Ubuntu 14.04在虚拟机上的桥接模式下设置静态IP
1.虚拟机--->虚拟机设置 将虚拟机设置为桥接模式 2.查看window 网卡以及IP信息 cmd下输入 ipconfig -all 可以看到,我的网卡为Realtek PCIe GBE Fa ...
- java中ArrayList、LinkedList、Vector的区别
ArrayList.LinkedList.Vector这三个类都实现了List接口. ArrayList是一个可以处理变长数组的类型,可以存放任意类型的对象.ArrayList的所有方法都是默认在单一 ...
- Oracle中Restore和Recovery的区别
一.参考解释一 在Oracle的备份与恢复的知识点中,经常会出现Restore 和 Recovery两个词. 由于这两个词在字典中的解释很接近,困扰了我很久.直到我在Oracle的官方文档中看到了以下 ...
- 输入url后发生了什么
(1)浏览器解析 (2)查询缓存 (3)DNS查询 顺序如下,若其中一步成功直接进去建立连接部分: -- 浏览器自身DNS -- 操作系统DNS -- 本地hosts文件 -- 像域名服务器发送请求 ...
- PYDay6- 内置函数、验证码、文件操作、发送邮件函数
1.内置函数 1.1Python的内置函数 abs() dict() help() min() setattr() all() dir() hex() next() slice() any() div ...
- Java-构造一个字符串
实用StringBuffer构造字符串 package com.tj; public class MyClass implements Cloneable { public static void m ...
- 【死磕 Spring】
[死磕 Spring]----- IOC 之深入理解 Spring IoC-------https://www.cnblogs.com/chenssy/p/9576769.html 1.Resourc ...
- Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vect ...
- php在字符串中替换多个字符
php替换多个字符串str_replace函数 echo str_replace(array("m","i"),array("n",&quo ...
- Leetcode 394.字符串编码
字符串编码 给定一个经过编码的字符串,返回它解码后的字符串. 编码规则为: k[encoded_string],表示其中方括号内部的 encoded_string 正好重复 k 次.注意 k 保证为正 ...