📄 lena.c
字号:
/* ======================================================================== */
/* TEXAS INSTRUMENTS, INC. */
/* */
/* NAME */
/* lena.c */
/* */
/* DATE */
/* June 2002 */
/* */
/* USAGE */
/* This header file contains an image. */
/* */
/* */
/* DESCRIPTION */
/* */
/* It is a 256x256 8-bit gray-level image. The image array is one */
/* dimensional and is defined as in_image[65536]. */
/* */
/* ------------------------------------------------------------------------ */
/* Copyright (c) 2002 Texas Instruments, Incorporated. */
/* All Rights Reserved. */
/* ======================================================================== */
#include "stdio.h"
#include "bmp.h"
#define bmpSize (144*128)
#define startPosiOffset 0x0a
#define imgWidthOffset 0x12
#define imgHeightOffset 0x16
#define bitsPixelOffset 0x1c
#pragma DATA_ALIGN(Bpixels, 8);
#pragma DATA_ALIGN(Gpixels, 8);
#pragma DATA_ALIGN(Rpixels, 8);
#pragma DATA_ALIGN(Bout, 8);
#pragma DATA_ALIGN(Gout, 8);
#pragma DATA_ALIGN(Rout, 8);
#pragma DATA_ALIGN(Bmatrix, 8);
#pragma DATA_ALIGN(Gmatrix, 8);
#pragma DATA_ALIGN(Rmatrix, 8);
#pragma DATA_SECTION(Bpixels,".imgbuf")
#pragma DATA_SECTION(Gpixels,".imgbuf")
#pragma DATA_SECTION(Rpixels,".imgbuf")
#pragma DATA_SECTION(Bout,".imgbuf")
#pragma DATA_SECTION(Gout,".imgbuf")
#pragma DATA_SECTION(Rout,".imgbuf")
#pragma DATA_SECTION(Bmatrix,".imgbuf")
#pragma DATA_SECTION(Gmatrix,".imgbuf")
#pragma DATA_SECTION(Rmatrix,".imgbuf")
unsigned char Bpixels[bmpSize];
unsigned char Gpixels[bmpSize];
unsigned char Rpixels[bmpSize];
unsigned char Bout[bmpSize];
unsigned char Gout[bmpSize];
unsigned char Rout[bmpSize];
short Bmatrix[sizeof(short)*bmpSize];
short Gmatrix[sizeof(short)*bmpSize];
short Rmatrix[sizeof(short)*bmpSize];
BMPhead bmphead;
void memclear(void *ptr, int count)
{
long *lptr = ptr;
#pragma MUST_ITERATE (32);
for (count>>=3; count>0; count--)
*lptr++ = 0;
}
int readbmp(char filename[])
{
FILE *fptr;
unsigned char test;
short matrixRow;
short matrixColumn;
int i,j,k,m,n;
int originAddr;
/* open a file on the host and read char array */
fptr = fopen(filename, "rb");
if(fptr== NULL){
printf("can not open file %s.\n", filename);
exit(1);
}
else
printf("open file %s succeeded.\n", filename);
/********** read BMP head ********************/
fseek(fptr,startPosiOffset,SEEK_SET);
fread(&bmphead.startPosition,1,4,fptr);
fseek(fptr,imgWidthOffset,SEEK_SET);
fread(&bmphead.width, 1, 4, fptr);
fseek(fptr,imgHeightOffset,SEEK_SET);
fread(&bmphead.height, 1, 4, fptr);
fseek(fptr,bitsPixelOffset,SEEK_SET);
fread(&bmphead.bitsPixel, 1, 2, fptr);
printf("bmp information:\n");
printf("data start position:%d\n",bmphead.startPosition);
printf("image width:%d\n",bmphead.width);
printf("image height:%d\n",bmphead.height);
printf("bits per pixel:%d\n",bmphead.bitsPixel);
if (bmphead.bitsPixel != 24)
{
fclose(fptr);
printf("This is not a 24bit BMP file.\n");
exit(1);
}
fseek(fptr,bmphead.startPosition,SEEK_SET);
printf("start to read pixels datas to address:\n");
printf("B:%x\n",&Bpixels[0]);
printf("G:%x\n",&Gpixels[0]);
printf("R:%x\n",&Rpixels[0]);
for(i=0;i<bmpSize;i++){
fread(&test,1,1,fptr);
Bpixels[i]=test;
fread(&test,1,1,fptr);
Gpixels[i]=test;
fread(&test,1,1,fptr);
Rpixels[i]=test;
}
printf("%d pixels read.\n",i);
if(!fclose(fptr))
{
printf("file closed.\n");
}
/////////////////////////////////////////////////
//data matrix formation and data type convertion
//to form 8x8 matrixes and contvert pixel datas to short
/////////////////////////////////////////////////
memclear(Bmatrix, sizeof(Bmatrix));
memclear(Gmatrix, sizeof(Gmatrix));
memclear(Rmatrix, sizeof(Rmatrix));
matrixRow = bmphead.height/8;
matrixColumn = bmphead.width/8;
n=0;
for(i=0;i<matrixRow;i++){
for(j=0;j<matrixColumn;j++){
for(k=0;k<8;k++){
for(m=0;m<8;m++,n++){
originAddr = (i*8+k)*bmphead.width+j*8+m;
Bmatrix[n] = (short)(Bpixels[originAddr]);
Gmatrix[n] = (short)(Gpixels[originAddr]);
Rmatrix[n] = (short)(Rpixels[originAddr]);
}
}
}
}
printf("8x8 matrix formed.\n");
printf("Bmatrix address:%x.\n",&Bmatrix[0]);
printf("Gmatrix address:%x.\n",&Gmatrix[0]);
printf("Rmatrix address:%x.\n",&Rmatrix[0]);
//////////////////////////////////////////////////////////////////////
//return nunbers of 8x8 blocks
//////////////////////////////////////////////////////////////////////
return(matrixRow*matrixColumn);
}
//////////////////////////////////////////////////////////////
//Reform bmp data from 8x8 Blocks and make data type conversion,
//from short to char.
//////////////////////////////////////////////////////////////
void bmpform(short Blue[],short Green[],short Red[])
{
short matrixRow;
short matrixColumn;
int i,j,k,m,n;
int originAddr;
matrixRow = bmphead.height/8;
matrixColumn = bmphead.width/8;
n=0;
for(i=0;i<matrixRow;i++){
for(j=0;j<matrixColumn;j++){
for(k=0;k<8;k++){
for(m=0;m<8;m++,n++){
originAddr = (i*8+k)*bmphead.width+j*8+m;
if(Blue[n]<0) Blue[n]=0;
if(Green[n]<0) Green[n]=0;
if(Red[n]<0) Red[n]=0;
Bout[originAddr] = Blue[n];
Gout[originAddr] = Green[n];
Rout[originAddr] = Red[n];
}
}
}
}
printf("bmp reformed.\n");
printf("Bout address: %x\n",&Bout[0]);
printf("Gout address: %x\n",&Gout[0]);
printf("Rout address: %x\n",&Rout[0]);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -