| 1 |
|
| 2 |
#include "mmxnow.h" |
| 3 |
#include <stdlib.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
mmxnow_image_t* read_pgm(const char* filename) |
| 8 |
{ |
| 9 |
mmxnow_image_t* img = malloc(sizeof(mmxnow_image_t)); |
| 10 |
FILE* f; |
| 11 |
char b[1000]; |
| 12 |
size_t size; |
| 13 |
|
| 14 |
if (!img) |
| 15 |
return 0; |
| 16 |
|
| 17 |
memset(img, 0, sizeof(img)); |
| 18 |
f = fopen(filename, "r"); |
| 19 |
if (!f) |
| 20 |
{ |
| 21 |
perror("file open"); |
| 22 |
goto fail; |
| 23 |
} |
| 24 |
|
| 25 |
if (!fgets(b, sizeof(b), f)) |
| 26 |
goto fail; |
| 27 |
|
| 28 |
if (strncmp(b, "P6", 2)) |
| 29 |
{ |
| 30 |
printf("unsupported format %s\n", b); |
| 31 |
goto fail; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
for (;;) |
| 36 |
{ |
| 37 |
fgets(b, sizeof(b), f); |
| 38 |
if (b[0] != '#') |
| 39 |
break; |
| 40 |
} |
| 41 |
sscanf(b, "%d %d", &img->width, &img->height); |
| 42 |
fgets(b, sizeof(b), f); |
| 43 |
if (!strcmp(b, "255")) |
| 44 |
{ |
| 45 |
printf("unsupported img format %s\n", b); |
| 46 |
goto fail; |
| 47 |
} |
| 48 |
size = img->width * img->height * 3; |
| 49 |
img->plane[0] = (uint8_t*) malloc(size); |
| 50 |
if (!img->plane[0]) |
| 51 |
goto fail; |
| 52 |
if (!feof(f)) |
| 53 |
fread(img->plane[0], size, 1, f); |
| 54 |
|
| 55 |
return img; |
| 56 |
|
| 57 |
fail: |
| 58 |
free(img); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
|
| 62 |
void write_pgm(const char* filename, mmxnow_image_t* img) |
| 63 |
{ |
| 64 |
FILE* f = fopen(filename, "w"); |
| 65 |
if (f) |
| 66 |
{ |
| 67 |
fprintf(f, "P6\n# mmxnow test image\n%d %d\n255\n", |
| 68 |
img->width, img->height); |
| 69 |
fwrite(img->plane[0], 1, (unsigned) (3 * img->width * img->height), f); |
| 70 |
printf("Written image: %s %dx%d\n", filename, |
| 71 |
img->width, img->height); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
int main(int argc, char* argv[]) |
| 76 |
{ |
| 77 |
mmxnow_image_t* img; |
| 78 |
|
| 79 |
img = read_pgm("test.pgm"); |
| 80 |
|
| 81 |
if (img) |
| 82 |
{ |
| 83 |
mmxnow_context_t* rgb24_yv12, *yv12_rgb16, *rgb16_rgb24; |
| 84 |
unsigned int flags = MMXNOW_MMX; |
| 85 |
|
| 86 |
rgb24_yv12 = mmxnow_create(MMXNOW_BGR24_TO_YV12, flags); |
| 87 |
yv12_rgb16 = mmxnow_create(MMXNOW_YV12_TO_BGR16, flags); |
| 88 |
rgb16_rgb24 = mmxnow_create(MMXNOW_BGR16_TO_BGR24, flags); |
| 89 |
|
| 90 |
if (rgb24_yv12 && yv12_rgb16 && rgb16_rgb24) |
| 91 |
{ |
| 92 |
printf("converting image\n"); |
| 93 |
} |
| 94 |
else |
| 95 |
printf("context %p %p %p\n", rgb24_yv12, yv12_rgb16, rgb16_rgb24); |
| 96 |
|
| 97 |
write_pgm("testX.pgm", img); |
| 98 |
} |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |