Skip to content

Commit

Permalink
Merge pull request #26 from SzBenedek2006/dev
Browse files Browse the repository at this point in the history
2.4.0
  • Loading branch information
SzBenedek2006 authored Nov 8, 2024
2 parents 73bf311 + bf3df9a commit a0e428d
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 86 deletions.
28 changes: 20 additions & 8 deletions JPEG_generator.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/types.h>
#include <time.h>
#include <stdint.h>
#include "jpeglib.h"
#include "JPEG_generator.h"
#include "my_utils.h"


/* Function to write a JPEG file */
void generateJPEG(char *filename, long width, long height, int quality) {
void generateJPEG(char *filename, long width, long height, int quality, uint8_t r, uint8_t g, uint8_t b, bool random_multiplier) {
float multiplier = 0.0f;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *outfile;
Expand All @@ -20,13 +25,20 @@ void generateJPEG(char *filename, long width, long height, int quality) {
exit(1);
}

int i, j;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
int index = (i * width + j) * 3; // Calculate index for RGB values
image_buffer[index] = rand() % (255 + 1); // Red
image_buffer[index + 1] = rand() % (255 + 1); // Green
image_buffer[index + 2] = rand() % (255 + 1); // Blue



for (int y = 0; y < height; y++) {
if (random_multiplier == true) {
multiplier = (rand() % 11)/10.0f;
} else {
multiplier = 1.0f;
}
for (int x = 0; x < width; x++) {
int index = (y * width + x) * 3;
image_buffer[index] = random_pixel(r, multiplier); // R
image_buffer[index + 1] = random_pixel(g, multiplier); // G
image_buffer[index + 2] = random_pixel(b, multiplier); // B
}
}

Expand Down
6 changes: 3 additions & 3 deletions JPEG_generator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef JPEG_GENERATOR
#define JPEG_GENERATOR
#include <stdint.h>

void generateJPEG(char *filename, long width, long height, int quality);

#endif // JPEG_GENERATOR
void generateJPEG(char *filename, long width, long height, int quality, uint8_t r, uint8_t g, uint8_t b, bool random_multiplier);
#endif
58 changes: 33 additions & 25 deletions PNG_generator.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Include necessary header files
#include <png.h>
#include <stdbool.h>
#include <stdio.h>
Expand All @@ -7,8 +6,11 @@
#include "PNG_generator.h"


int generatePNG(const char *filename, unsigned int width, unsigned int height, bool alpha, bool allowDebugInfo) {
// Initialize PNG structures and open a file for writing
int generatePNG(const char *filename, unsigned int width, unsigned int height, bool alpha, bool allowDebugInfo, uint8_t r, uint8_t g, uint8_t b, bool random_multiplier) {

float multiplier = 0.0f;

// Initialize PNG structures and open a file for writing
FILE *fp = fopen(filename, "wb");

// Handle file opening error
Expand All @@ -32,7 +34,7 @@ int generatePNG(const char *filename, unsigned int width, unsigned int height, b
if (!info_ptr) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
printDebug("Error creating tunsigned inthe image file. png error 3");
printDebug("Error creating unsigned int image file. png error 3");
return 3;
}

Expand All @@ -50,17 +52,19 @@ int generatePNG(const char *filename, unsigned int width, unsigned int height, b
if (alpha == true) { // If alpha true run
png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height);
for (int y = 0; y < height; y++) {
row_pointers[y] =
(png_byte *)malloc(4 * width); // 4 bytes per pixel (RGBA)

// Fill row_pointers[y] with your image data for this row.

for (int x = 0; x < width; x++) {
row_pointers[y][4 * x] = rand() % (255 + 1); // Red channel
row_pointers[y][4 * x + 1] = rand() % (255 + 1); // Green channel
row_pointers[y][4 * x + 2] = rand() % (255 + 1); // Blue channel
row_pointers[y][4 * x + 3] = rand() % (255 + 1); // Alpha channel
}
if (random_multiplier == true) {
multiplier = (rand() % 11)/10.0f;
} else {
multiplier = 1.0f;
}

row_pointers[y] = (png_byte *)malloc(4 * width); // 4 bytes per pixel (RGBA)
for (int x = 0; x < width; x++) {
row_pointers[y][4 * x] = random_pixel(r, multiplier); // Red channel
row_pointers[y][4 * x + 1] = random_pixel(g, multiplier); // Green channel
row_pointers[y][4 * x + 2] = random_pixel(b, multiplier); // Blue channel
row_pointers[y][4 * x + 3] = random_pixel(255, multiplier); // Alpha channel
}
}

int color_type = PNG_COLOR_TYPE_RGBA;
Expand All @@ -83,17 +87,21 @@ int generatePNG(const char *filename, unsigned int width, unsigned int height, b
} else { // If alpha false, run
// Write the image data
png_bytep *row_pointers = (png_bytep *)malloc(sizeof(png_bytep) * height);
for (int y = 0; y < height; y++) {
row_pointers[y] =
(png_byte *)malloc(3 * width); // 4 bytes per pixel (RGBA)

// Fill row_pointers[y] with your image data for this row.

for (int x = 0; x < width; x++) {
row_pointers[y][3 * x] = rand() % (255 + 1); // Red channel
row_pointers[y][3 * x + 1] = rand() % (255 + 1); // Green channel
row_pointers[y][3 * x + 2] = rand() % (255 + 1); // Blue channel
}
for (int y = 0; y < height; y++) {
if (random_multiplier == true) {
multiplier = (rand() % 11)/10.0f;
} else {
multiplier = 1.0f;
}

//printf("multiplier: %f\n", multiplier);
row_pointers[y] = (png_byte *)malloc(3 * width); // 3 bytes per pixel (RGB)
for (int x = 0; x < width; x++) {
row_pointers[y][3 * x] = random_pixel(r, multiplier); printDebugPlusInt("r:", r); // Red channel
row_pointers[y][3 * x + 1] = random_pixel(g, multiplier); printDebugPlusInt("g:", g); // Green channel
row_pointers[y][3 * x + 2] = random_pixel(b, multiplier); printDebugPlusInt("g:", b); // Blue channel
}
}

int color_type = PNG_COLOR_TYPE_RGB;
Expand Down
3 changes: 2 additions & 1 deletion PNG_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#define PNG_GENERATOR

#include <stdbool.h>
#include <stdint.h>

int generatePNG(const char *filename, unsigned int width, unsigned int height, bool alpha, bool allowDebugInfo);
int generatePNG(const char *filename, unsigned int width, unsigned int height, bool alpha, bool allowDebugInfo, uint8_t r, uint8_t g, uint8_t b, bool random_multiplier);

#endif
31 changes: 14 additions & 17 deletions dir_creator.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bool check_access_termux () {
}


int dirCreatorLinux(char dirName[], bool isTermux) { // Starting of the function
int dirCreatorLinux(char dirName[], bool isTermux) {

mkdir(dirName, 0700);

Expand All @@ -33,14 +33,14 @@ int dirCreatorLinux(char dirName[], bool isTermux) { // Starting of the function

if (dir != NULL) {
printDebug("Output directory created");
closedir(dir); // Close dir!
return 0; // Return directory exists.
} else if (ENOENT == errno) { // If dir not exists
closedir(dir);
return 0;
} else if (ENOENT == errno) {
printDebug("Output directory doesn't exist");
return 1; // Return directory does not exist.
} else { // If other fail
printDebug("Output directory may or may not exist");
return 2; // Return opendir() failed for some other reason.
return 1;
} else {
printDebug("Output directory is in a superposition");
return 2;
}
} else {
printDebugPlusInt("check_access_termux():", check_access_termux());
Expand Down Expand Up @@ -69,18 +69,15 @@ int dirCreatorLinux(char dirName[], bool isTermux) { // Starting of the function
}
DIR *dir = opendir(androidInternalPath); // androidInternalPath should now be the dir where pics will be placed.

if (dir) { // If dir exists
if (dir) {

closedir(dir);
// Close dir!
return 0; // Return directory exists.
} else if (ENOENT == errno) { // If dir not exists
return 1; // Return directory does not exist.
return 0;
} else if (ENOENT == errno) {
return 1;

} else { // If other fail
return 2; // Return opendir() failed for some other reason.
} else {
return 2;
}
}
}

// Megcsinálni a termux-setup-storage-t
91 changes: 67 additions & 24 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "PNG_generator.h"
#include "dir_creator.h"
#include "JPEG_generator.h"
#include "my_utils.h" // my_utils.c is replaced
#include "my_utils.h"
#include "progressbar.h"
#include "version.h"

Expand All @@ -13,6 +13,7 @@
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#include <stdint.h>


const int MS = 1000;
Expand Down Expand Up @@ -42,34 +43,35 @@ int main(int argc, char* argv[])
int n;
bool help = false;
bool termuxExternal = false;
bool random_multiplier = false;
char outDir[] = "out";
char androidInternalPath[] = "/storage/emulated/0/";
int quality = 100;
uint8_t quality = 100;
uint8_t r = 255;
uint8_t g = 255;
uint8_t b = 255;
int temp = 0;


// Terminal sizes:

getTerminalSize(&terminalHeight, &terminalWidth);

/*for (int i = 0; i < terminalHeight - 3; i++) {
* printf("\n");
}*/

// Number of the same arguments
short unsigned int sCount = 0; // -s, --size
short unsigned int cCount = 0; // -c --count
short unsigned int aCount = 0; // -a --alpha
short unsigned int hCount = 0; // -h --help
short unsigned int tCount = 0; // --termux_external
short unsigned int dCount = 0; // -d --debug
short unsigned int fCount = 0; // -f --format
short unsigned int qCount = 0; // -f --format
short unsigned int sCount = 0; // -s, --size
short unsigned int cCount = 0; // -c --count
short unsigned int aCount = 0; // -a --alpha
short unsigned int hCount = 0; // -h --help
short unsigned int tCount = 0; // --termux_external
short unsigned int dCount = 0; // -d --debug
short unsigned int fCount = 0; // -f --format
short unsigned int qCount = 0; // -q --quality
short unsigned int rgbCount = 0; // --rgb --RGB
short unsigned int snCount = 0; // --sensor-noise


// Print the arguments
// for (int i = 0; i <= argc+1; i++) {
// printf("Argv%d = %s\n", i, argv[i]);
//}

// Handle the arguments.
for (n = 1; n < argc; n++) {
Expand Down Expand Up @@ -117,7 +119,6 @@ int main(int argc, char* argv[])
printf("RIG currently only supports png (default) and jpeg as a format option!");
return 3;
}

} else {
printf("--format/-f is not set correctly or left empty, write image format after -f or --format!\n");
return 3;
Expand All @@ -132,12 +133,51 @@ int main(int argc, char* argv[])
printf("Quality isn't set correctly!\n");
return 3;
}

} else {
printf("Missing quality value after -q or --quality.\n");
return 3;
}

} else if (strcmp(argv[n], "--rgb") == 0 || strcmp(argv[n], "--RGB") == 0) {
if ((argv[n + 1]) != NULL) {
temp = atoi(argv[n + 1]);
if (temp <= 255 && r >= 0) {
r = temp;
} else {
printf("Red value is outside of the range (0-255)!\n");
return 3;
}
} else {
printf("Red value missing!\n");
return 3;
}
if ((argv[n + 2]) != NULL) {
temp = atoi(argv[n + 2]);
if (temp <= 255 && g >= 0) {
g = temp;
} else {
printf("Green value is outside of the range (0-255)!\n");
return 3;
}
} else {
printf("Green value missing!\n");
return 3;
}
if ((argv[n + 3]) != NULL) {
temp = atoi(argv[n + 3]);
printf("atoi %d\n", temp);
if (temp <= 255 && b >= 0) {
b = temp;
} else {
printf("Blue value is outside of the range (0-255)!\n");
return 3;
}
} else {
printf("Blue value missing!\n");
return 3;
}
n += 3;
} else if (strcmp(argv[n], "--sensor-noise") == 0) {
random_multiplier = true;
} else { // If there is no known argument at a given argc location.
printf("Unknown option \"%s\" at the %d. argument. Use -h for help.\n", argv[n], n);
return 3;
Expand All @@ -162,9 +202,14 @@ int main(int argc, char* argv[])





// Depends on allowDebugInfo == true
errorFileOpener();

printDebugPlusInt("r", r);
printDebugPlusInt("g", g);
printDebugPlusInt("b", b);

// All printDebug depends on errorFileOpener
printDebugPlusInt("sCount = ", sCount);
Expand Down Expand Up @@ -199,7 +244,6 @@ int main(int argc, char* argv[])
return 0;
}

// New:
dirCreatorLinux(outDir, termuxExternal);


Expand Down Expand Up @@ -250,18 +294,17 @@ int main(int argc, char* argv[])
printDebugPlusFloat("time:", genTime * (args->total - args->progress));


//progressbar(i, count, terminalWidth - 30, genTime); -----------------------------

// Create file for image
sprintf(imagename, "%s/random_image%d.%s", outDir, i, format);
printDebugPlusStr("Image name:", imagename);

if (strcmp(format, "png") == 0) {
// Write PNG file
errorCount = errorCount + generatePNG(imagename, width, height, alpha, allowDebugInfo);
errorCount = errorCount + generatePNG(imagename, width, height, alpha, allowDebugInfo, r, g, b, random_multiplier);
} else {
// Write JPEG file
generateJPEG(imagename, width, height, quality);
generateJPEG(imagename, width, height, quality, r, g, b, random_multiplier);
}


Expand Down Expand Up @@ -313,7 +356,7 @@ int main(int argc, char* argv[])

printf(" Moving files\n");
// TODO: Make a progressbar
// To do that, I need to manually move the files.
// use ls and mv -v (verbose flag) to check the strings
// The directory will be made with dirCreatorLinux, so the files should be only moved to it.
shellCommand[0] = '\0'; // Initialize it as an empty string

Expand Down
Loading

0 comments on commit a0e428d

Please sign in to comment.