Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load an image in Doom patch format
- static unsigned char convertedimage[1<<26];
- unsigned char* imageInDoomFormat(struct RGB_Sprite* image, size_t* size)
- {
- //@TODO handle squishing
- unsigned x, y;
- unsigned char* img;
- unsigned char* imgptr = convertedimage;
- unsigned char *colpointers, *startofspan;
- #define WRITE8(buf, a) ({*buf = (a); buf++;})
- #define WRITE16(buf, a) ({*buf = (a)&255; buf++; *buf = (a)>>8; buf++;})
- #define WRITE32(buf, a) ({WRITE16(buf, (a)&65535); WRITE16(buf, (a)>>16);})
- // Write image size and offset
- WRITE16(imgptr, image->width);
- WRITE16(imgptr, image->height/image->heightFactor);
- WRITE16(imgptr, image->xoffs);
- WRITE16(imgptr, image->yoffs);
- // Leave placeholder to column pointers
- colpointers = imgptr;
- imgptr += image->width*4;
- // Write columns
- for (x = 0; x < image->width; x++)
- {
- int lastStartY = 0;
- int spanSize = 0;
- startofspan = NULL;
- //printf("%d ", x);
- // Write column pointer (@TODO may be wrong)
- WRITE32(colpointers, imgptr - convertedimage);
- // Write pixels
- for (y = 0; y < image->height/image->heightFactor; y ++)
- {
- unsigned char paletteIndex = 0;
- unsigned char opaque = 0; // If 1, we have a pixel
- int layer;
- if (image->vibrating && (x+y) & 1)
- {
- for (layer = 0; layer < image->numLayers && !opaque; layer++)
- {
- READPIXEL(image->layers[layer].x + x, image->layers[layer].y + y*image->heightFactor + 1);
- rgbaToPalette(PIX_R, PIX_G, PIX_B, PIX_A, &paletteIndex, &opaque); // Get palette index and opacity from pixel values
- }
- }
- for (layer = 0; layer < image->numLayers && !opaque; layer++)
- {
- READPIXEL(image->layers[layer].x + x, image->layers[layer].y + y*image->heightFactor);
- rgbaToPalette(PIX_R, PIX_G, PIX_B, PIX_A, &paletteIndex, &opaque); // Get palette index and opacity from pixel values
- }
- // End span if we have a transparent pixel
- if (!opaque)
- {
- if (startofspan)
- {
- WRITE8(imgptr, 0);
- }
- startofspan = NULL;
- continue;
- }
- // Start new column if we need to
- if (!startofspan || spanSize == 255)
- {
- int writeY = y;
- // If we reached the span size limit, finish the previous span
- if (startofspan)
- {
- WRITE8(imgptr, 0);
- }
- if (y > 254)
- {
- // Make sure we're aligned to 254
- if (lastStartY < 254)
- {
- WRITE8(imgptr, 254);
- WRITE8(imgptr, 0);
- imgptr += 2;
- lastStartY = 254;
- }
- // Write stopgap empty spans if needed
- writeY = y - lastStartY;
- while (writeY > 254)
- {
- WRITE8(imgptr, 254);
- WRITE8(imgptr, 0);
- imgptr += 2;
- writeY -= 254;
- }
- }
- startofspan = imgptr;
- WRITE8(imgptr, writeY);///@TODO calculate starting y pos
- imgptr += 2;
- spanSize = 0;
- lastStartY = y;
- }
- // Write the pixel
- WRITE8(imgptr, paletteIndex);
- spanSize++;
- startofspan[1] = spanSize;
- }
- if (startofspan)
- WRITE8(imgptr, 0);
- WRITE8(imgptr, 0xFF);
- }
- *size = imgptr-convertedimage;
- img = malloc(*size);
- memcpy(img, convertedimage, *size);
- return img;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement