Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
14 years ago

write .bmp on sd card by ip sd card controller

Hi all!

I'm doing a project about " write.bmp from camera to sd card by use de2 board" i use IP Controller Sd card and I have some problem about my project:

- I interface DE2 board with Camera. it's Ok

- image is stored on Sdram and i use Sram buffer to display on Vga. it's Ok

-But when i write .bmp to on Sdcard. it's not Ok. I check sdcard. I see filesize of image, but i can't open that image.( maybe i write wrongly)

Give me some help, please..

8 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    my code:

    //================# include <io.h># include <system.h># include <stdio.h># include "gui.h"# include "altera_up_sd_card_avalon_interface.h"

    struct image images[MAX_NUM_IMAGES];

    struct image *current_image;

    struct image *prev_image = NULL;

    int num_images = 0;

    int current_image_num = 0;

    short int file_handle1;

    char name1[] = "filexx.bmp";

    // send image on Sdram to Sram_frame_buffer

    void send_image(int direction)

    {

    current_image = (direction == 2) ? current_image->next :

    current_image->prev;

    printf("current image: %d\n", current_image->image_num);

    int i = current_image->image_num;

    printf("sending image\n");

    unsigned char byte_r, byte_g, byte_b;

    int pixel;

    int x, y;

    int x_start = 0;

    int x_end = IMG_X_SIZE;

    int x_inc = 1;

    for (x = x_start; x < x_end ; x += x_inc)

    {

    for(y = 0; y < IMG_Y_SIZE; y += 1)

    {

    // send request

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 16, 1);

    // set requested x and y positions

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 10, x);

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 12, y);

    // get pixel from memory

    byte_r = saved_images[0][y][x];

    byte_g = saved_images[1][y][x];

    byte_b = saved_images[i][2][y][x];

    if (num_images == 0)

    pixel = 0;

    else

    pixel = ((byte_r >> 3) << 11) | ((byte_g >> 2) << 5) |((byte_b >> 3) << 0);

    // send pixel

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 14, pixel);

    // reset request

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 16, 0);

    }

    }

    printf("sent\n");

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    void save_image() //== store image on sdram

    {

    num_images++;

    unsigned char byte_r, byte_g, byte_b;

    int pixel;

    int x, y;

    // find next free slot

    int i;

    for (i = 0; i < MAX_NUM_IMAGES; i++)

    {

    if (!images.is_taken)

    {

    current_image = &images;

    break;

    }

    }

    if (prev_image == NULL)

    {

    current_image->prev = current_image;

    current_image->next = current_image;

    }

    else if (num_images == 2)

    {

    prev_image->prev = current_image;

    prev_image->next = current_image;

    current_image->prev = prev_image;

    current_image->next = prev_image;

    }

    else if (num_images >= 3)

    {

    current_image->prev = prev_image;

    current_image->next = prev_image->next;

    prev_image->next->prev = current_image;

    prev_image->next = current_image;

    }

    current_image->is_taken = 1;

    current_image->image_num = i;

    prev_image = current_image;

    printf("saving image: %d/%d\n", i, num_images);

    for (y = 0; y < IMG_Y_SIZE; y += 1)

    {

    for(x = 0; x < IMG_X_SIZE; x += 1)

    {

    // send request

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 4, 1);

    // set requested x and y positions

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 0, x);

    IOWR_16DIRECT(TV_TO_VGA_0_BASE, 2, y);

    // get pixel data

    pixel = IORD_16DIRECT(TV_TO_VGA_0_BASE, 6);

    // enter pixel data into image matrix

    byte_r = (unsigned char) (((pixel >> 11) & 0x1F) << 3);

    byte_g = (unsigned char) (((pixel >> 5) & 0x3F) << 2);

    byte_b = (unsigned char) (((pixel >> 0) & 0x1F) << 3);

    // save pixel

    saved_images[0][y][x] = byte_r;

    saved_images[1][y][x] = byte_g;

    saved_images[i][2][y][x] = byte_b;

    }

    }

    printf("saved\n");

    printf("number of images: %d\n", num_images);

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    my code:

    //==========================write image on sd card

    void write_image(int number)

    {

    char i = number;

    char h = 0;

    bool create, hum ;

    int x, y,j, c;

    alt_up_sd_card_dev *device_reference = NULL;

    int connected = 0;

    device_reference = alt_up_sd_card_open_dev("/dev/SD_Card_0");

    if (device_reference != NULL)

    {

    if ((connected == 0) && (alt_up_sd_card_is_Present()))

    {

    printf("Card connected.\n");

    if (alt_up_sd_card_is_FAT16())

    {

    printf("FAT16 file system detected.\n");

    }

    else

    {

    printf("Unknown file system.\n");

    }

    connected = 1;

    }

    else if ((connected == 1) && (alt_up_sd_card_is_Present() == false))

    {

    printf("Card disconnected.\n");

    connected = 0;

    }

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    //==============define header==============

    unsigned char BITMAP_HEADER[] = {

    0x42, 0x4D, 0xB6, 0x91, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00,

    0x00, 0x00, 0x50, 0x01, 0x00, 0x00, 0xE8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00,

    0x00, 0x00, 0x70, 0xAD, 0x00, 0x00, 0xEB, 0x0A, 0x00, 0x00, 0xEB, 0x0A, 0x00, 0x00, 0x00, 0x00,

    0x00, 0x00, 0x00, 0x00, 0x00, 0x00

    };

    printf("Bitmap header size is %d\n", (int) sizeof(BITMAP_HEADER));

    int file_size = (IMG_X_SIZE1*IMG_Y_SIZE1) * 3 + sizeof(BITMAP_HEADER);

    // write byte 0 (LSB)

    BITMAP_HEADER[FILE_SIZE_OFFSET+0] = (file_size >> 0) & 0xFF;

    // write byte 1

    BITMAP_HEADER[FILE_SIZE_OFFSET+1] = (file_size >> 8) & 0xFF;

    // write byte 2

    BITMAP_HEADER[FILE_SIZE_OFFSET+2] = (file_size >> 16) & 0xFF;

    // write byte 3 (MSB)

    BITMAP_HEADER[FILE_SIZE_OFFSET+3] = (file_size >> 24) & 0xFF;

    printf("Bitmap file size: %02X %02X %02X %02X\n",

    BITMAP_HEADER[FILE_SIZE_OFFSET+0],

    BITMAP_HEADER[FILE_SIZE_OFFSET+1],

    BITMAP_HEADER[FILE_SIZE_OFFSET+2],

    BITMAP_HEADER[FILE_SIZE_OFFSET+3]

    );

    // write byte 0 (LSB)

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+0] = (unsigned char)((IMG_X_SIZE1 >> 0) & 0xFF);

    // write byte 1

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+1] = (unsigned char)((IMG_X_SIZE1 >> 8) & 0xFF);

    // write byte 2

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+2] = (unsigned char)((IMG_X_SIZE1 >> 16) & 0xFF);

    // write byte 3 (MSB)

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+3] = (unsigned char)((IMG_X_SIZE1 >> 24) & 0xFF);

    printf("Bitmap width: %02X %02X %02X %02X\n",

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET],

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+1],

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+2],

    BITMAP_HEADER[PIXEL_WIDTH_OFFSET+3]

    );
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    / write byte 0 (LSB)

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+0] = (unsigned char)((IMG_Y_SIZE1 >> 0) & 0xFF);

    // write byte 1

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+1] = (unsigned char)((IMG_Y_SIZE1 >> 8) & 0xFF);

    // write byte 2

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+2] = (unsigned char)((IMG_Y_SIZE1 >> 16) & 0xFF);

    // write byte 3 (MSB)

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+3] = (unsigned char)((IMG_Y_SIZE1 >> 24) & 0xFF);

    printf("Bitmap height: %02X %02X %02X %02X\n",

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET],

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+1],

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+2],

    BITMAP_HEADER[PIXEL_HEIGHT_OFFSET+3]

    );
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    //========open file to write bitmap=====

    name1[4] = h/10 +48;

    name1[5] = h%10 +48;

    file_handle1 = alt_up_sd_card_fopen(name1, create);

    if (file_handle1 == -1)

    printf("file could not be open.\n");

    else if ((file_handle1 == -2))

    printf("file be already.\n");

    else

    printf("file be open.\n");

    if (create == true)

    printf("file be create .\n");

    //=========write header=============

    for (j=0; j<54; i++)

    {

    alt_up_sd_card_write( file_handle1, BITMAP_HEADER[j]);

    printf("write header.\n");

    }

    //============write data=================

    for (y = 0; y < IMG_Y_SIZE1; y += 1)

    {

    printf(".");

    for(x = 0; x < IMG_X_SIZE1; ++x)

    {

    for (c = 2; c >= 0; c--)

    {

    alt_up_sd_card_write( file_handle1, saved_images[i][c][y][x]);

    //printf("file be write .\n");

    }

    }

    }

    printf("\nDone!\n");

    hum = alt_up_sd_card_fclose(file_handle1);

    if (hum == true)

    printf("file be close.\n");

    if (h <100)

    h++;

    return 0;

    }
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Because, First I post document on forum, so i have some mistakes..

    I have to post my code more once. hic.

    Ok..Please, You help me? Thanks so much
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    could you please provide the link or the program files you have interfaced the sdcard for storage. I am working on the same project now.