Advertisement
xlujiax

Untitled

Oct 9th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <stdio.h>
  4. #include "funciones.h"
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main(int argc, char** argv) {
  9.  
  10. freopen("imgmini1_3x4.txt", "r", stdin);
  11. int* image;
  12. int** sparse, **sparse2, **sparse3, **sparse4, **sparse5;
  13.  
  14. image = readImg(3, 4);
  15.  
  16. writeImg(image, 3, 4);
  17.  
  18. sparse = toSparseImg(image, 3, 4);
  19. for (int i = 0; i < 3; i++) {
  20. if (sparse[i] == NULL)
  21. cout << "NULL" << endl;
  22. else {
  23. for (int j = 0; sparse[i][j] != -1; j++) {
  24. cout << sparse[i][j];
  25. }
  26. cout << endl;
  27. }
  28. }
  29. writeSparseImg(sparse, 3, 4);
  30. cout << endl;
  31.  
  32. sparse2 = bitwiseNOT_sprImg(sparse, 3, 4);
  33. for (int i = 0; i < 3; i++) {
  34. if (sparse2[i] == NULL)
  35. cout << "NULL" << endl;
  36. else {
  37. for (int j = 0; sparse2[i][j] != -1; j++) {
  38. cout << sparse2[i][j];
  39. }
  40. cout << endl;
  41. }
  42. }
  43. writeSparseImg(sparse2, 3, 4);
  44. cout << endl;
  45.  
  46. sparse3 = bitwiseAND_sprImg(sparse, sparse2, 3, 4, 3, 4);
  47. writeSparseImg(sparse3, 3, 4);
  48.  
  49. int a = 3;
  50. int b = 4;
  51. sparse4 = trim_sprImg(sparse, a, b);
  52. writeSparseImg(sparse4, a, b);
  53.  
  54. a = 3;
  55. b = 4;
  56. sparse5 = scaleUP_sprImg(sparse, a,b,2);
  57. writeSparseImg(sparse5, a, b);
  58.  
  59. return 0;
  60. }
  61. -----------------------//sparce.cpp---------------------------
  62.  
  63. #include <iostream>
  64. #include <stdio.h>
  65. #include <cstdio>
  66. #include <cstdlib>
  67. #include <iomanip>
  68. #include "funciones.h"
  69. #define INC 5
  70.  
  71. using namespace std;
  72.  
  73. int* readImg(int height, int width) {
  74. int * image = new int[height * width + 1];
  75. char dato;
  76. for (int i = 0; i < height * width; i++) {
  77. cin >> dato;
  78. if (dato == '0')
  79. image[i] = 0;
  80. else
  81. image[i] = 1;
  82. }
  83. image[height * width + 1] = -1;
  84. return image;
  85. }
  86.  
  87. void writeImg(int* image, int height, int width) {
  88. int cont = 0;
  89. for (int i = 0; i < height; i++) {
  90. for (int j = 0; j < width; j++) {
  91. if (image[cont] == 0)
  92. cout << ". ";
  93. else
  94. cout << "# ";
  95. cont++;
  96. }
  97. cout << endl;
  98. }
  99. }
  100.  
  101. void incrementar(int* &lista, int numElem) {
  102. if (lista == NULL) {
  103. lista = new int[INC + 1];
  104. for (int i = 0; i <= INC; i++) {
  105. lista[i] = -1;
  106. }
  107. } else {
  108. int* listaAux = new int[numElem + INC + 1];
  109. for (int i = 0; i < numElem; i++) {
  110. listaAux[i] = lista[i];
  111. }
  112. for (int i = numElem; i <= numElem + INC; i++) {
  113. listaAux[i] = -1;
  114. }
  115. delete [] lista;
  116. lista = listaAux;
  117. }
  118. }
  119.  
  120. void agregarDato(int * & lista, int dato) {
  121. if (lista == NULL) {
  122. incrementar(lista, INC);
  123. lista[0] = dato;
  124. lista[1] = -1;
  125. } else {
  126. int numElem = 0;
  127. for (int i = 0; lista[i] != -1; i++, numElem++);
  128. if (numElem % INC == 0)
  129. incrementar(lista, numElem);
  130. lista[numElem] = dato;
  131. lista[numElem + 1] = -1;
  132. }
  133. }
  134.  
  135. int** toSparseImg(int* image, int height, int width) {
  136. int** sparse = new int*[height + 1];
  137. for (int i = 0; i <= height; i++) {
  138. sparse[i] = NULL;
  139. }
  140. int cont = 0;
  141. for (int i = 0; i < height; i++) {
  142. for (int j = 0; j < width; j++) {
  143. if (image[cont] == 1)
  144. agregarDato(sparse[i], j);
  145. cont++;
  146. }
  147. cout << endl;
  148. }
  149. return sparse;
  150. }
  151.  
  152. void writeSparseImg(int** sparseImg, int height, int width) {
  153. for (int i = 0; i < height; i++) {
  154. if (sparseImg[i] != NULL) {
  155. int * aux = sparseImg[i];
  156. int cont = 0;
  157. for (int j = 0; j < width; j++) {
  158. if (j == aux[cont]) {
  159. cout << setw(2) << '#';
  160. cont++;
  161. } else cout << setw(2) << '.';
  162. }
  163. } else {
  164. for (int j = 0; j < width; j++) cout << setw(2) << '.';
  165. }
  166. cout << endl;
  167. }
  168. }
  169.  
  170. bool esta(int* lista, int dato) {
  171. bool esta = false;
  172. for (int i = 0; lista[i] != -1; i++) {
  173. if (lista[i] == dato) {
  174. esta = true;
  175. break;
  176. }
  177. }
  178. return esta;
  179. }
  180.  
  181. int ** bitwiseNOT_sprImg(int** sparseImg, int height, int width) {
  182. int** sparse = new int*[height + 1];
  183. for (int i = 0; i <= height; i++)
  184. sparse[i] = NULL;
  185.  
  186. for (int i = 0; i < height; i++) {
  187. if (sparseImg[i] == NULL)
  188. for (int j = 0; j < width; j++) {
  189. agregarDato(sparse[i], j);
  190. } else {
  191. int* aux = sparseImg[i];
  192. for (int j = 0; j < width; j++) {
  193. if (!esta(aux, j)) {
  194. agregarDato(sparse[i], j);
  195. }
  196. }
  197. }
  198. }
  199. return sparse;
  200. }
  201.  
  202. int ** bitwiseAND_sprImg(int ** sprImg1, int ** sprImg2, int h1, int w1, int h2, int w2){
  203.  
  204. int max;
  205. if(h1 > h2) max = h1; else max = h2;
  206. int ** sparce = new int *[max+1];
  207. for(int i=0; i<= max; i++) sparce[i] = NULL;
  208.  
  209. for(int i=0; i < max ; i++){
  210. if(sprImg1[i] != NULL && sprImg2[i] != NULL ){
  211. int * aux1 = sprImg1[i];
  212. int * aux2 = sprImg2[i];
  213. for(int j=0; aux1[j] != -1; j++){
  214. if(esta(aux2, aux1[j])){
  215. agregarDato(sparce[i], j);
  216. }
  217. }
  218. }
  219. }
  220. return sparce;
  221. }
  222.  
  223. int **trim_sprImg(int** sparseImg, int &height, int &width){
  224. int cant = 0;
  225. for (int i = 0; i < height; i++)
  226. if(sparseImg[i] != NULL) cant++;
  227.  
  228. int ** trim = new int*[cant+1];
  229. for (int i = 0; i <= cant; i++) trim[i] = NULL;
  230.  
  231.  
  232. int cont = 0;
  233.  
  234. int max = 0;
  235. for (int i = 0; i < height; i++) {
  236. if(sparseImg[i] != NULL){
  237. int *aux = sparseImg[i];
  238. width = 0;
  239. for (int j = 0; aux[j] != -1; j++) {
  240. agregarDato(trim[cont], aux[j]-1); //Para
  241. width++;
  242. }
  243. if (width > max) max = width;
  244. cont++;
  245. }
  246. }
  247. width = max;
  248. height = cant;
  249. return trim;
  250. }
  251.  
  252. int** scaleUP_sprImg(int ** sparceImg, int & height, int & width, int scale){
  253. int cant = height*2;
  254. int ** dupli = new int*[cant + 1];
  255. for(int i=0; i<= cant; i++) dupli[i] = NULL;
  256.  
  257. for(int i=0; i < cant; i+=2){
  258. if(sparceImg[i/2] == NULL)
  259. dupli[i+1] = dupli[i];
  260. if(sparceImg[i/2] != NULL){
  261. int * aux = sparceImg[i/2];
  262. for(int j=0; aux[j] != -1; j++){
  263. agregarDato(dupli[i], aux[j]*2);
  264. agregarDato(dupli[i], (aux[j]*2)+1);
  265. }
  266. dupli[i+1] = dupli[i];
  267. }
  268. }
  269. height = height*2;
  270. width = width*2;
  271. return dupli;
  272. }
  273. //sparse.h
  274. #ifndef FUNCIONES_H
  275. #define FUNCIONES_H
  276.  
  277. int* readImg(int, int);
  278. void writeImg(int*, int, int);
  279. int ** toSparseImg(int*, int, int);
  280. void writeSparseImg(int** , int , int );
  281. int ** bitwiseNOT_sprImg(int**, int, int);
  282. int ** bitwiseAND_sprImg(int **, int **, int, int, int, int);
  283. int ** trim_sprImg(int**, int &, int&);
  284. int **scaleUP_sprImg(int**, int &, int&, int);
  285.  
  286. #endif /* FUNCIONES_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement