栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c/c++ 图片内存的分配,赋值,检验赋值

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c/c++ 图片内存的分配,赋值,检验赋值

//以下在c++ buider调试成功。


#include

void *pImgDestBuffer;//图片分配内存首地址
 int length=3*10000*10000;//预设图片所占内存的大小,
 //假设图片w=10000,H=10000,R,G,B三色各占一个字节
 //int的范围为-2147483648 ~ 2147483647,length不会溢出


int _tmain(int argclength, _TCHAR* argv[])
{   pImgDestBuffer=malloc(length);//分配内存
    if ( pImgDestBuffer==0)
    {  return -1;  //内存没有分配成功,返回
    }
    unsigned char * tempBuffer =(unsigned char*)pImgDestBuffer ;//转换void * 到unsigned          char * unsigned char t=0;

    //赋值
    for (int i=0; i < length; i++)
    {   t= (unsigned char )128;
        *tempBuffer =t;
         tempBuffer++;//指针加1
    }

    tempBuffer=( unsigned char * ) pImgDestBuffer ;

    //打印第1个像素
    t1=*tempBuffer;
    tempBuffer++;
    t2=*tempBuffer;
    tempBuffer++;
    t3=*tempBuffer;
    tempBuffer++;
    printf("first pix:%d,%d,%dn",t1,t2,t3);


    printf("middle colmun pixes:n");
    int rowlength=  width*3;
    int Colcentre = rowlength/2;
    tempBuffer=( unsigned char * ) pImgDestBuffer;
    tempBuffer+= Colcentre;
     //打印中间列
     for (int i = 0; i < height; i++)
     {
      t1=*tempBuffer;
      tempBuffer++;
      t2=*tempBuffer;
      tempBuffer++;
      t3=*tempBuffer;
      tempBuffer++;
      tempBuffer+=rowlength;
      sprintf(str,"|%d,%d,%d| ",t1,t2,t3);
      printf(str);
     }
     printf("n");
    printf("middle row pixes:n");
    tempBuffer=( unsigned char * ) pImgDestBuffer;
    int middleRow =length/2;
    tempBuffer += middleRow;
     //打印中间行
     for (int i = 0; i < width; i++)
     {
      t1=*tempBuffer;
      tempBuffer++;
      t2=*tempBuffer;
      tempBuffer++;
      t3=*tempBuffer;
      tempBuffer++;
      sprintf(str,"|%d,%d,%d| ",t1,t2,t3);
      printf(str);
     }
     printf("n");


    free(pImgDestBuffer);//释放内存
    system("pause");
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/954337.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号