Advertisement
Guest User

Untitled

a guest
Jun 9th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. #include <windows.h>
  2. #include <time.h>
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <cutil.h>
  7. using namespace std;
  8. #include "cuda_runtime.h"
  9. #include "device_launch_parameters.h"
  10. #define THREADS_PER_BLOCK 3
  11. FILE*forg=fopen("C:\\Users\\szparson\\Documents\\Visual Studio 2010\\Projects\\bitmapa\\bitmapa\\input1.bmp", "rb"); //Uchwyt do orginalnego pliku
  12. FILE*fsz=fopen("C:\\Users\\szparson\\Documents\\Visual Studio 2010\\Projects\\bitmapa\\bitmapa\\output1.bmp", "wb"); //Uchwyt do nowego pliku
  13. struct FileHeader {
  14. short bfType;
  15. int bfSize;
  16. short bfReserved1;
  17. short bfReserved2;
  18. short bfOffBits;
  19. };
  20. FileHeader File;
  21.  
  22. struct PictureHeader {
  23. int biSize;
  24. int biWidth;
  25. int biHeight;
  26. short biPlanes;
  27. short biBitCount;
  28. int biCompression;
  29. int biSizeImage;
  30. int biXPelsPerMeter;
  31. int biYPelsPerMeter;
  32. int biClrUsed;
  33. int biClrImportant;
  34. };
  35. PictureHeader Picture;
  36.  
  37. void header(){
  38.  
  39. fread(&File.bfType,sizeof(File.bfType),1,forg);
  40. cout<<"Typ:"<< File.bfType <<endl;
  41. fread(&File.bfSize,sizeof(File.bfSize),1,forg);
  42. cout << "Rozmiar pliku: " << File.bfSize << " bajtow" << endl;
  43.  
  44. fread(&File.bfReserved1,sizeof(File.bfReserved1),1,forg);
  45. cout << "Zarezerwowane1: " << File.bfReserved1 << endl;
  46.  
  47. fread(&File.bfReserved2,sizeof(File.bfReserved2),1,forg);
  48. cout << "Zarezerwowane2: " << File.bfReserved2 << endl;
  49.  
  50. fread(&File.bfOffBits,sizeof(File.bfOffBits),1,forg);
  51. cout << "Pozycja danych obrazkowych: " << File.bfOffBits << endl;
  52.  
  53. printf("\n");
  54.  
  55. fseek(forg,14,SEEK_SET);
  56. fread(&Picture.biSize,sizeof(Picture.biSize),1,forg);
  57. cout <<"Wielkosc naglowka informacyjnego: " << Picture.biSize << endl;
  58.  
  59. fread(&Picture.biWidth,sizeof(Picture.biWidth),1,forg);
  60. cout <<"Szerokosc: " << Picture.biWidth << " pikseli "<<endl;
  61.  
  62. fread(&Picture.biHeight,sizeof(Picture.biHeight),1,forg);
  63. cout << "Wysokosc: " << Picture.biHeight << " pikseli "<< endl;
  64.  
  65. fread(&Picture.biPlanes,sizeof(Picture.biPlanes),1,forg);
  66. cout << "Liczba platow (zwykle 0): " << Picture.biPlanes << endl;
  67.  
  68. fread(&Picture.biBitCount,sizeof(Picture.biBitCount),1,forg);
  69. cout << "Liczba bitow na piksel: (1, 4, 8, or 24)" << Picture.biBitCount << endl;
  70.  
  71. fread(&Picture.biCompression,sizeof(Picture.biCompression),1,forg);
  72. cout <<"Kompresja: " << Picture.biCompression << "(0=none, 1=RLE-8, 2=RLE-4)" << endl;
  73.  
  74. fread(&Picture.biSizeImage,sizeof(Picture.biSizeImage),1,forg);
  75. cout <<"Rozmiar samego rysunku: " << Picture.biSizeImage << endl;
  76.  
  77. fread(&Picture.biXPelsPerMeter,sizeof(Picture.biXPelsPerMeter),1,forg);
  78. cout <<"Rozdzielczosc pozioma: " << Picture.biXPelsPerMeter << endl;
  79.  
  80. fread(&Picture.biYPelsPerMeter,sizeof(Picture.biYPelsPerMeter),1,forg);
  81. cout <<"Rozdzielczosc pionowa: " << Picture.biYPelsPerMeter << endl;
  82.  
  83. fread(&Picture.biClrUsed,sizeof(Picture.biClrUsed),1,forg);
  84. cout <<"Liczba kolorow w palecie: "<< Picture.biClrUsed << endl;
  85.  
  86. fread(&Picture.biClrImportant,sizeof(Picture.biClrImportant),1,forg);
  87. cout <<"Wazne kolory w palecie: " << Picture.biClrImportant << endl;
  88. }
  89. char z;
  90. __global__ void ReadImage(int *B, int *G, int *R, int bfSize )
  91. {
  92. int index = threadIdx.x + blockIdx.x * THREADS_PER_BLOCK;
  93. if (index < bfSize)
  94. {
  95. B[index] = B[index]+10;
  96. G[index] = G[index]-10;
  97. R[index] = R[index]+10;
  98. }
  99.  
  100. }
  101. int main()
  102. {
  103. PrintDeviceInformation();
  104. header();
  105. time_t czas, czas1, czas2, czas3;
  106. time_t start, start1, start2, start3;
  107. int ile = 1;
  108. long liczba_blokow = Picture.biWidth * Picture.biHeight;
  109. int *B, *G, *R;
  110. int *d_B, *d_G, *d_R;
  111. B=new int[File.bfSize*sizeof(int)];
  112. G=new int[File.bfSize*sizeof(int)];
  113. R=new int[File.bfSize*sizeof(int)];
  114.  
  115. cudaMalloc(&d_B, File.bfSize*sizeof(int));
  116. cudaMalloc(&d_G, File.bfSize*sizeof(int));
  117. cudaMalloc(&d_R, File.bfSize*sizeof(int));
  118.  
  119. fseek(forg,0,SEEK_SET);
  120. for(int i=0; i<File.bfOffBits; i++)
  121. {
  122. z=fgetc(forg);
  123. fprintf(fsz, "%c",z); //Utworzenie naglowka nowej Bitmapy
  124. }
  125. for (int i = File.bfOffBits; i < File.bfSize; i++)
  126. {
  127. B[i]=fgetc(forg);
  128. G[i]=fgetc(forg);
  129. R[i]=fgetc(forg);
  130. }
  131. start = clock();
  132. start1 = clock();
  133.  
  134. for( unsigned int i = 0; i < ile; i++)
  135. {
  136. cudaMemcpy(d_B, B, File.bfSize*sizeof(int), cudaMemcpyHostToDevice);
  137. cudaMemcpy(d_G, G, File.bfSize*sizeof(int), cudaMemcpyHostToDevice);
  138. cudaMemcpy(d_R, R, File.bfSize*sizeof(int), cudaMemcpyHostToDevice);
  139. }
  140.  
  141. czas1= (float(clock() - start1)*CLOCKS_PER_SEC)/1000;
  142. cout << "Przesylanie do CUDA " << dec << czas1 << " milisekund " <<endl;
  143.  
  144. start2 = clock();
  145.  
  146. for( unsigned int i = 0; i < ile; i++)
  147. ReadImage<<< liczba_blokow, THREADS_PER_BLOCK>>>(d_B, d_G, d_R, File.bfSize);
  148.  
  149. czas2= (float(clock() - start2)*CLOCKS_PER_SEC)/1000;
  150. cout << "Wykonanie funkcji " << dec << czas2 << " milisekund " <<endl;
  151. start3 = clock();
  152. for( unsigned int i = 0; i < ile; i++)
  153. {
  154. cudaMemcpy(B, d_B, File.bfSize*sizeof(int), cudaMemcpyDeviceToHost);
  155. cudaMemcpy(G, d_G, File.bfSize*sizeof(int), cudaMemcpyDeviceToHost);
  156. cudaMemcpy(R, d_R, File.bfSize*sizeof(int), cudaMemcpyDeviceToHost);
  157. }
  158. czas3= (float(clock() - start3)*CLOCKS_PER_SEC)/1000;
  159. cout << "Powrót z CUDA " << dec << czas3 << " milisekund " <<endl;
  160.  
  161. czas= (float(clock() - start)*CLOCKS_PER_SEC)/1000;
  162. cout << "Program wykonywal sie " << dec << czas << " milisekund " <<endl;
  163. fseek(fsz,54,SEEK_SET);
  164. for (int i = File.bfOffBits; i < File.bfSize; i++)
  165. {
  166. fprintf(fsz, "%c", (int)(B[i]));
  167. fprintf(fsz, "%c", (int)(G[i]));
  168. fprintf(fsz, "%c", (int)(R[i]));
  169. }
  170.  
  171. delete[] B;
  172. delete[] G;
  173. delete[] R;
  174. cudaFree(d_B); cudaFree(d_G); cudaFree(d_R);
  175. system("PAUSE");
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement