hello guys!
I'm just programming a big sprited game, and thus I need to make paletised sprites.

I did it one for one pixel/char, and like I'm using 16-colored sprites, I wanted to make a compressor for two pixels/char.

With the help from Ashbad, We edited a program from PierrotLL.
But, the second pixel i totally ignored...

input: short datas
"65535,45468,45468,65535" wille be taken for 2 pixels
output:
"0,0x1,0x1,0" //sprite
"655535,45468" // palette.


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int color_swatches[1024], nb_color=0;

int swatches(int color1, int color2)
{
   int i, l = 0, j = 0;
   for(i=0 ; i<nb_color ; i++) {
      if(color1 == color_swatches[i]) {
         j = (i&0x0F) << 4;
         i = -1;
         break;
      }
   }
   if(i>=nb_color) {
      color_swatches[i] = color1;
      nb_color++;
      j = (nb_color&0x0F) << 4;
   }
   for(l = 0; l < nb_color; l++) {
      if(color2 == color_swatches[i])
         j |= i&0x0F;
      return j;
   }
   color_swatches[l] = color2;
   nb_color++;
   return j;
}

int main()
{
   int c, n1=0, n2 = 0;
    FILE *input, *output;
   unsigned char resultname[] = "out\\Rpk000.c";
   unsigned char inputname[] = "in\\Pk000.c";
   for(int i = 1; i < 152; i++)
    {

        resultname[7] = '0' + i /100;
        resultname[8] = '0' + (i/10)%10;
        resultname[9] = '0' + i%10;

        inputname[5] = resultname[7];
        inputname[6] = resultname[8];
        inputname[7] = resultname[9];

        printf("N %i : %s ; %s ; ", i, inputname, resultname);

        input = fopen(inputname, "r");
        if(input != NULL)
            printf("Ouverture bien faite; ");
        else
        {
            printf("Ouverture failed; \n");
            continue;
        }

        output = fopen(resultname, "w+");
        if(output != NULL)
            printf("Ouverture bien faite; ");
        else
        {
            printf("Ouverture failed;\n");
            continue;
        }



        printf("\n");
        fprintf(output, "unsigned char m[] = {\n");
        nb_color=-1;
        for(int i = 0;i< 1024; i++)
            color_swatches[i] = 65535;
      int alignment_check = 0;
        while((c = fgetc(input)) != EOF) {
            switch(c) {
                case '\t': fputc(c, output); break;
                case '\n': fputc(c, output); break;
                case EOF:  break;
                case ',':
               if(!alignment_check) {
                  alignment_check = 1;
               } else {
                  fprintf(output, "%p,", swatches(n1, n2));
                  n1=0; n2=0;
                  alignment_check = 0;
               }
               break;

                default:
                    if(c>='0' && c<='9') {
                  if(!alignment_check) {
                     n1 = n1*10 + c-'0';
                  } else {
                     n2 = n2*10 + c-'0';
                  }
               }
               break;
            }
        }
        fseek ( output , -3 , SEEK_CUR );
        fprintf(output, " \n};");

        fprintf(output, "\n\nunsigned short p[] = {", i);
        for(int n=0 ; n<nb_color ; n++)
            fprintf(output, "%p,", color_swatches[n]);
        fseek ( output , -1 , SEEK_CUR );
        fprintf(output, "};\n\n");

        fclose(input);
        fclose(output);

    }
   }


I'll thanks any help from your initative. Thanks!
I just lose all datas because corruption, and I didn't got enough time to make bavk-ups.

Ashbad please could you reupload the edited source of the compressor, please?
Eiyeron wrote:
I just lose all datas because corruption, and I didn't got enough time to make bavk-ups.

Ashbad please could you reupload the edited source of the compressor, please?


The only backup I have is unfortunately the program you yourself posted above in the original post Sad
What's the problem ?
When reading bitmap data, you have just the 4 bits you want.

By example you can easily make a macro :
Code:
#define GET_PIXEL(bitmap, n) ((n)&1 ? (bitmap)[(n)>>1]&0xF : (bitmap)[(n)>>1]>>4)
Wouldn't it be easier to palettise your sprites in an image editor, save them as indexed BMP* images, and then extract the palette and (indexed) pixel data from the BMP files? In GIMP, you can easily create a custom palette and convert the images to indexed using that palette, and I'm sure you can do the same in Photoshop too if you have and prefer to use Photoshop instead.


*It doesn't have to be BMP. Other image formats exist that might be easier to parse than BMP.
I, with it already made a porgram that palettise them, BUT, the program that I remade from backups found from hidtory are a bit buggy.
(@PierrotLL: Mon abscence est normale, je n'ai plus de temps à passer sur le Net :S)

Here are the code

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int color_swatches[1024], nb_color=0;

int swatches(int color1, int color2)
{
   int i, l = 0, j = 0;
   for(i=0 ; i<nb_color ; i++) {
      if(color1 == color_swatches[i]) {
         j = (i&0x0F) << 4;
         i = -1;
         break;
      }
   }
   if(i>=nb_color) {
      color_swatches[i] = color1;
      nb_color++;
      j = (nb_color&0x0F) << 4;
   }


   for(l = 0; l < nb_color; l++) {
      if(color2 == color_swatches[l])
      {
         j |= l&0x0F;
         return j;
      }
   }
   color_swatches[l] = color2;
   nb_color++;
   j |= l&0x0F;
   return j;
}

int main()
{
   int c, n1=0, n2 = 0, n=0;
    FILE *input, *output;
   unsigned char resultname[] = "out\\Maj\\0.c";
   unsigned char inputname[] = "in\\Maj\\0.c";
   for(int i = 0; i < 26; i++)
    {

        resultname[8] = 'A' + i;
        // resultname[7] = '0' + i /100;
        // resultname[8] = '0' + (i/10)%10;
        // resultname[9] = '0' + i%10;

        inputname[7] = resultname[8];
        // inputname[5] = resultname[7];
        // inputname[6] = resultname[8];
        // inputname[7] = resultname[9];

        printf("N %i : %s ; %s ; ", i, inputname, resultname);

        input = fopen(inputname, "r");
        if(input != NULL)
            printf("Ouverture bien faite; ");
        else
        {
            printf("Ouverture failed; \n");
            continue;
        }

        output = fopen(resultname, "w+");
        if(output != NULL)
            printf("Ouverture bien faite; ");
        else
        {
            printf("Ouverture failed;\n");
            continue;
        }


        printf("\n");
        fprintf(output, "const unsigned char m[] = {\n");
        nb_color=-1;
        for(int i = 0;i< 1024; i++)
            color_swatches[i] = 65535;
      int alignment_check = 0;
        while((c = fgetc(input)) != EOF) {
            switch(c) {
                case '\t': fputc(c, output); break;
                case '\n': fputc(c, output); break;
                case EOF:  break;
                case ',':
               if(!alignment_check) {
                  alignment_check = 1;
               } else {
                  fprintf(output, "%p,", swatches(n1, n2));
                  n1=0; n2=0;
                  alignment_check = 0;
               }
               break;

                default:
                    if(c>='0' && c<='9') {
                  if(!alignment_check) {
                     n1 = n1*10 + c-'0';
                  } else {
                     n2 = n2*10 + c-'0';
                  }
               }
               break;
            }
        }
        fseek ( output , -3 , SEEK_CUR );
        fprintf(output, " \n};");

        fprintf(output, "\n\nconst unsigned short p[] = {");
        for(n=0 ; n<nb_color ; n++)
            fprintf(output, "%p,", color_swatches[n]);
        fseek ( output , -1 , SEEK_CUR );
        fprintf(output, "};\n\n");

        fclose(input);
        fclose(output);

    }
}


Takes at input Color values like this:

Code:
65535,16901,16901,52825,65535,65535,
16901,52825,52825,16901,52825,65535,
16901,52825,65535,16901,52825,65535,
16901,16901,16901,16901,52825,65535,
16901,52825,52825,16901,52825,65535,
16901,52825,65535,16901,52825,65535,
16901,52825,65535,16901,52825,65535,
52825,52825,65535,52825,52825,65535,
65535,65535,65535,65535,65535,65535,
  
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 1 of 1
» 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