Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdint.h>
- #include <math.h>
- static uint8_t *generate_edgemask(uint8_t *src, int width, int height, int pitch)
- {
- const int sobel_h[] = {1, 0, -1, 2, 0, -2, 1, 0, -1};
- const int sobel_v[] = {1, 2, 1, 0, 0, 0, -1, -2, -1};
- uint8_t *dst = calloc(width * height, 1);
- if (!dst) {
- return NULL;
- }
- uint8_t *srcp = src;
- uint8_t *dstp = dst;
- for (int y = 1; y < height - 1; y++) {
- uint8_t *top = srcp;
- srcp += pitch;
- uint8_t *bottom = srcp + pitch;
- dstp += width;
- for (int x = 1; x < width - 1; x++) {
- uint8_t array = {
- top[x - 1], top[x], top[x + 1],
- srcp[x - 1], srcp[x], srcp[x + 1],
- bottom[x - 1], bottom[x], bottom[x + 1]
- };
- int val_h = 0;
- int val_v = 0;
- for (int i = 0; i < 9; i++) {
- val_h += array[i] * sobel_h[i];
- val_v += array[i] * sobel_v[i];
- }
- dst[x] = (uint8_t)((sqrt((double)(val_h * val_h + val_v * val_v))) / 8);
- }
- }
- return dst;
- }
- static int write_mask(uint8_t *mask, size_t size)
- {
- FILE *fp = fopen("mask.y8", "wb");
- if (!fp)
- return -1;
- fwrite(mask, size, 1, fp);
- fclose(fp);
- return 0;
- }
- int main(void)
- {
- int ret = -1;
- int width = 352, height = 288;
- size_t size = width * height;
- uint8_t *src = malloc(size);
- if (!src) {
- return ret;
- }
- File *fsrc = ("cif.y8", "rb");
- if (!fsrc) {
- free(src);
- return ret;
- }
- if (fread(src, size, 1, fsrc) == size) {
- uint8_t *mask = generate_edgemask(src, width, height, width);
- if (!mask)
- break;
- ret = write_mask(mask, size);
- free(mask);
- }
- free(src);
- fclose(fp);
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement