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

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


#include <iostream>

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,%d\n",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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值