public mazegenerator() { quadtable = new block [100,100]; for(int i = 0; i < 2000; i+=20) { for(int j = 0; j < 2000; j+=20) { block currentsquare = new block(19,19,i,j,"earth"); quadtable[i/20,j/20] = currentsquare; } } room newroom = new room(20,20,6,4); rooms.Add(newroom); foreach (Vector2 wall in newroom.walls) { quadtable[(int)wall.x,(int)wall.y] = new block(19,19,wall.x*20,wall.y*20,"wall"); } foreach (Vector2 floor in newroom.floortiles) { quadtable[(int)floor.x,(int)floor.y] = new block(19,19,floor.x*20,floor.y*20,"floor"); } }
public void upSampling(int width, int height, ref block[,] YUVblocks, block[,] Yblocks, block[,] UVblocks) { for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { //for Y block just copy elements because it wasn't subsampled block YUVblock = new block(8, 8, new Point(i * 8, j * 8)); for (int k = 0; k < 8; k++) { for (int l = 0; l < 8; l++) { YUVblock.pixelMatrix[k, l].r = Yblocks[i, j].pixelMatrix[k, l].r; } } //for U and V block apply upSampling because was 4:2:0 subsampled for (int k = 0; k < 4; k++) { for (int l = 0; l < 4; l++) { YUVblock.pixelMatrix[k * 2, l * 2].g = YUVblock.pixelMatrix[k * 2 + 1, l * 2].g = YUVblock.pixelMatrix[k * 2, l * 2 + 1].g = YUVblock.pixelMatrix[k * 2 + 1, l * 2 + 1].g = UVblocks[i, j].pixelMatrix[k, l].g; //u block YUVblock.pixelMatrix[k * 2, l * 2].b = YUVblock.pixelMatrix[k * 2 + 1, l * 2].b = YUVblock.pixelMatrix[k * 2, l * 2 + 1].b = YUVblock.pixelMatrix[k * 2 + 1, l * 2 + 1].b = UVblocks[i, j].pixelMatrix[k, l].b; //v block } } YUVblocks[i, j] = YUVblock; } } }
private void button1_Click(object sender, EventArgs e) { string type = ""; string comm = ""; string[] dimarray = new string[2]; string maxPixelValue = ""; int width = 0; int height = 0; Image img = new Image(800, 600); readImage(ref type, ref comm, ref maxPixelValue, ref width, ref height, ref img); block[,] Yblocks = new block[height / 8, width / 8]; block[,] UVblocks = new block[height / 8, width / 8]; block[,] YUVblocks = new block[height / 8, width / 8]; encoding(width, height, img, ref Yblocks, ref UVblocks); upSampling(width, height, ref YUVblocks, Yblocks, UVblocks); block[,] dctblock = dctTransform(YUVblocks, width, height); quantize(ref dctblock, width, height); int[] entropyEncodedArray = entropyEncoding(dctblock, width, height); block[,] entropyDecodedBlocks = entropyDecoding(width, height, entropyEncodedArray); deQuantize(ref entropyDecodedBlocks, width, height); block[,] invDCT = dctInvTransform(entropyDecodedBlocks, width, height); Image decoded = decoding(width, height, invDCT); //for lab1 Image decoded = decoding(width, height, Yblocks, UVblocks); saveImage(type, comm, maxPixelValue, width, height, decoded); MessageBox.Show("Done"); }
//encoding with 4:2:0 subsampling (U and V blocks are 4x4 size and each pixel stores average of 2x2 pixels) public void encoding(int width, int height, Image image, ref block[,] Yblocks, ref block[,] UVblocks) { //convert to YUV image pixel matrix var YUVimg = new Image(width, height); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { YUVimg.pixelMatrix[i, j].r = Convert.ToInt32(0.299 * image.pixelMatrix[i, j].r + 0.587 * image.pixelMatrix[i, j].g + 0.114 * image.pixelMatrix[i, j].b); YUVimg.pixelMatrix[i, j].g = Convert.ToInt32(128 - 0.1687 * image.pixelMatrix[i, j].r - 0.3312 * image.pixelMatrix[i, j].g + 0.5 * image.pixelMatrix[i, j].b); YUVimg.pixelMatrix[i, j].b = Convert.ToInt32(128 + 0.5 * image.pixelMatrix[i, j].r - 0.4186 * image.pixelMatrix[i, j].g - 0.0813 * image.pixelMatrix[i, j].b); } } for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { block Yblock = new block(8, 8, new Point(i * 8, j * 8)); for (int k = 0; k < 8; k++) { for (int l = 0; l < 8; l++) { Yblock.pixelMatrix[k, l].r = YUVimg.pixelMatrix[i * 8 + k, j *8 + l].r; } } Yblocks[i, j] = Yblock; block UVblock = new block(4, 4, new Point(i * 8, j * 8)); for (int k = 0; k < 4; k++) { for (int l = 0; l < 4; l++) { UVblock.pixelMatrix[k, l].g = UVblock.pixelMatrix[k, l].g = UVblock.pixelMatrix[k, l].g = UVblock.pixelMatrix[k, l].g = (YUVimg.pixelMatrix[i * 8 + k * 2, j * 8 + l * 2].g + YUVimg.pixelMatrix[i * 8 + k * 2 + 1, j * 8 + l * 2].g + YUVimg.pixelMatrix[i * 8 + k * 2, j * 8 + l * 2 + 1].g + YUVimg.pixelMatrix[i * 8 + k * 2 + 1, j * 8 + l * 2 + 1].g) / 4; UVblock.pixelMatrix[k, l].b = (YUVimg.pixelMatrix[i * 8 + k * 2, j * 8 + l * 2].b + YUVimg.pixelMatrix[i * 8 + k * 2 + 1, j * 8 + l * 2].b + YUVimg.pixelMatrix[i * 8 + k * 2, j * 8 + l * 2 + 1].b + YUVimg.pixelMatrix[i * 8 + k * 2 + 1, j * 8 + l * 2 + 1].b) / 4; } } UVblocks[i, j] = UVblock; } } }
block[,] dctTransform(block[,] YUVblocks, int width, int height) { block[,] dctBlocks = new block[height / 8, width / 8]; for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { dctBlocks[i, j] = dctMatrixTransform(YUVblocks[i, j], 8, 8); } } return(dctBlocks); }
/// <summary> /// Constructor from string. /// <para>String is flatten array of a sort & we cut it upon new line.</para> /// </summary> public Map(string line) { // TODO: exceptions are needed // string length may differ from dimensions this.gameMap = new block[MapHight, MapWidth]; string[] flatArray = line.Split('\n'); // split upon new line for (int i = 0; i < MapHight; i++) { for (int j = 0; j < MapWidth; j++) { this.gameMap[i, j] = (block)int.Parse(flatArray[i][j] + ""); } } }
public cache(int BA_N, int BL_N, int T_S, int P_S) { TAG_SIZE = T_S; PAYLOAD_SIZE = P_S; INDEX_SIZE = Globals.PHYSICAL_ADD_LEN - TAG_SIZE - Globals.BYTE_OFF_LEN; BANK_NUM = BA_N; BLOCK_NUM = BL_N; bankS = new block[ BLOCK_NUM , BANK_NUM]; for (int i = 0; i < BLOCK_NUM; i++) for (int j = 0; j < BANK_NUM; j++) bankS[i, j] = new block(TAG_SIZE, PAYLOAD_SIZE); }
void deQuantize(ref block[,] dctblocks, int width, int height) { for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { for (int k = 0; k < 8; k++) { for (int l = 0; l < 8; l++) { dctblocks[i, j].pixelMatrix[k, l].r *= Q[k, l]; dctblocks[i, j].pixelMatrix[k, l].g *= Q[k, l]; dctblocks[i, j].pixelMatrix[k, l].b *= Q[k, l]; } } } } }
int [] entropyEncoding(block[,] dctBlocks, int width, int height) { int [] auxByteArray = new int[1000000]; int length = 0; for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { //array obtained after traverse y,u and v matrix int[] zigZagArray = new int[1000]; zigZagArray = zigZagMatrix(dctBlocks[i, j], 8, 8); int zeros = 0; int l = 64 * 3; //to see if dct->zigzag array are working good if (i == 0 && j == 0) { Console.WriteLine(dctBlocks[i, j].pos.x + " " + dctBlocks[i, j].pos.y); Console.WriteLine("YUV blocks before entropy encoding:"); dctBlocks[i, j].toString(); Console.WriteLine("ZigZag array before entropy encoding:"); for (int p = 0; p < l; p++) { Console.Write(zigZagArray[p] + " "); } Console.WriteLine(); } for (int p = 0; p < l; p++) { //add (size,amplitude) for first elem of each y/u/v block if (p == 0 || p == 64 || p == 128) { zeros = 0; auxByteArray[++length] = getSize(zigZagArray[p]); auxByteArray[++length] = zigZagArray[p]; } else //if are last elem from each y/u/v block if (p == 63 || p == 127 || p == 191) { if (zigZagArray[p] == 0)//if it have 0 value add (0,0) pair { auxByteArray[++length] = 0; auxByteArray[++length] = 0; } else //else add (runlight,size)(amplitude) { auxByteArray[++length] = zeros; auxByteArray[++length] = getSize(zigZagArray[p]); auxByteArray[++length] = zigZagArray[p]; zeros = 0; } } //if are not first or last elems from yuv blocks else { if (zigZagArray[p] != 0) { auxByteArray[++length] = zeros; auxByteArray[++length] = getSize(zigZagArray[p]); auxByteArray[++length] = zigZagArray[p]; zeros = 0; } else { zeros++; } } } //store array length on 0 pos auxByteArray[0] = length; //to see if zigZag array -> entropped array are working good if (i == 1 && j == 2) { Console.WriteLine("Entropped array:"); for (int q = 0; q <= length; ++q) { Console.Write(auxByteArray[q] + " "); } Console.WriteLine(); } } } return(auxByteArray); }
Image decoding(int width, int height, block[,] YUVblocks) { Image newImage = new Image(width, height); for (int i = 0; i < height / 8; i++) { for (int j = 0; j < width / 8; j++) { int x = YUVblocks[i, j].pos.x; int y = YUVblocks[i, j].pos.y; for (int k = 0; k < 8; k++) { for (int l = 0; l < 8; l++) { newImage.pixelMatrix[x + k, y + l].r = YUVblocks[i, j].pixelMatrix[k, l].r; newImage.pixelMatrix[x + k, y + l].g = YUVblocks[i, j].pixelMatrix[k, l].g; newImage.pixelMatrix[x + k, y + l].b = YUVblocks[i, j].pixelMatrix[k, l].b; } } } } for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int y = newImage.pixelMatrix[i, j].r; int u = newImage.pixelMatrix[i, j].g; int v = newImage.pixelMatrix[i, j].b; int x1 = Convert.ToInt32(y + 1.140 * (v - 128)); if (x1 < 0) { x1 = 0; } if (x1 > 255) { x1 = 255; } int x2 = Convert.ToInt32(y - 0.344 * (u - 128) - 0.711 * (v - 128)); if (x2 < 0) { x2 = 0; } if (x2 > 255) { x2 = 255; } int x3 = Convert.ToInt32(y + 1.772 * (u - 128)); if (x3 < 0) { x3 = 0; } if (x3 > 255) { x3 = 255; } newImage.pixelMatrix[i, j].r = x1; newImage.pixelMatrix[i, j].g = x2; newImage.pixelMatrix[i, j].b = x3; } } return(newImage); }
public Map() { Matrix = new block[4, 8]; }
public Map(block[,] gMap) { this.gameMap = (block[, ])gMap.Clone(); }
block[,] gameMap; // map itself // Constructors /// <summary> /// Empty constructor for the map. /// </summary> public Map() { this.gameMap = new block[MapHight, MapWidth]; }