Skip to content

Commit

Permalink
updated and moved new image file generation examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
reginaldford committed Sep 4, 2024
1 parent 2215eec commit eda40d9
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 226 deletions.
48 changes: 48 additions & 0 deletions sms_src/examples/generateBWImg.sms
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/local/bin/sms -qm225
# Demonstrates generating a black and white striped TGA image with 3 shades

{
# Include the bwImg library
eval(fileParse("bwImg.sms"));

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

# Create an array for the striped pattern
let grayscaleArray = ui8Repeat(ui8[0], width * height);

# Define 3 shades of gray
let shade1 = ui8(85); # Light gray
let shade2 = ui8(170); # Medium gray
let shade3 = ui8(255); # White

# Size of each stripe
let stripeSize = 32;

# Fill the array with a striped pattern
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
# Determine the column position, rounding to the nearest integer
let blockCol = round(j / stripeSize);

# Alternate between shades based on column position
if (mod(blockCol, 3) == 0) {
grayscaleArray[i * width + j] = shade1;
} else if (mod(blockCol, 3) == 1) {
grayscaleArray[i * width + j] = shade2;
} else {
grayscaleArray[i * width + j] = shade3;
};
};
};

# Use the bwImg.write function to create the TGA file
let result = bwImg.write("striped_pattern.tga", width, height, grayscaleArray);
if (result == true) {
putLn("Striped pattern TGA file 'striped_pattern.tga' has been created.");
} else {
putLn("Error: " str+ result);
};
};

42 changes: 42 additions & 0 deletions sms_src/examples/generateRGBImg.sms
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/local/bin/sms -m196
# Usage: This script demonstrates creating a rainbow gradient using colorImg.write

{
# Evaluate and include the colorImg library
eval(fileParse("colorImg.sms"));

# Image dimensions
let width = 256;
let height = 256;
let bytesPerPixel = 3;

# Create an RGB array for the rainbow gradient
let rgbArray = ui8Repeat([ui8(0)], width * height * bytesPerPixel);

# Fill the RGB array with a rainbow gradient
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
# Calculate the pixel index in the RGB array
let rgbIndex = (i * width + j) * bytesPerPixel;

# Create a gradient with varying colors (RGB order)
let r = ui8((i * 255) / height); # Red varies by height
let g = ui8((j * 255) / width); # Green varies by width
let b = ui8(((i + j) * 255) / (width + height)); # Blue varies by combined position

# Assign the RGB values to the array
rgbArray[rgbIndex] = r;
rgbArray[rgbIndex + 1] = g;
rgbArray[rgbIndex + 2] = b;
};
};

# Call the colorImg.write function to create the TGA file
let result = colorImg.write("rainbow.tga", width, height, rgbArray);
if (result == true) {
putLn("Rainbow gradient TGA file 'rainbow_gradient.tga' has been created.");
} else {
putLn("Error: " str+ result);
};
};

60 changes: 0 additions & 60 deletions sms_src/examples/makeTga.sms

This file was deleted.

70 changes: 0 additions & 70 deletions sms_src/examples/makeTgaRGB.sms

This file was deleted.

83 changes: 0 additions & 83 deletions src/main/engine/sm_ast_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -1247,89 +1247,6 @@ inline sm_object *sm_engine_eval(sm_object *input, sm_cx *current_cx, sm_expr *s
fclose(fptr);
return (sm_object *)sms_true;
}
case SM_FILE_WRITETGA_EXPR: {
// Check and retrieve the filename
sm_string *filename = (sm_string *)eager_type_check(sme, 0, SM_STRING_TYPE, current_cx, sf);
if (filename->my_type != SM_STRING_TYPE) {
return (sm_object *)filename;
}

// Check and retrieve the width
sm_f64 *width_obj = (sm_f64 *)eager_type_check(sme, 1, SM_F64_TYPE, current_cx, sf);
if (width_obj->my_type != SM_F64_TYPE) {
return (sm_object *)width_obj;
}
uint16_t width = (uint16_t)(width_obj->value);

// Check and retrieve the height
sm_f64 *height_obj = (sm_f64 *)eager_type_check(sme, 2, SM_F64_TYPE, current_cx, sf);
if (height_obj->my_type != SM_F64_TYPE) {
return (sm_object *)height_obj;
}
uint16_t height = (uint16_t)(height_obj->value);

// Check and retrieve the pixel expression
sm_expr *pixel_expr = (sm_expr *)eager_type_check(sme, 3, SM_EXPR_TYPE, current_cx, sf);
if (pixel_expr->my_type != SM_EXPR_TYPE || pixel_expr->size != 3 * width * height) {
sm_symbol *title = sm_new_symbol("BadTgaArraySize", 12);
sm_string *message =
sm_new_string(49, "Expected pixel array to have (3 * width * height) elements.");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}

// Extract the file name
const char *file_name = &filename->content;

// Allocate memory for pixel data
uint8_t *pixels = (uint8_t *)malloc(3 * width * height * sizeof(uint8_t));
if (!pixels) {
sm_symbol *title = sm_new_symbol("MemoryError", 11);
sm_string *message =
sm_new_string(24, "Failed to allocate memory for fileWriteTga function");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}

// Fill the pixel data by extracting each value from the pixel_expr
for (uint16_t y = 0; y < height; y++) {
for (uint16_t x = 0; x < width; x++) {
for (uint16_t c = 0; c < 3; c++) {
// Flip bgr to rgb
uint32_t index = 3 * (y * width + x + 1) - c - 1;
sm_f64 *pixel_value_obj = (sm_f64 *)sm_expr_get_arg(pixel_expr, index);
if (pixel_value_obj->my_type != SM_F64_TYPE) {
free(pixels);
return (sm_object *)pixel_value_obj;
}
f64 pixel_value = pixel_value_obj->value;
pixels[index] =
(uint8_t)(pixel_value < 0 ? 0 : (pixel_value > 255 ? 255 : (uint8_t)(pixel_value)));
}
}
}

// Open the file for writing
FILE *output = fopen(file_name, "wb");
if (!output) {
free(pixels);
sm_symbol *title = sm_new_symbol("FileError", 9);
sm_string *message = sm_new_string(29, "Failed to open file for writing");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}

// Write the TGA file
bool success = sm_fileWriteTga(output, pixels, width, height);
fclose(output);
free(pixels);

if (!success) {
sm_symbol *title = sm_new_symbol("FileError", 9);
sm_string *message = sm_new_string(22, "Failed to write TGA file");
return (sm_object *)sm_new_error_from_expr(title, message, sme, NULL);
}

return (sm_object *)sms_true;
break;
}
case SM_FILE_APPEND_EXPR: {
// obtain the file name
sm_string *fname_str;
Expand Down
1 change: 0 additions & 1 deletion src/main/parser/sms.l
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ fileRm {return FILE_RM;}
fileSize {return FILE_SIZE;}
fileWriteStr {return FILE_WRITESTR;}
fileWriteArr {return FILE_WRITEARR;}
fileWriteTga {return FILE_WRITETGA;}
fileAppend {return FILE_APPEND;}

zeros {return ZEROS;}
Expand Down
2 changes: 0 additions & 2 deletions src/main/parser/sms.y
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ int parsing_fpath_len;
%token <expr> FILE_PART
%token <expr> FILE_WRITESTR
%token <expr> FILE_WRITEARR
%token <expr> FILE_WRITETGA
%token <expr> FILE_EXISTS
%token <expr> FILE_STAT
%token <expr> FILE_APPEND
Expand Down Expand Up @@ -485,7 +484,6 @@ EXPR : SELF { $$ = (sm_expr*)sm_new_self(); }
| FILE_PART '(' EXPR ',' EXPR ',' EXPR ')' {$$ = sm_new_expr_3(SM_FILE_PART_EXPR,(sm_object*)$3,(sm_object*)$5,(sm_object*)$7, _note());}
| FILE_WRITESTR '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_FILE_WRITESTR_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| FILE_WRITEARR '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_FILE_WRITEARR_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| FILE_WRITETGA '(' EXPR ',' EXPR ',' EXPR ',' EXPR ')' {$$ = sm_new_expr_4(SM_FILE_WRITETGA_EXPR,(sm_object*)$3,(sm_object*)$5,(sm_object*)$7,(sm_object*)$9, _note());}
| FILE_APPEND '(' EXPR ',' EXPR ')' {$$ = sm_new_expr_2(SM_FILE_APPEND_EXPR,(sm_object*)$3,(sm_object*)$5, _note());}
| FILE_EXISTS '(' EXPR ')' {$$ = sm_new_expr(SM_FILE_EXISTS_EXPR,(sm_object*)$3, _note());}
| FILE_RM '(' EXPR ')' {$$ = sm_new_expr(SM_FILE_RM_EXPR,(sm_object*)$3, _note());}
Expand Down
16 changes: 7 additions & 9 deletions src/main/sm_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ char *sm_global_fn_name(uint32_t which) {
"fileRm", // SM_FILE_RM_EXPR
"fileWriteStr", // SM_FILE_WRITESTR_EXPR
"fileWriteArr", // SM_FILE_WRITEARR_EXPR
"fileWriteTga", // SM_FILE_WRITETGA_EXPR
"fileAppend", // SM_FILE_APPEND_EXPR
"zeros", // SM_ZEROS_EXPR
"part", // SM_PART_EXPR
Expand Down Expand Up @@ -271,14 +270,13 @@ char *sm_global_fn_name(uint32_t which) {
// Corresponding string length of the string that would come from the sm_global_fn_name(which)
uint32_t sm_global_fn_name_len(uint32_t which) {
static uint64_t response_len[] = {
8, 4, 4, 5, 4, 2, 2, 3, 3, 3, 9, 3, 9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5,
3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 2, 3, 3, 4, 3, 3, 3, 4, 4, 3, 2, 2, 3,
6, 6, 5, 3, 7, 6, 4, 6, 8, 12, 5, 5, 4, 2, 2, 2, 1, 1, 2, 2, 5, 5, 5, 5, 5,
3, 5, 4, 5, 2, 11, 5, 5, 5, 8, 8, 12, 5, 4, 7, 6, 8, 6, 8, 5, 9, 11, 7, 8, 10,
8, 6, 6, 6, 12, 12, 12, 10, 5, 4, 4, 3, 6, 6, 4, 5, 5, 4, 3, 3, 2, 3, 3, 4, 7,
8, 11, 8, 11, 4, 7, 7, 7, 6, 6, 6, 6, 7, 8, 4, 8, 7, 9, 11, 8, 6, 9, 3, 6, 12,
13, 8, 9, 7, 4, 4, 5, 6, 6, 6, 11, 8, 8, 3, 5, 8, 10, 9, 7, 8, 1};
8, 4, 4, 5, 4, 2, 2, 3, 3, 3, 9, 3, 9, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 4, 4, 3, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 3, 3, 3, 4, 4, 4,
4, 4, 4, 5, 5, 5, 2, 3, 3, 4, 3, 3, 3, 4, 4, 3, 2, 2, 3, 6, 6, 5, 3, 7, 6, 4, 6, 8,
12, 5, 5, 4, 2, 2, 2, 1, 1, 2, 2, 5, 5, 5, 5, 5, 3, 5, 4, 5, 2, 11, 5, 5, 5, 8, 8, 12,
5, 4, 7, 6, 8, 6, 8, 5, 9, 11, 7, 8, 10, 8, 6, 6, 6, 12, 12, 10, 5, 4, 4, 3, 6, 6, 4, 5,
5, 4, 3, 3, 2, 3, 3, 4, 7, 8, 11, 8, 11, 4, 7, 7, 7, 6, 6, 6, 6, 7, 8, 4, 8, 7, 9, 11,
8, 6, 9, 3, 6, 12, 13, 8, 9, 7, 4, 4, 5, 6, 6, 6, 11, 8, 8, 3, 5, 8, 10, 9, 7, 8, 1};


if (which >= sm_global_num_fns())
Expand Down
2 changes: 1 addition & 1 deletion src/main/sm_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern uint32_t sms_num_symbols;

void sm_init(sm_env *env, int num_args, char **argv) {
// Set version number. Major.Minor.Patch
char *sms_version = "0.21.35";
char *sms_version = "0.21.36";
int sms_version_len = 7;
sm_strncpy(env->version, sms_version, sms_version_len);
env->version_len = sms_version_len;
Expand Down

0 comments on commit eda40d9

Please sign in to comment.