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