Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

read bitmap from university program sdcard de2 nios

Status
Not open for further replies.

yupina-chan

Member level 2
Member level 2
Joined
Nov 27, 2013
Messages
51
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Visit site
Activity points
397
hi. i want to read a bitmap image from my sdcard. i was able to write to sdcard as in the tutorial. my source code for reading is as follows:

Code:
#include <stdio.h>
#include <system.h>
#include <altera_up_sd_card_avalon_interface.h>

#pragma pack(2)

typedef struct{
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
} BMPFileHeader;

typedef struct{
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} BMPInfoHeader;


short int sd_fileh;
short int header;
int x;
int main()
{
printf("SD Card Access Test\n");

alt_up_sd_card_dev *sd_card_dev = alt_up_sd_card_open_dev(SD_CARD_NAME);

if(sd_card_dev != 0)
{
if(alt_up_sd_card_is_Present())
{
if(alt_up_sd_card_is_FAT16())
printf("Card is FAT16\n");
else
printf("Card is not FAT16\n");

sd_fileh = alt_up_sd_card_fopen("pic.bmp", false);

if (sd_fileh < 0)
printf("Problem creating file. Error %i", sd_fileh);
else
{
printf("SD Accessed Successfully, reading BMP image...");
for(x=0;x<54;x++)
{
header = (unsigned char)(alt_up_sd_card_read(sd_fileh));
}
printf("Size: %ld\n", sizeof(BMPFileHeader));

}
printf("Done!\n");

printf("Closing File...");
alt_up_sd_card_fclose(sd_fileh);
printf("Done!\n");
}

}

return 0;
}

it builds without errors and the sd card was accessed successfully but the returned bitmap size was wrong. my image is 20.3 kb while the returned size was 14kb. please guide me in correcting my code especially in the reading part. im not familiar with c. thanks in advance!
my
 
Last edited:

hi. i want to read a bitmap image from my sdcard. i was able to write to sdcard as in the tutorial. my source code for reading is as follows:

Code:
#include <stdio.h>
#include <system.h>
#include <altera_up_sd_card_avalon_interface.h>

#pragma pack(2)

typedef struct{
unsigned short int type; /* Magic identifier */
unsigned int size; /* File size in bytes */
unsigned short int reserved1, reserved2;
unsigned int offset; /* Offset to image data, bytes */
} BMPFileHeader;

typedef struct{
unsigned int size; /* Header size in bytes */
int width,height; /* Width and height of image */
unsigned short int planes; /* Number of colour planes */
unsigned short int bits; /* Bits per pixel */
unsigned int compression; /* Compression type */
unsigned int imagesize; /* Image size in bytes */
int xresolution,yresolution; /* Pixels per meter */
unsigned int ncolours; /* Number of colours */
unsigned int importantcolours; /* Important colours */
} BMPInfoHeader;


short int sd_fileh;
short int header;
int x;
int main()
{
printf("SD Card Access Test\n");

alt_up_sd_card_dev *sd_card_dev = alt_up_sd_card_open_dev(SD_CARD_NAME);

if(sd_card_dev != 0)
{
if(alt_up_sd_card_is_Present())
{
if(alt_up_sd_card_is_FAT16())
printf("Card is FAT16\n");
else
printf("Card is not FAT16\n");

sd_fileh = alt_up_sd_card_fopen("pic.bmp", false);

if (sd_fileh < 0)
printf("Problem creating file. Error %i", sd_fileh);
else
{
printf("SD Accessed Successfully, reading BMP image...");
for(x=0;x<54;x++)
{
header = (unsigned char)(alt_up_sd_card_read(sd_fileh));
}
printf("Size: %ld\n", sizeof(BMPFileHeader));

}
printf("Done!\n");

printf("Closing File...");
alt_up_sd_card_fclose(sd_fileh);
printf("Done!\n");
}

}

return 0;
}

it builds without errors and the sd card was accessed successfully but the returned bitmap size was wrong. my image is 20.3 kb while the returned size was 14kb. please guide me in correcting my code especially in the reading part. im not familiar with c. thanks in advance!
my

i amazed how you managed to come so far without knowing c...
so i guess it's learning time now for you, just loo for some c tutrials...
 

thank you for the reply but i think ive read enough c tutorials now but i still cant get it right. my problems are:

sd_fileh = alt_up_sd_card_fopen("pic.bmp", false);
returns an int

and the fopen cannot be used.

i dont know how to translate this:
filePtr = fopen(filename,"rb");

and
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);

i know the functions of these in c but in hal, i dont know where to put this or use this.
 

thank you for the reply but i think ive read enough c tutorials now but i still cant get it right. my problems are:

see my sugestions : problems 1 and 2

1
Code:

Code C - [expand]
1
2
3
short int sd_fileh;
 
sd_fileh = alt_up_sd_card_fopen("pic.bmp", false);


Code:
returns an int


and the fopen cannot be used.

i dont know how to translate this:
filePtr = fopen(filename,"rb");

Code:

Code C - [expand]
1
2
3
short int filePtr;
 
filePtr = alt_up_sd_card_fopen(filename, false);


Code:

2
and
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER),1,filePtr);
Code:

Code C - [expand]
1
2
3
4
5
6
short int byte_to_read[100];
 
for(i=0;i<sizeof(BITMAPFILEHEADER);i++)
{
  byte_to_read[i] = alt_up_sd_card_read(filePtr);
}


Code:
i know the functions of these in c but in hal, i dont know where to put this or use this.

hope i helped....
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top