Skip to content

Commit

Permalink
Make example program to take command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
61315 committed Feb 12, 2022
1 parent 4990f98 commit 1c13d30
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions examples/ppm.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// examples/ppm.c
/* examples/ppm.c
* $ ./ppm source! mask! result? context? neighbors? probes?
*/
#include <stdlib.h>
#include <stdio.h>

Expand Down Expand Up @@ -37,6 +39,11 @@ static struct ppm *
ppm_read(const char *filename)
{
FILE *f = fopen(filename, "rb");
if (f == NULL)
{
perror(filename);
return NULL;
}
struct ppm *m;
long width, height;
if (fscanf(f, "P6 %ld%ld%*d%*c", &width, &height) < 2)
Expand All @@ -47,15 +54,29 @@ ppm_read(const char *filename)
return m;
}

int main()
int main(int argc, char **argv)
{
const char *source_in = argc > 1 ? argv[1] : NULL;
const char *mask_in = argc > 2 ? argv[2] : NULL;
const char *result_out = argc > 3 ? argv[3] : "out.ppm";
const int context = argc > 4 ? atoi(argv[4]) : 1;
const int neighbors = argc > 5 ? atoi(argv[5]) : 9;
const int probes = argc > 6 ? atoi(argv[6]) : 64;

printf("context: %d neighbors: %d probes: %d\n", context, neighbors, probes);
#ifdef _WIN32
int _setmode(int, int);
_setmode(0, 0x8000);
_setmode(1, 0x8000);
#endif
struct ppm *source_ppm = ppm_read("assets/source.ppm");
struct ppm *mask_ppm = ppm_read("assets/mask.ppm");
struct ppm *source_ppm = ppm_read(source_in);
struct ppm *mask_ppm = ppm_read(mask_in);

if (source_ppm == NULL || mask_ppm == NULL)
{
fprintf(stderr, "Source or mask image not provided.\n");
exit(EXIT_FAILURE);
}

unsigned char *mask_grayscale = malloc(mask_ppm->width * mask_ppm->height);

Expand All @@ -82,23 +103,24 @@ int main()
TImageSynthParameters params = {
.isMakeSeamlesslyTileableHorizontally = FALSE,
.isMakeSeamlesslyTileableVertically = FALSE,
.matchContextType = 2,
.matchContextType = context,
.mapWeight = 0.5f,
.sensitivityToOutliers = 0.117f,
.patchSize = 9,
.maxProbeCount = 50,
.patchSize = neighbors,
.maxProbeCount = probes,
};

int cancel_token = 0;
int error = 0;

error = imageSynth(&source_buffer, &mask_buffer, T_RGB, &params, NULL, NULL, &cancel_token);

printf("Task finished with the error code: %d\n", error);
if (error != 0)
fprintf(stderr, "Operation failed. Error code: %d\n", error);

FILE *file_result = fopen("ppm_result.ppm", "wb");
ppm_write(source_ppm, file_result);
fclose(file_result);
FILE *result_file = fopen(result_out, "wb");
ppm_write(source_ppm, result_file);
fclose(result_file);

free(source_ppm);
free(mask_ppm);
Expand Down

0 comments on commit 1c13d30

Please sign in to comment.