Advertisement
Guest User

Untitled

a guest
Sep 8th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include <math.h>
  4.  
  5. static uint8_t *generate_edgemask(uint8_t *src, int width, int height, int pitch)
  6. {
  7.     const int sobel_h[] = {1, 0, -1, 2, 0, -2, 1, 0, -1};
  8.     const int sobel_v[] = {1, 2, 1, 0, 0, 0, -1, -2, -1};
  9.     uint8_t *dst = calloc(width * height, 1);
  10.     if (!dst) {
  11.         return NULL;
  12.     }
  13.  
  14.     uint8_t *srcp = src;
  15.     uint8_t *dstp = dst;
  16.     for (int y = 1; y < height - 1; y++) {
  17.         uint8_t *top = srcp;
  18.         srcp += pitch;
  19.         uint8_t *bottom = srcp + pitch;
  20.         dstp += width;
  21.         for (int x = 1; x < width - 1; x++) {
  22.             uint8_t array = {
  23.                 top[x - 1], top[x], top[x + 1],
  24.                 srcp[x - 1], srcp[x], srcp[x + 1],
  25.                 bottom[x - 1], bottom[x], bottom[x + 1]
  26.             };
  27.             int val_h = 0;
  28.             int val_v = 0;
  29.             for (int i = 0; i < 9; i++) {
  30.                 val_h += array[i] * sobel_h[i];
  31.                 val_v += array[i] * sobel_v[i];
  32.             }
  33.             dst[x] = (uint8_t)((sqrt((double)(val_h * val_h + val_v * val_v))) / 8);
  34.         }
  35.     }
  36.     return dst;
  37. }
  38.  
  39. static int write_mask(uint8_t *mask, size_t size)
  40. {
  41.     FILE *fp = fopen("mask.y8", "wb");
  42.     if (!fp)
  43.         return -1;
  44.     fwrite(mask, size, 1, fp);
  45.     fclose(fp);
  46.     return 0;
  47. }
  48.  
  49. int main(void)
  50. {
  51.     int ret = -1;
  52.     int width = 352, height = 288;
  53.     size_t size = width * height;
  54.  
  55.     uint8_t *src = malloc(size);
  56.     if (!src) {
  57.         return ret;
  58.     }
  59.  
  60.     File *fsrc = ("cif.y8", "rb");
  61.     if (!fsrc) {
  62.         free(src);
  63.         return ret;
  64.     }
  65.  
  66.     if (fread(src, size, 1, fsrc) == size) {
  67.         uint8_t *mask = generate_edgemask(src, width, height, width);
  68.         if (!mask)
  69.             break;
  70.         ret = write_mask(mask, size);
  71.         free(mask);
  72.     }
  73.  
  74.     free(src);
  75.     fclose(fp);
  76.     return ret;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement