What did you change if anything? What do you mean by slides up and out of view can you post a screenshot or video?
It goes up until it is out of view:
I don't understand are you calling the function many times? Did you make any changes to the loading and saving code? It looks like the problem is that it may not be loading to the beginning of the data pointer but I am not sure yet.
I don't think it's doing it many times. I do it once and it does that until it stops moving, and when I do it a second time it works perfectly fine.
*BUMP*
Small update:
I got it to load a new image. Razz

More screenshots(doodles):

Zoomed:
That's simply awesome! Awesome work so far! What else will you be adding to it?
That really looks great, very impressive work. I second tifreak's question, plus I also wonder when we'll be getting to try out a demo of the program in action.
Well, I'm hoping to get rotating to work, but I can't seem to find/make a routine for that. Also, later, I'll probably put an eraser tool(which I probably should have put in before), and maybe a text tool. I can't remember anything else I might put it, but any suggestions will be taken into consideration.

Problems I'm having:
Resizing the array(it gives me a segfault)
Rotating(can't find/make a good routine)
Weird glitching problem(https://img.ourl.ca/rfg/PaintFillGlitch.png)
(Can't think of anymore)

If anyone can help, it would be appreciated. Very Happy

Edit: I just saw Kerm's reply. Razz Um, I might release a demo once I get at least resizing and the weird glitching problem. Very Happy

Also, maybe once I get a demo out, Kerm could add some ppf file support for SourceCoder(maybe some convert to and from). Razz

P.S. If you want me to get this done faster, you can always help. Razz Laughing Very Happy
*BUMP*

I know it hasn't been 24 hours, but I'm trying to get it to save BMP. The problem is that I try to open it and it says that it is corrupt.

Here is my saving code:

Code:
void saveBMP(char *filename, color_t *data, int width, int height){
    BMPHEAD bh;
    memset ((char *)&bh,0,sizeof(BMPHEAD)); /* sets everything to 0 */
    memcpy (bh.id,"BM",2);
    bh.reserved[0]  = 0;
    bh.reserved[1]  = 0;
    bh.headersize  = 54L;
    bh.infoSize  =  0x28L;
    bh.width     = width;
    bh.depth     = height;
    bh.biPlanes  =  1;
    bh.bits      = 24;
    bh.biCompression = 0L;
    int bytesPerLine;

    bytesPerLine = bh.width * 3;  /* (for 24 bit images) */
    /* round up to a dword boundary */
    if (bytesPerLine & 0x0003)
    {
        bytesPerLine |= 0x0003;
        ++bytesPerLine;
    }
    bh.filesize=bh.headersize+(long)bytesPerLine*bh.depth;
    int size = bh.headersize+(long)bytesPerLine*bh.depth;
    char * PATH;
    PATH = alloca(strlen("\\\\fls0\\")+strlen1(filename)+strlen(".bmp")+1);
    strcpy(PATH, "\\\\fls0\\");
    strcat(PATH, filename);
    strcat(PATH, ".bmp");
    unsigned short * pFile = alloca(2*(strlen(PATH)+1));
    Bfile_StrToName_ncpy(pFile, (unsigned char*)PATH, strlen(PATH)+1);
    Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size);
    int hFile = Bfile_OpenFile_OS(pFile, 2);
    Bfile_WriteFile_OS(hFile, &bh, sizeof(bh));
    char *linebuf;
    linebuf = (char *) malloc(bytesPerLine*sizeof(char));
    if (linebuf == NULL){
        Bfile_CloseFile_OS(hFile);
        return;
    }
    int line;
    for (line = height; line >= 0; line --){
        for(int i = 0; i < width; i++){
            linebuf[i] = data[line*width+i];
        }
        Bfile_WriteFile_OS(hFile, linebuf, sizeof(linebuf));
    }
    Bfile_CloseFile_OS(hFile);
    free(linebuf);
}


Using this website for basis: http://www.siggraph.org/education/materials/HyperVis/asp_data/compimag/bmpfile.htm

I don't think loading the data is going correctly. Can someone please help?
*BUMP*
Here's a comparison of a plain white 100*100 file saved in my (Prizm)Paint and a file saved in (MS)Paint(mine is on top):


Can someone help me with this problem?
Could you please post the files open with a hexadecimal editor instead of notepad?
Here is mine:

And here is MS Paint's:


Can you help?

Also, I need help saving the image's data in hex form. Could you, or someone else, help with that too?
Okay. I still need help with saving BMPs, but I (kinda) got opening working. It only opens 16 bit pics and when it opens it does this:

(that's me moving the cursor)

Can someone help(with this and saving)?
When you get this working amazingly...this will be in my opinion THE program to play with on Prizm Very Happy
Thanks!

I'm still trying to get BMP saving to work and it seems to almost get it right, but not quite.

Edit: I've got it to save viewable BMP images, but the colors are all screwed up. D:



Here is the code I am using(it's a port of Tari's mkg3a saving code):

Code:

#pragma pack(1)

struct bmp_header {
    uint8_t signature[2];
    uint32_t file_size;
    uint16_t reserved1;
    uint16_t reserved2;
    uint32_t px_offset;
};

struct dib_header {
    uint32_t header_size;
    int32_t width;
    int32_t height;
    uint16_t nplanes;
    uint16_t bpp;
    uint32_t compress_type;
    uint32_t bmp_byte_size;
    int32_t hres;
    int32_t vres;
    uint32_t ncolors;
    uint32_t nimpcolors;
};

#pragma pack()

uint8_t convertChannelDepth(uint8_t c, uint8_t cd, uint8_t dd) {
    float v = (float)c / ((1 << cd) - 1);
    v *= (1 << dd) - 1;
    return (uint8_t)v;
}

void writeBitmap(char *path, color_t *data, int w, int h) {
    // TODO: don't write broken files on big-endian systems
    uint8_t *cdata;
    int x, y;
    uint32_t imgSize = w * h * 3;
    uint32_t fullsize = (imgSize + sizeof(struct dib_header));
    struct bmp_header bh = {
        {0x42, 0x4D},                                       // Signature
        (((fullsize+sizeof(bh) >> 24) & 0xFF) | ((fullsize+sizeof(bh) << 8) & 0xff0000) | ((fullsize+sizeof(bh) >> 8)&0xff00) | ((fullsize+sizeof(bh) << 24)&0xff000000)),   // File size
        0, 0,
        ((((sizeof(bh) + sizeof(struct dib_header)) >> 24) & 0xFF) | (((sizeof(bh) + sizeof(struct dib_header)) << 8) & 0xff0000) | (((sizeof(bh) + sizeof(struct dib_header)) >> 8)&0xff00) | (((sizeof(bh) + sizeof(struct dib_header)) << 24)&0xff000000))                                          // Reserved            // Pixel data offset
    };
    struct dib_header dh = {
        (((40 >> 24) & 0xFF) | ((40 << 8) & 0xff0000) | ((40 >> 8)&0xff00) | ((40 << 24)&0xff000000)),         // DIB header size
        (w << 24) | ((w << 8) & 0x00ff0000) | ((w >> 8) & 0x0000ff00) | ((w >> 24) & 0x000000ff), (h << 24) | ((h <<  8) & 0x00ff0000) | ((h >>  8) & 0x0000ff00) | ((h >> 24) & 0x000000ff),       // Dimensions
        (1 >> 8 & 0xff) | (1 << 8),          // Number of planes
        (24 >> 8 & 0xff) | (24 << 8),         // BPP
        0,          // Compression
        (((imgSize >> 24) & 0xFF) | ((imgSize << 8) & 0xff0000) | ((imgSize >> 8)&0xff00) | ((imgSize << 24)&0xff000000)),    // Pixel array size
        (1 << 24) | ((1 << 8) & 0x00ff0000) | ((1 >> 8) & 0x0000ff00) | ((1 >> 24) & 0x000000ff), (1 << 24) | ((1 << 8) & 0x00ff0000) | ((1 >> 8) & 0x0000ff00) | ((1 >> 24) & 0x000000ff),       // Pixels per meter, X/Y
        0, 0        // Color junk we don't care about
    };
    char * PATH;
    PATH = alloca(strlen("\\\\fls0\\")+strlen1(path)+strlen(".bmp")+1);
    strcpy(PATH, "\\\\fls0\\");
    strcat(PATH, path);
    strcat(PATH, ".bmp");
    int size = imgSize + sizeof(bh) + sizeof(struct dib_header);
    unsigned short * pFile = alloca(2*(strlen(PATH)+1));
    Bfile_StrToName_ncpy(pFile, (unsigned char*)PATH, strlen(PATH)+1);
    Bfile_CreateEntry_OS(pFile, CREATEMODE_FILE, &size);
    int hFile = Bfile_OpenFile_OS(pFile, 2);
    if (hFile < 0) {
        //printf("Failed to open file for writing: %s\n", strerror(errno));
        PrintXY(1, 0, "XXOpen Fail", TEXT_MODE_NORMAL, 0);
        return;
    }
    // Headers, sigh
    //fwrite(&bh, sizeof(bh), 1, fp);
    Bfile_WriteFile_OS(hFile, &bh, sizeof(bh));
    //fwrite(&dh, sizeof(dh), 1, fp);
    Bfile_WriteFile_OS(hFile, &dh, sizeof(dh));

    // Convert image data to 24bpp BGR and invert scan
    cdata = malloc(3 * w * h);
    for (y = 0; y < h; y++) {
        uint8_t *destRow = cdata + w * 3 * (h - 1 - y);
        for (x = 0; x < w; x++) {
            uint8_t r, g, b;
            uint16_t px = data[w * y + x];
            px = (px << 8 & 0xFF00) | px >> 8;
            r = convertChannelDepth((px >> 11) & 0x1F, 5, 8);
            g = convertChannelDepth((px >> 5) & 0x3F, 6, 8);
            b = convertChannelDepth(px & 0x1F, 5, 8);
            destRow[3 * x] = b;
            destRow[3 * x + 1] = g;
            destRow[3 * x + 2] = r;
        }
    }
    //fwrite(cdata, 3, w * h, fp);
    Bfile_WriteFile_OS(hFile, cdata, w * h * 3);
    free(cdata);
    //fclose(fp);
    Bfile_CloseFile_OS(hFile);
}




If any of you guys could help, it would be greatly appreciated. Very Happy
I'd guess it's an endianness issue, considering this:
Code:
    // TODO: don't write broken files on big-endian systems


The foolproof way is to create a copy of the image that is correct, then compare the image data between what you generate and what's correct.
After that, what do I do to make the BMP data correct?

Edit: It saves correctly! Huzzah!
Spenceboy98 wrote:
After that, what do I do to make the BMP data correct?

Edit: It saves correctly! Huzzah!
Congratulations! And hopefully you learned something along the way about data representations and endianness.
Thanks! I have!

Now I can't get the loading to work(also ported from mkg3a):

Code:
static void dibHeader_convert(struct dib_header *h) {
    h->header_size = (((h->header_size >> 24) & 0xFF) | ((h->header_size << 8) & 0xff0000) | ((h->header_size >> 8)&0xff00) | ((h->header_size << 24)&0xff000000));
    h->width = ((h->width << 24) | ((h->width << 8) & 0x00ff0000) | ((h->width >> 8) & 0x0000ff00) | ((h->width >> 24) & 0x000000ff));
    h->height = ((h->height << 24) | ((h->height << 8) & 0x00ff0000) | ((h->height >> 8) & 0x0000ff00) | ((h->height >> 24) & 0x000000ff));
    h->nplanes = ((h->nplanes >> 8 & 0xff) | (h->nplanes << 8));
    h->bpp = ((h->bpp >> 8 & 0xff) | (h->bpp << 8));
}

int readBMPHeader(struct bmp_header *bh, struct dib_header *h, int hFile) {
    size_t sz;

    // BMP header
    sz = Bfile_ReadFile_OS(hFile, bh, sizeof(*bh), 0);
    if (sz != sizeof(*bh)){
        PrintXY(1, 0, "XXStrange BMP Header", 0, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    if (bh->signature[0] != 0x42 || bh->signature[1] != 0x4D){    // "BM"
        PrintXY(1, 0, "XXNot a BMP", 0, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    // DIB header
    sz = Bfile_ReadFile_OS(hFile, h, sizeof(*h), sizeof(*bh));

    dibHeader_convert(h);

    if (sz < sizeof(*h)){
        PrintXY(1, 0, "XXStrange DIB Header", TEXT_MODE_NORMAL, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    if (h->nplanes != 1){
        PrintXY(1, 0, "XXnplanes not 1", TEXT_MODE_NORMAL, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    if (h->bpp != 24){
        PrintXY(1, 0, "XXNot 24 bpp", TEXT_MODE_NORMAL, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    if (h->compress_type != 0){
        PrintXY(1, 0, "XXCompression bad", TEXT_MODE_NORMAL, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    if (h->ncolors != 0){
        PrintXY(1, 0, "XXPalette Unsupported", TEXT_MODE_NORMAL, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }

    // Seek to image data
    if (Bfile_SeekFile_OS(hFile, bh->px_offset) != bh->px_offset){
        PrintXY(1, 0, "XXBad Seek", 0, 0);
        Bdisp_PutDisp_DD();
        return 1;
    }
    return 0;
}

int readBMPData(struct dib_header *dh, color_t *d, int hFile) {
    int row;
    size_t sz;

    // XXX hardcoded dimensions are begging to explode
    for (row = dh->height - 1; row >= 0; row--) {
        // Rows are 2208 bytes wide, so no alignment to worry about
        // (rows are supposed to be aligned to 4 bytes)
        sz = Bfile_ReadFile_OS(hFile, d, dh->width*3, Bfile_TellFile_OS(hFile));
        if (sz != (unsigned)dh->width*3){
            PrintXY(1, 0, "XXUnexpected EOF", 0, 0);
            Bdisp_PutDisp_DD();
            return 1;
        }
    }
    return 0;
}

color_t *convertBPP(int32_t w, int32_t h, color_t *d) {
    int pxi;
    uint16_t px;
    char r, g, b;

    for (pxi = 0; pxi < w * h; pxi++) {
        b = convertChannelDepth(d[pxi * 3], 8, 5);
        g = convertChannelDepth(d[pxi * 3 + 1], 8, 6);
        r = convertChannelDepth(d[pxi * 3 + 2], 8, 5);

        px = (r << 11) | (g << 5) | b;
        // Write as big-endian
        d[2 * pxi] = px >> 8;
        d[2 * pxi + 1] = px & 0xFF;
    }
    return realloc(d, 2 * w * h);
}

color_t *loadBMP(char *path, int *width, int *height) {
    unsigned short * buffer = alloca(2*(strlen(path)+1));
    Bfile_StrToName_ncpy(buffer, (unsigned char*)path, strlen(path)+1);
    int hFile = Bfile_OpenFile_OS(buffer, 0);
    struct bmp_header *bh;
    struct dib_header *dh;
    int32_t w, h;
    int err = 0;
    color_t *data;

    bh = malloc(sizeof(*bh));
    dh = malloc(sizeof(*dh));
    err = readBMPHeader(bh, dh, hFile);
    *width = w = dh->width;
    *height = h = dh->height;
    free(bh);
    free(dh);
    if (err) {
        Bfile_CloseFile_OS(hFile);
        PrintXY(1, 1, "XXHeader Error!", TEXT_MODE_NORMAL, TEXT_COLOR_RED);
        Bdisp_PutDisp_DD();
        return 0;
    }

    data = malloc(w * 3 * h);
    if(!data){
        Bfile_CloseFile_OS(hFile);
        return 0;
    }
    if (readBMPData(dh, data, hFile)) {
        free(data);
        Bfile_CloseFile_OS(hFile);
        PrintXY(1, 1, "XXData Error!", TEXT_MODE_NORMAL, TEXT_COLOR_RED);
        Bdisp_PutDisp_DD();
        return 0;
    }
    Bfile_CloseFile_OS(hFile);
    return convertBPP(w, h, data);
}


It keeps returning at the Bfile_SeekFile_OS. Could any of you help?
*BUMP*
Now saving and loading seem to work. It still has the "moving up and out of view" problem, and I have no idea how to fix it. Plus, when loading 24-bit pics it loads backwards. :/

What it does:


What it's supposed to look like(btw, I drew this on calc(but I converted to png to view here)):


Hopefully, I'll be able to get it to work and get a demo out soon. Very Happy

Edit: I've found the the "moving up and out of view" is caused by my scaling routine. Some images don't open because there isn't enough memory to allocate, so you won't be able to load or create large images. :/
  
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
Page 3 of 5
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement