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

cc++ 图片内存的分配,赋值,检验赋值,第1张

//以下在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,%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;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1325259.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存