Advertisement
Guest User

Untitled

a guest
May 24th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.18 KB | None | 0 0
  1. // Load an image in Doom patch format
  2. static unsigned char convertedimage[1<<26];
  3. unsigned char* imageInDoomFormat(struct RGB_Sprite* image, size_t* size)
  4. {
  5.     //@TODO handle squishing
  6.     unsigned x, y;
  7.    
  8.     unsigned char* img;
  9.     unsigned char* imgptr = convertedimage;
  10.     unsigned char *colpointers, *startofspan;
  11.  
  12. #define WRITE8(buf, a) ({*buf = (a); buf++;})
  13. #define WRITE16(buf, a) ({*buf = (a)&255; buf++; *buf = (a)>>8; buf++;})
  14. #define WRITE32(buf, a) ({WRITE16(buf, (a)&65535); WRITE16(buf, (a)>>16);})
  15.     // Write image size and offset
  16.     WRITE16(imgptr, image->width);
  17.     WRITE16(imgptr, image->height/image->heightFactor);
  18.     WRITE16(imgptr, image->xoffs);
  19.     WRITE16(imgptr, image->yoffs);
  20.    
  21.     // Leave placeholder to column pointers
  22.     colpointers = imgptr;
  23.     imgptr += image->width*4;
  24.    
  25.     // Write columns
  26.     for (x = 0; x < image->width; x++)
  27.     {
  28.         int lastStartY = 0;
  29.         int spanSize = 0;
  30.         startofspan = NULL;
  31.        
  32.         //printf("%d ", x);
  33.         // Write column pointer (@TODO may be wrong)
  34.         WRITE32(colpointers, imgptr - convertedimage);
  35.        
  36.         // Write pixels
  37.         for (y = 0; y < image->height/image->heightFactor; y ++)
  38.         {
  39.             unsigned char paletteIndex = 0;
  40.             unsigned char opaque = 0; // If 1, we have a pixel
  41.             int layer;
  42.            
  43.             if (image->vibrating && (x+y) & 1)
  44.             {
  45.                 for (layer = 0; layer < image->numLayers && !opaque; layer++)
  46.                 {
  47.                     READPIXEL(image->layers[layer].x + x, image->layers[layer].y + y*image->heightFactor + 1);
  48.                     rgbaToPalette(PIX_R, PIX_G, PIX_B, PIX_A, &paletteIndex, &opaque); // Get palette index and opacity from pixel values
  49.                 }
  50.             }
  51.            
  52.             for (layer = 0; layer < image->numLayers && !opaque; layer++)
  53.             {
  54.                 READPIXEL(image->layers[layer].x + x, image->layers[layer].y + y*image->heightFactor);
  55.                 rgbaToPalette(PIX_R, PIX_G, PIX_B, PIX_A, &paletteIndex, &opaque); // Get palette index and opacity from pixel values
  56.             }
  57.            
  58.             // End span if we have a transparent pixel
  59.             if (!opaque)
  60.             {
  61.                 if (startofspan)
  62.                 {
  63.                     WRITE8(imgptr, 0);
  64.                 }
  65.                 startofspan = NULL;
  66.                 continue;
  67.             }
  68.            
  69.             // Start new column if we need to
  70.             if (!startofspan || spanSize == 255)
  71.             {
  72.                 int writeY = y;
  73.                
  74.                 // If we reached the span size limit, finish the previous span
  75.                 if (startofspan)
  76.                 {
  77.                     WRITE8(imgptr, 0);
  78.                 }
  79.                
  80.                 if (y > 254)
  81.                 {
  82.                     // Make sure we're aligned to 254
  83.                     if (lastStartY < 254)
  84.                     {
  85.                         WRITE8(imgptr, 254);
  86.                         WRITE8(imgptr, 0);
  87.                         imgptr += 2;
  88.                         lastStartY = 254;
  89.                     }
  90.                    
  91.                     // Write stopgap empty spans if needed
  92.                     writeY = y - lastStartY;
  93.                    
  94.                     while (writeY > 254)
  95.                     {
  96.                         WRITE8(imgptr, 254);
  97.                         WRITE8(imgptr, 0);
  98.                         imgptr += 2;
  99.                         writeY -= 254;
  100.                     }
  101.                 }
  102.                
  103.                 startofspan = imgptr;
  104.                 WRITE8(imgptr, writeY);///@TODO calculate starting y pos
  105.                 imgptr += 2;
  106.                 spanSize = 0;
  107.                
  108.                 lastStartY = y;
  109.             }
  110.            
  111.             // Write the pixel
  112.             WRITE8(imgptr, paletteIndex);
  113.             spanSize++;
  114.             startofspan[1] = spanSize;
  115.         }
  116.        
  117.         if (startofspan)
  118.             WRITE8(imgptr, 0);
  119.        
  120.         WRITE8(imgptr, 0xFF);
  121.     }
  122.  
  123.     *size = imgptr-convertedimage;
  124.     img = malloc(*size);
  125.     memcpy(img, convertedimage, *size);
  126.     return img;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement