This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Technology & Calculator Open Topic subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. Community News & Discuss Nspire => Technology & Calculator Open Topic
Author Message
bwang


Member


Joined: 15 Mar 2009
Posts: 128

Posted: 08 Jun 2010 07:16:51 pm    Post subject:

The following program does not work:

Code:

//This program reads in a 24-bit bmp image file named test.tns in the directory bmpviewer

#include <os.h>
#include "utils.h"

asm(".string \"PRG\"\n");

int main(void)
{
 //bitmap header
 char* header = (char*) malloc(0x36);
 //screen buffer
 char* scrbuf = (char*) malloc(SCREEN_BYTES_SIZE);
 clearBuf(scrbuf);
 //image file
 FILE* img;
 //loop indices
 int i, x, y;
 //read the header
 img = fopen("/documents/bmpviewer/test.tns", "r");
 fread(header, 1, 0x36, img);
 //compute some ints (image data size, width, and height) from the header
 int bmpsize = header[34] +
 256 * header[35] +
 65536 * header[36] +
 65536 * 256 * header[37];
 int w = header[18] +
 256 * header[19] +
 65536 * header[20] +
 65536 * 256 * header[21];
 int h = header[22] +
 256 * header[23] +
 65536 * header[24] +
 65536 * 256 * header[25];
 //number of pixels
 int pxsize = bmpsize / 3;
 //allocate byte data and pixel (grayscale) data
 char* data = (char*) malloc(bmpsize);
 char* pxdata = (char*) malloc(pxsize);
 //read in data and close image
 fread(data, 1, bmpsize, img);
 fclose(img);
 //convert RGB to grayscale
 for (i = 0; i < pxsize; i++) {
 pxdata[i] = (data[3 * i] + data[3 * i + 1] + data[3 * i + 2]) / 48;
 }
 //draw to the buffer and display it
 for (x = 0; x < w; x++) {
 for (y = 0; y < h; y++) {
 setPixelBuf(scrbuf, x, h - 1 - y, pxdata[w * y + x]);
 }
 }
 refresh(scrbuf);
 while (!isKeyPressed(KEY_NSPIRE_ESC));
 return 0;
}

When I try reading the file into the "data" array, it reads a few thousand bytes and then stops working. Why is this so?
The program is attached. To run it, place demo-noncas.tns and test.tns in a folder called bmpviewer on the Nspire.


Last edited by Guest on 08 Jun 2010 07:27:49 pm; edited 1 time in total
Back to top
bsl


Advanced Newbie


Joined: 09 Jan 2010
Posts: 94

Posted: 08 Jun 2010 11:41:36 pm    Post subject:

This is a good example of using the printf() call for debugging.
printf() is the oldest debugging tool around.
You may be mixing ints and chars. If you typecast everything to an int it might work as expected.
Also open in binary mode: "rb" instead of "r" , img = fopen("/documents/bmpviewer/test.tns", "rb");

Code:

 int bmpsize = header[34] +
 256 * header[35] +
 65536 * header[36] +
 65536 * 256 * header[37];
 printf("bmpsize(no typecast)=%d\n",bmpsize); // check emulator console

  bmpsize = (int)header[34] +
 256 * (int)header[35] +
 65536 * (int)header[36] +
 65536 * 256 * (int)header[37];
 printf("bmpsize(with typecast)=%d\n",bmpsize); // check emulator console


Last edited by Guest on 08 Jun 2010 11:55:02 pm; edited 1 time in total
Back to top
calc84maniac


Elite


Joined: 22 Jan 2007
Posts: 770

Posted: 08 Jun 2010 11:54:29 pm    Post subject:

Or perhaps these pointers should be unsigned char?
Back to top
bwang


Member


Joined: 15 Mar 2009
Posts: 128

Posted: 08 Jun 2010 11:59:39 pm    Post subject:

Yay! Opening the files as "rb" did the trick :)

calc84maniac: How do you list all the files in a directory, like you did in gbc4nspire?


Last edited by Guest on 09 Jun 2010 12:11:58 am; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement