Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5.  
  6. #define WIDTH 512
  7. #define HEIGHT 512
  8. #define DEPTH 255
  9.  
  10. static const unsigned MAX = 8;
  11. void *digitizer(); //Function prototype
  12. void *tracker(); //Function prototype
  13.  
  14. pthread_cond_t buf_notfull = PTHREAD_COND_INITIALIZER;
  15. pthread_cond_t buf_notempty = PTHREAD_COND_INITIALIZER;
  16.  
  17. /* For safe condition variable usage, must use a boolean predicate and */
  18. /* a mutex with the condition. */
  19. // int bufavail = 0;
  20. int bufavail = MAX;
  21. char* frame_buf[MAX];
  22. pthread_mutex_t buflock = PTHREAD_MUTEX_INITIALIZER;
  23.  
  24. char *read(void)
  25. {
  26. FILE *infile; /* lenna image */
  27. char *pixels; /* image to populate */
  28. size_t i;
  29.  
  30. infile = fopen("lena512.pgm", "r");
  31. /* skip magic number, comment, width, height, and depth */
  32. for (i = 0; i < 4; ++i)
  33. fscanf(infile, "%*[^\n]\n");
  34. pixels = (char *)malloc(WIDTH * HEIGHT * sizeof(char));
  35. /* read each row of image */
  36. for (i = 0; i < HEIGHT; ++i) {
  37. fread(&pixels[i*WIDTH], sizeof(char), WIDTH, infile);
  38. }
  39. fclose(infile);
  40. return pixels;
  41. }
  42.  
  43. void write(char *pixels)
  44. {
  45. FILE *outfile;
  46. size_t i;
  47.  
  48. outfile = fopen("rotated.pgm", "w");
  49. fputs("P5\n", outfile); /* magic number for a binary pgm */
  50. /* the width, height, and depth of the image */
  51. fprintf(outfile, "%d %d\n%d\n", WIDTH, HEIGHT, DEPTH);
  52. /* now write each row */
  53. for (i = 0; i < HEIGHT; ++i) {
  54. fwrite(&pixels[i*WIDTH], sizeof(char), WIDTH, outfile);
  55. }
  56. fclose(outfile);
  57. free(pixels);
  58. }
  59.  
  60. void rotate(char *pS, char *pD, unsigned int row, unsigned int col)
  61. {
  62. unsigned int r, c;
  63. for (r = 0; r < row; r++)
  64. {
  65. for (c = 0; c < col; c++)
  66. {
  67. *(pD + c * row + (row - r - 1)) = *(pS + r * col + c);
  68. }
  69. }
  70. write(pD);
  71. }
  72.  
  73. void *digitizer()
  74. {
  75. char* dig_image;
  76. int tail = 0;
  77.  
  78. while(1) {
  79. dig_image = read();
  80.  
  81. pthread_mutex_lock(&buflock);
  82. if (bufavail == 0) {
  83. pthread_cond_wait(&buf_notempty, &buflock);
  84. }
  85. pthread_mutex_unlock(&buflock);
  86.  
  87. frame_buf[tail%MAX] = dig_image;
  88. tail = tail + 1;
  89.  
  90. pthread_mutex_lock(&buflock);
  91. bufavail = bufavail - 1;
  92. pthread_mutex_unlock(&buflock);
  93.  
  94. if(bufavail < MAX) {
  95. pthread_cond_broadcast(&buf_notfull);
  96. }
  97. }
  98. }
  99.  
  100. void *tracker()
  101. {
  102. char *pSource;
  103. char *pDestination;
  104. char *track_image;
  105. int head = 0;
  106.  
  107. while(1) {
  108. pthread_mutex_lock(&buflock);
  109. if (bufavail == MAX)
  110. pthread_cond_wait(&buf_notfull, &buflock);
  111. pthread_mutex_unlock(&buflock);
  112.  
  113. pSource = frame_buf[head%MAX];
  114. head = head + 1;
  115.  
  116. pthread_mutex_lock(&buflock);
  117. bufavail = bufavail + 1;
  118. pthread_mutex_unlock(&buflock);
  119.  
  120. if(bufavail > 0) {
  121. pthread_cond_broadcast(&buf_notempty);
  122. }
  123.  
  124. pDestination = (char *)malloc(HEIGHT * WIDTH);
  125. rotate(pSource, pDestination, HEIGHT, WIDTH);
  126. }
  127. }
  128.  
  129. int main(void)
  130. {
  131. pthread_t thread1, thread2;
  132. /* Create independent threads each of which will execute function */
  133. pthread_create (&thread1, NULL, digitizer, NULL);
  134. pthread_create (&thread2, NULL, tracker, NULL);
  135.  
  136. /* Wait till threads are complete before main continues. Unless we */
  137. /* wait we run the risk of executing an exit which will terminate */
  138. /* the process and all threads before the threads have completed. */
  139.  
  140. pthread_join(thread1, NULL);
  141. pthread_join(thread2, NULL);
  142. // return 0;
  143. exit(0);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement