#include"Widget_Image.h"
#include"TableView.h"
#include"common.h"
#include"MainWindow.h"
#include"FileStructs/FilePNG.h"
#include"FileStructs/FileBMP.h"
#include"ColorSpaces/ColorHS.h"
#include"Number.h"
#include<QToolTip>
#include<QMouseEvent>
#include<QPaintEvent>
#include<QPainter>
#include<QMessageBox>
#include<QDebug>
#include<bitset>
using namespace std;
#include<math.h>
#include<time.h>
Widget_Image::Widget_Image(QWidget *parent):QWidget(parent),zoomFactor(1),penColor(Qt::black),tolerance(0),editMode(Edit_Pen),dealMaskOnly(false),flashTimerID(0),flashStatus(false){
setMouseTracking(true);
tableViewColor=new TableView_Color();
tableViewColor->setAttribute(Qt::WA_DeleteOnClose);
tableViewColor->setWidgetImage(*this);
penColor.setAlpha(0xFF);
//开始闪烁
flashTimerID=startTimer(500);
}
Widget_Image::~Widget_Image(){stopFlash();}
void Widget_Image::maskImageResize(){
if(image.size()!=maskImage.size()){
maskImage=QImage(image.size(),QImage::Format_RGBA8888);
}
maskImage.fill(Qt::transparent);
resize(image.size());
update();
}
void Widget_Image::loadImage(const QString &filename){
if(image.load(filename)){
maskImageResize();
}
}
void Widget_Image::saveImage(const QString &filename,const QString &code)const{
if(!image.save(filename))return;
//fix some thing
FilePNG filePng;
filePng.loadFile(filename.toStdString());
filePng.parseData();
//remove pHYs chunk
filePng.removeUnnecessaryChunk();
//re-save file
filePng.saveFilePNG(filename.toStdString());
filePng.memoryFree();
}
void Widget_Image::loadPalette(const QString &filename){
//打开文件
QFile file(filename);
if(!file.open(QIODevice::ReadOnly)){
QMessageBox::critical(getTopWidget(*this),tr("调色板加载失败"),file.errorString());
return;
}
//读取数据
colorsList.clear();
do{
auto lineData=file.readLine();lineData.replace('\n',"");//去掉换行符号
qDebug()<<lineData.size();
//开始读取
ColorName cn;
auto lst=lineData.split(':');
if(lst.size()>0)cn.name=lst[0];//名字
if(lst.size()>1){
lst=lst[1].split(',');//读取颜色
if(lst.size()==4){
QColor color;
color.setRed(lst[0].toUInt());
color.setGreen(lst[1].toUInt());
color.setBlue(lst[2].toUInt());
color.setAlpha(lst[3].toUInt());
cn.color=qColor2uint32(color);
}
}
colorsList.push_back(cn);
}while(!file.atEnd());
//完成
file.close();
}
void Widget_Image::savePalette(const QString &filename)const{
//打开文件
QFile file(filename);
if(!file.open(QIODevice::WriteOnly)){
QMessageBox::critical(getTopWidget(*this),tr("调色板保存失败"),file.errorString());
return;
}
//写入内容
for(auto &data:colorsList){
if(file.pos()>0)file.write("\n");
auto color=uint2QColor(data.color);
file.write(QString().sprintf("%s:%d,%d,%d,%d",data.name.toStdString().data(),color.red(),color.green(),color.blue(),color.alpha()).toLocal8Bit());
}
//完成
file.flush();
file.close();
}
void Widget_Image::loadAllTiles(const QString &filename){
//获取图块尺寸
QImage img(filename);
auto sz=img.size();
sz.setWidth(sz.height());//以h*h的方式去分割出各个图块
splitToTiles(sz);
}
void Widget_Image::saveAllTiles(const QString &filename)const{
auto amount=allTiles.size();
if(amount<=0)return;
//以长方形格式保存
QSize sz=allTiles.at(0).size();
QImage img(sz.width()*amount,sz.height(),QImage::Format_RGBA8888);//横向
//QImage img(sz.width(),sz.height()*amount,QImage::Format_RGBA8888);//纵向
QPainter painter(&img);//开始绘图
for(int i=0;i<amount;++i){
painter.drawImage(sz.width()*i,0,allTiles.at(i));//横向
//painter.drawImage(0,sz.height()*i,allTiles.at(i));//纵向
}
//保存
img.save(filename);
}
void Widget_Image::loadSelectRegion(const QString &filename){maskImage.load(filename);}
void Widget_Image::saveSelectRegion(const QString &filename)const{maskImage.save(filename);}
void Widget_Image::loadFilePng(const FilePNG &filePng){
Bitmap_32bit bitmap32;
filePng.decodeTo(bitmap32);//解码
fromBitmap32(bitmap32);//生成QImage数据
makeColorsList(filePng);//生成颜色表
bitmap32.deleteBitmap();//内存回收
maskImageResize();//刷新
}
void Widget_Image::makeColorsList(const List<uint32> &clrList){
colorsList.clear();
for(auto &value:clrList){
colorsList.push_back(value);
}
}
void Widget_Image::makeColorsList(const FilePNG &filePng){
List<uint32> clrList;
if(filePng.ihdr && filePng.plte.hasData()){
filePng.plte.getColorsList(*filePng.ihdr,clrList);
}
makeColorsList(clrList);
}
void Widget_Image::makeColorsList(const FileBMP &fileBmp){
List<uint32> clrList;
if(fileBmp.bgrasList.dataLength){
fileBmp.bgrasList.getColorsList(clrList);
}
makeColorsList(clrList);
}
void Widget_Image::makeColorsList(const QImage &image){
colorsList.clear();
if(image.colorCount()){//使用QImage的色表图
auto colorTable=image.colorTable();
for(auto rgb:colorTable){
colorsList.push_back(qRgb2uint32(rgb));
}
}else{//统计QImage中的颜色数
colorsMap.clear();
uint32 u32;
//开始扫描
decltype(colorsMap.begin()) itr;
forEachPixel(tr("makeColorsList"),image.rect(),[&](int x,int y,QColor &color){
u32=qColor2uint32(color);
itr=colorsMap.find(u32);
if(itr==colorsMap.end()){
colorsMap.insert(std::pair<uint32,SizeType>(u32,1));
}else{
++itr->second;
}
});
//添加列表
colorsList.clear();
for(auto &keyVal:colorsMap){
colorsList.push_back(keyVal.first);
}
}
}
void Widget_Image::saveFilePng(const QString &filename,uint8 bitDepth,bool hasPalette,bool hasColor,bool hasAlpha,List<ColorName> *colorsList)const{
List<uint32> clrList;
if(colorsList)toUint32List(clrList);
//转换成Bitmap32格式
Bitmap_32bit bitmap32;
bitmap32.coordinateType=Bitmap_32bit::CoordinateType_Screen;
toBitmap32(bitmap32);
//保存
FilePNG filePng;
filePng.encodeFrom(bitmap32,bitDepth,hasPalette,hasColor,hasAlpha,colorsList ? &clrList:NULL);
filePng.saveFilePNG(filename.toStdString());
//内存回收
bitmap32.deleteBitmap();
filePng.memoryFree(true);
}
void Widget_Image::loadFileBmp(const QString &filename){
//加载文件并分析
FileBMP fileBmp;
fileBmp.loadFile(filename.toStdString());
fileBmp.parseData();
//进行解码
Bitmap_32bit bitmap32;
fileBmp.decodeTo(bitmap32);
bitmap32.coordinateType=Bitmap_32bit::CoordinateType_Screen;//Qt使用屏幕坐标系
//生成图像用于显示
fromBitmap32(bitmap32);
update();
//生成色表
makeColorsList(fileBmp);
//内存回收
bitmap32.deleteBitmap();
fileBmp.memoryFree();
}
void Widget_Image::saveFileBmp(const QString &filename,uint16 bitCount,List<ColorName> *colorsList)const{
List<uint32> clrList;
if(colorsList)toUint32List(clrList);
//转换成Bitmap32格式
Bitmap_32bit bitmap32;
toBitmap32(bitmap32);
//保存
FileBMP fileBmp;
fileBmp.encodeFrom(bitmap32,bitCount,colorsList ? &clrList:NULL);
fileBmp.saveFileBMP(filename.toStdString());
//内存回收
bitmap32.deleteBitmap();
fileBmp.memoryFree(true);
}
template<typename T>
void mapAdd(map<T,SizeType> &theMap,const T &value){
auto itr=theMap.find(value);
if(itr==theMap.end()){
theMap.insert(pair<T,SizeType>(value,1));
}else{
++itr->second;
}
}
//扫描过程,扫描出图像特征
#define PRINT(format,...) qDebug(format,__VA_ARGS__);
void Widget_Image::scan(){
int pixelAmount=image.width()*image.height();
PRINT("分辨率:%d*%d==%d",image.width(),image.height(),pixelAmount)//分辨率
//记录信息用的变量
int alpha0Amount=0,alpha255Amount=0,alphaOtherAmount=0;//全透明数,不透明数,局部透明数
int trnsHiddenAmount=0;//透明度为0时的隐藏像素数
int grayAmount=0;//灰度数
map<uint32,SizeType> rgbMap;//统计各个rgb值出现的数量
map<int,SizeType> hueMap;//统计各个色相值出现的数量
//开始扫描
//QPoint pos;
maskImage.fill(Qt::transparent);
forEachPixel("扫描中",image.rect(),[&](int x,int y,QColor &color){
//统计透明度
int alpha=color.alpha();
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![dmg](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![folder](https://csdnimg.cn/release/downloadcmsfe/public/img/folder.005fa2e5.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
共 23 条
- 1
资源评论
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/de50ac10e30e41b89db28203f97239be_qq_41701956.jpg!1)
Java程序员-张凯
- 粉丝: 1w+
- 资源: 6828
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)