Skip to content

Commit

Permalink
makeTga.sms and makeTgaRGB.sms are B&W, RGB color gradient examples. …
Browse files Browse the repository at this point in the history
…Removed deprecated gradientTGA.sms
  • Loading branch information
reginaldford committed Sep 3, 2024
1 parent 24e00ed commit 2215eec
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 35 deletions.
19 changes: 0 additions & 19 deletions sms_src/examples/gradientTGA.sms

This file was deleted.

30 changes: 14 additions & 16 deletions sms_src/examples/makeTga.sms
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/usr/local/bin/sms -qm125
# Demonstrates generating a black and white gradient TGA image

{
# Image dimensions
let width = 256;
let width = 256;
let height = 256;

# TGA file header size is 18 bytes
let headerSize = 18;

# Create a blank buffer with header size + width * height bytes for the image
let buffer = ui8Repeat([ui8(0)], headerSize + width * height);

Expand Down Expand Up @@ -35,28 +38,23 @@
buffer[17] = ui8(0); # Image descriptor (1 byte)

# Fill the image buffer with a gradient
let i = 0;
while (i < height) {
let j = 0;
while (j < width) {
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
# Calculate the pixel index after the header
let pixelIndex = headerSize + i * width + j;

# Create a gradient from black to white
buffer[pixelIndex] = ui8(i * 255 / height); # Vertical gradient

j ++;
};
i ++;
};

# Write the buffer to a file
let success = fileWriteArr("gradient.tga", buffer);
if(success)
putLn("Gradient TGA file 'gradient.tga' has been created.")
else {
putLn("Something went wrong...");
putLn(str(success));
if (success) {
putLn("Gradient TGA file 'gradient.tga' has been created.");
} else {
putLn("Something went wrong...");
putLn(str(success));
};
};

70 changes: 70 additions & 0 deletions sms_src/examples/makeTgaRGB.sms
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/local/bin/sms -qm 10g
# Demonstrates generating a 3-byte color map TGA image (B, G, R gradient)

{
# Image dimensions
let width = 256;
let height = 256;

# TGA file header size is 18 bytes
let headerSize = 18;

# Each pixel in a 3-byte color map has 3 bytes (B, G, R)
let bytesPerPixel = 3;

# Create a blank buffer with header size + width * height * bytesPerPixel for the image
let buffer = ui8Repeat([ui8(0)], headerSize + width * height * bytesPerPixel);

# Write the TGA header to the buffer
# ID length (1 byte), Color Map Type (1 byte), Image Type (1 byte)
buffer[0] = ui8(0); # No image ID field
buffer[1] = ui8(0); # No color map
buffer[2] = ui8(2); # Uncompressed, true-color image

# Color Map Specification (5 bytes)
buffer[3] = ui8(0); # First entry index (2 bytes)
buffer[4] = ui8(0);
buffer[5] = ui8(0); # Color map length (2 bytes)
buffer[6] = ui8(0);
buffer[7] = ui8(0); # Color map entry size (1 byte)

# Image Specification (10 bytes)
buffer[8] = ui8(0); # X-origin (2 bytes)
buffer[9] = ui8(0);
buffer[10] = ui8(0); # Y-origin (2 bytes)
buffer[11] = ui8(0);
buffer[12] = ui8(mod(width, 256)); # Width (2 bytes)
buffer[13] = ui8(width / 256);
buffer[14] = ui8(mod(height, 256)); # Height (2 bytes)
buffer[15] = ui8(height / 256);
buffer[16] = ui8(24); # Pixel depth (1 byte, 24 bits for BGR color)
buffer[17] = ui8(0); # Image descriptor (1 byte)

# Fill the image buffer with a color gradient
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
# Calculate the pixel index after the header
let pixelIndex = headerSize + (i * width + j) * bytesPerPixel;

# Create a gradient (B, G, R)
let blue = ui8(i * 255 / height); # Blue gradient based on vertical position
let green = ui8(j * 255 / width); # Green gradient based on horizontal position
let red = ui8((i + j) * 255 / (width + height)); # Red gradient combining both positions

# Assign color values to the buffer
buffer[pixelIndex] = blue;
buffer[pixelIndex + 1] = green;
buffer[pixelIndex + 2] = red;
};
};

# Write the buffer to a file
let success = fileWriteArr("color_gradient.tga", buffer);
if (success)
putLn("Color gradient TGA file 'color_gradient.tga' has been created.")
else {
putLn("Something went wrong...");
putLn(str(success));
};
};

0 comments on commit 2215eec

Please sign in to comment.