diff options
| author | Ray <[email protected]> | 2018-11-06 15:10:50 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2018-11-06 15:10:50 +0100 |
| commit | fc1c9505ba94007a6c03d7f61a14df4a72637346 (patch) | |
| tree | fd6257c1809a86466528d9ee5e848db045240827 /src/textures.c | |
| parent | e340517a735c9a563b39e24809b2f214ece991ad (diff) | |
| download | raylib-fc1c9505ba94007a6c03d7f61a14df4a72637346.tar.gz raylib-fc1c9505ba94007a6c03d7f61a14df4a72637346.zip | |
Remove end-line spaces
Diffstat (limited to 'src/textures.c')
| -rw-r--r-- | src/textures.c | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/src/textures.c b/src/textures.c index aa9e66f6..17f2ae8a 100644 --- a/src/textures.c +++ b/src/textures.c @@ -424,7 +424,7 @@ Color *GetImageData(Image image) if ((image.format == UNCOMPRESSED_R32) || (image.format == UNCOMPRESSED_R32G32B32) || (image.format == UNCOMPRESSED_R32G32B32A32)) TraceLog(LOG_WARNING, "32bit pixel format converted to 8bit per channel"); - + for (int i = 0, k = 0; i < image.width*image.height; i++) { switch (image.format) @@ -500,7 +500,7 @@ Color *GetImageData(Image image) pixels[i].g = 0; pixels[i].b = 0; pixels[i].a = 255; - + } break; case UNCOMPRESSED_R32G32B32: { @@ -508,7 +508,7 @@ Color *GetImageData(Image image) pixels[i].g = (unsigned char)(((float *)image.data)[k + 1]*255.0f); pixels[i].b = (unsigned char)(((float *)image.data)[k + 2]*255.0f); pixels[i].a = 255; - + k += 3; } case UNCOMPRESSED_R32G32B32A32: @@ -517,7 +517,7 @@ Color *GetImageData(Image image) pixels[i].g = (unsigned char)(((float *)image.data)[k]*255.0f); pixels[i].b = (unsigned char)(((float *)image.data)[k]*255.0f); pixels[i].a = (unsigned char)(((float *)image.data)[k]*255.0f); - + k += 4; } default: break; @@ -532,7 +532,7 @@ Color *GetImageData(Image image) Vector4 *GetImageDataNormalized(Image image) { Vector4 *pixels = (Vector4 *)malloc(image.width*image.height*sizeof(Vector4)); - + if (image.format >= COMPRESSED_DXT1_RGB) TraceLog(LOG_WARNING, "Pixel data retrieval not supported for compressed image formats"); else { @@ -611,7 +611,7 @@ Vector4 *GetImageDataNormalized(Image image) pixels[i].y = 0.0f; pixels[i].z = 0.0f; pixels[i].w = 1.0f; - + } break; case UNCOMPRESSED_R32G32B32: { @@ -619,7 +619,7 @@ Vector4 *GetImageDataNormalized(Image image) pixels[i].y = ((float *)image.data)[k + 1]; pixels[i].z = ((float *)image.data)[k + 2]; pixels[i].w = 1.0f; - + k += 3; } case UNCOMPRESSED_R32G32B32A32: @@ -628,14 +628,14 @@ Vector4 *GetImageDataNormalized(Image image) pixels[i].y = ((float *)image.data)[k + 1]; pixels[i].z = ((float *)image.data)[k + 2]; pixels[i].w = ((float *)image.data)[k + 3]; - + k += 4; } default: break; } } } - + return pixels; } @@ -720,16 +720,16 @@ void UpdateTexture(Texture2D texture, const void *pixels) void ExportImage(Image image, const char *fileName) { int success = 0; - + // NOTE: Getting Color array as RGBA unsigned char values unsigned char *imgData = (unsigned char *)GetImageData(image); - + if (IsFileExtension(fileName, ".png")) success = stbi_write_png(fileName, image.width, image.height, 4, imgData, image.width*4); else if (IsFileExtension(fileName, ".bmp")) success = stbi_write_bmp(fileName, image.width, image.height, 4, imgData); else if (IsFileExtension(fileName, ".tga")) success = stbi_write_tga(fileName, image.width, image.height, 4, imgData); else if (IsFileExtension(fileName, ".jpg")) success = stbi_write_jpg(fileName, image.width, image.height, 4, imgData, 80); // JPG quality: between 1 and 100 else if (IsFileExtension(fileName, ".ktx")) success = SaveKTX(image, fileName); - else if (IsFileExtension(fileName, ".raw")) + else if (IsFileExtension(fileName, ".raw")) { // Export raw pixel data (without header) // NOTE: It's up to the user to track image parameters @@ -740,7 +740,7 @@ void ExportImage(Image image, const char *fileName) if (success != 0) TraceLog(LOG_INFO, "Image exported successfully: %s", fileName); else TraceLog(LOG_WARNING, "Image could not be exported."); - + free(imgData); } @@ -748,10 +748,10 @@ void ExportImage(Image image, const char *fileName) void ExportImageAsCode(Image image, const char *fileName) { #define BYTES_TEXT_PER_LINE 20 - + char varFileName[256] = { 0 }; int dataSize = GetPixelDataSize(image.width, image.height, image.format); - + FILE *txtFile = fopen(fileName, "wt"); fprintf(txtFile, "\n//////////////////////////////////////////////////////////////////////////////////////\n"); @@ -764,11 +764,11 @@ void ExportImageAsCode(Image image, const char *fileName) fprintf(txtFile, "// Copyright (c) 2018 Ramon Santamaria (@raysan5) //\n"); fprintf(txtFile, "// //\n"); fprintf(txtFile, "////////////////////////////////////////////////////////////////////////////////////////\n\n"); - + // Get file name from path and convert variable name to uppercase strcpy(varFileName, GetFileNameWithoutExt(fileName)); for (int i = 0; varFileName[i] != '\0'; i++) if (varFileName[i] >= 'a' && varFileName[i] <= 'z') { varFileName[i] = varFileName[i] - 32; } - + // Add image information fprintf(txtFile, "// Image data information\n"); fprintf(txtFile, "#define %s_WIDTH %i\n", varFileName, image.width); @@ -987,7 +987,7 @@ void ImageFormat(Image *image, int newFormat) case UNCOMPRESSED_R32: { // WARNING: Image is converted to GRAYSCALE eqeuivalent 32bit - + image->data = (float *)malloc(image->width*image->height*sizeof(float)); for (int i = 0; i < image->width*image->height; i++) @@ -1023,7 +1023,7 @@ void ImageFormat(Image *image, int newFormat) free(pixels); pixels = NULL; - + // In case original image had mipmaps, generate mipmaps for formated image // NOTE: Original mipmaps are replaced by new ones, if custom mipmaps were used, they are lost if (image->mipmaps > 1) @@ -1087,14 +1087,14 @@ void ImageAlphaMask(Image *image, Image alphaMask) void ImageAlphaClear(Image *image, Color color, float threshold) { Color *pixels = GetImageData(*image); - + for (int i = 0; i < image->width*image->height; i++) if (pixels[i].a <= (unsigned char)(threshold*255.0f)) pixels[i] = color; UnloadImage(*image); - + int prevFormat = image->format; *image = LoadImageEx(pixels, image->width, image->height); - + ImageFormat(image, prevFormat); } @@ -1102,13 +1102,13 @@ void ImageAlphaClear(Image *image, Color color, float threshold) void ImageAlphaCrop(Image *image, float threshold) { Rectangle crop = { 0 }; - + Color *pixels = GetImageData(*image); - + int minx = 0; int miny = 0; - for (int i = 0; i < image->width*image->height; i++) + for (int i = 0; i < image->width*image->height; i++) { if (pixels[i].a > (unsigned char)(threshold*255.0f)) { @@ -1127,18 +1127,18 @@ void ImageAlphaCrop(Image *image, float threshold) else if (crop.height < (float)miny) crop.height = (float)miny; } } - + crop.width -= (crop.x - 1); crop.height -= (crop.y - 1); - + TraceLog(LOG_INFO, "Crop rectangle: (%i, %i, %i, %i)", crop.x, crop.y, crop.width, crop.height); - + free(pixels); - + // NOTE: Added this weird check to avoid additional 1px crop to // image data that has already been cropped... - if ((crop.x != 1) && - (crop.y != 1) && + if ((crop.x != 1) && + (crop.y != 1) && (crop.width != image->width - 1) && (crop.height != image->height - 1)) ImageCrop(image, crop); } @@ -1148,8 +1148,8 @@ void ImageAlphaPremultiply(Image *image) { float alpha = 0.0f; Color *pixels = GetImageData(*image); - - for (int i = 0; i < image->width*image->height; i++) + + for (int i = 0; i < image->width*image->height; i++) { alpha = (float)pixels[i].a/255.0f; pixels[i].r = (unsigned char)((float)pixels[i].r*alpha); @@ -1158,10 +1158,10 @@ void ImageAlphaPremultiply(Image *image) } UnloadImage(*image); - + int prevFormat = image->format; *image = LoadImageEx(pixels, image->width, image->height); - + ImageFormat(image, prevFormat); } @@ -1282,9 +1282,9 @@ void ImageResizeCanvas(Image *image, int newWidth,int newHeight, int offsetX, in Image imTemp = GenImageColor(newWidth, newHeight, color); Rectangle srcRec = { 0.0f, 0.0f, (float)image->width, (float)image->height }; Rectangle dstRec = { (float)offsetX, (float)offsetY, (float)srcRec.width, (float)srcRec.height }; - + // TODO: Review different scaling situations - + if ((newWidth > image->width) && (newHeight > image->height)) { ImageDraw(&imTemp, *image, srcRec, dstRec); @@ -1314,7 +1314,7 @@ void ImageMipmaps(Image *image) { if (mipWidth != 1) mipWidth /= 2; if (mipHeight != 1) mipHeight /= 2; - + // Security check for NPOT textures if (mipWidth < 1) mipWidth = 1; if (mipHeight < 1) mipHeight = 1; @@ -1332,8 +1332,8 @@ void ImageMipmaps(Image *image) if (image->mipmaps < mipCount) { void *temp = realloc(image->data, mipSize); - - if (temp != NULL) + + if (temp != NULL) { image->data = temp; // Assign new pointer (new size) to store mipmaps data TraceLog(LOG_DEBUG, "Image data memory point reallocated: 0x%x", temp); @@ -1342,29 +1342,29 @@ void ImageMipmaps(Image *image) // Pointer to allocated memory point where store next mipmap level data unsigned char *nextmip = (unsigned char *)image->data + GetPixelDataSize(image->width, image->height, image->format); - + mipWidth = image->width/2; mipHeight = image->height/2; mipSize = GetPixelDataSize(mipWidth, mipHeight, image->format); Image imCopy = ImageCopy(*image); - + for (int i = 1; i < mipCount; i++) { TraceLog(LOG_DEBUG, "Gen mipmap level: %i (%i x %i) - size: %i - offset: 0x%x", i, mipWidth, mipHeight, mipSize, nextmip); - + ImageResize(&imCopy, mipWidth, mipHeight); // Uses internally Mitchell cubic downscale filter memcpy(nextmip, imCopy.data, mipSize); nextmip += mipSize; image->mipmaps++; - + mipWidth /= 2; mipHeight /= 2; - + // Security check for NPOT textures if (mipWidth < 1) mipWidth = 1; if (mipHeight < 1) mipHeight = 1; - + mipSize = GetPixelDataSize(mipWidth, mipHeight, image->format); } @@ -1487,10 +1487,10 @@ void ImageDither(Image *image, int rBpp, int gBpp, int bBpp, int aBpp) Color *ImageExtractPalette(Image image, int maxPaletteSize, int *extractCount) { #define COLOR_EQUAL(col1, col2) ((col1.r == col2.r)&&(col1.g == col2.g)&&(col1.b == col2.b)&&(col1.a == col2.a)) - + Color *pixels = GetImageData(image); Color *palette = (Color *)malloc(maxPaletteSize*sizeof(Color)); - + int palCount = 0; for (int i = 0; i < maxPaletteSize; i++) palette[i] = BLANK; // Set all colors to BLANK @@ -1499,23 +1499,23 @@ Color *ImageExtractPalette(Image image, int maxPaletteSize, int *extractCount) if (pixels[i].a > 0) { bool colorInPalette = false; - + // Check if the color is already on palette for (int j = 0; j < maxPaletteSize; j++) { - if (COLOR_EQUAL(pixels[i], palette[j])) + if (COLOR_EQUAL(pixels[i], palette[j])) { colorInPalette = true; break; } } - + // Store color if not on the palette if (!colorInPalette) { palette[palCount] = pixels[i]; // Add pixels[i] to palette palCount++; - + // We reached the limit of colors supported by palette if (palCount >= maxPaletteSize) { @@ -1527,9 +1527,9 @@ Color *ImageExtractPalette(Image image, int maxPaletteSize, int *extractCount) } free(pixels); - + *extractCount = palCount; - + return palette; } @@ -1595,7 +1595,7 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) UnloadImage(srcCopy); // Source copy not required any more... Vector4 fsrc, fdst, fout; // float based versions of pixel data - + // Blit pixels, copy source image into destination // TODO: Probably out-of-bounds blitting could be considered here instead of so much cropping... for (int j = (int)dstRec.y; j < (int)(dstRec.y + dstRec.height); j++) @@ -1603,12 +1603,12 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) for (int i = (int)dstRec.x; i < (int)(dstRec.x + dstRec.width); i++) { // Alpha blending (https://en.wikipedia.org/wiki/Alpha_compositing) - + fdst = ColorNormalize(dstPixels[j*(int)dst->width + i]); fsrc = ColorNormalize(srcPixels[(j - (int)dstRec.y)*(int)dstRec.width + (i - (int)dstRec.x)]); fout.w = fsrc.w + fdst.w*(1.0f - fsrc.w); - + if (fout.w <= 0.0f) { fout.x = 0.0f; @@ -1622,9 +1622,9 @@ void ImageDraw(Image *dst, Image src, Rectangle srcRec, Rectangle dstRec) fout.z = (fsrc.z*fsrc.w + fdst.z*fdst.w*(1 - fsrc.w))/fout.w; } - dstPixels[j*(int)dst->width + i] = (Color){ (unsigned char)(fout.x*255.0f), - (unsigned char)(fout.y*255.0f), - (unsigned char)(fout.z*255.0f), + dstPixels[j*(int)dst->width + i] = (Color){ (unsigned char)(fout.x*255.0f), + (unsigned char)(fout.y*255.0f), + (unsigned char)(fout.z*255.0f), (unsigned char)(fout.w*255.0f) }; // TODO: Support other blending options @@ -1662,16 +1662,16 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co // TODO: ISSUE: Measured text size does not seem to be correct... issue on ImageDraw() Vector2 imSize = MeasureTextEx(font, text, (float)font.baseSize, spacing); - + TraceLog(LOG_DEBUG, "Text Image size: %f, %f", imSize.x, imSize.y); // NOTE: glGetTexImage() not available in OpenGL ES - // TODO: This is horrible, retrieving font texture from GPU!!! + // TODO: This is horrible, retrieving font texture from GPU!!! // Define ImageFont struct? or include Image spritefont in Font struct? Image imFont = GetTextureData(font.texture); - + ImageFormat(&imFont, UNCOMPRESSED_R8G8B8A8); // Make sure image format could be properly colored! - + ImageColorTint(&imFont, tint); // Apply color tint to font // Create image to store text @@ -1702,10 +1702,10 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co else index = GetGlyphIndex(font, (unsigned char)text[i]); CharInfo letter = font.chars[index]; - + if ((unsigned char)text[i] != ' ') { - ImageDraw(&imText, imFont, letter.rec, (Rectangle){ (float)(posX + letter.offsetX), + ImageDraw(&imText, imFont, letter.rec, (Rectangle){ (float)(posX + letter.offsetX), (float)letter.offsetY, (float)letter.rec.width, (float)letter.rec.height }); } @@ -1734,11 +1734,11 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co void ImageDrawRectangle(Image *dst, Vector2 position, Rectangle rec, Color color) { Image imRec = GenImageColor((int)rec.width, (int)rec.height, color); - + Rectangle dstRec = { position.x, position.y, (float)imRec.width, (float)imRec.height }; ImageDraw(dst, imRec, rec, dstRec); - + UnloadImage(imRec); } @@ -2056,13 +2056,13 @@ void ImageColorReplace(Image *image, Color color, Color replace) Image GenImageColor(int width, int height, Color color) { Color *pixels = (Color *)calloc(width*height, sizeof(Color)); - + for (int i = 0; i < width*height; i++) pixels[i] = color; - + Image image = LoadImageEx(pixels, width, height); - + free(pixels); - + return image; } @@ -2120,17 +2120,17 @@ Image GenImageGradientRadial(int width, int height, float density, Color inner, float centerX = (float)width/2.0f; float centerY = (float)height/2.0f; - + for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float dist = hypotf((float)x - centerX, (float)y - centerY); float factor = (dist - radius*density)/(radius*(1.0f - density)); - + factor = (float)fmax(factor, 0.f); factor = (float)fmin(factor, 1.f); // dist can be bigger than radius so we have to check - + pixels[y*width + x].r = (int)((float)outer.r*factor + (float)inner.r*(1.0f - factor)); pixels[y*width + x].g = (int)((float)outer.g*factor + (float)inner.g*(1.0f - factor)); pixels[y*width + x].b = (int)((float)outer.b*factor + (float)inner.b*(1.0f - factor)); @@ -2192,7 +2192,7 @@ Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float { float nx = (float)(x + offsetX)*scale/(float)width; float ny = (float)(y + offsetY)*scale/(float)height; - + // Typical values to start playing with: // lacunarity = ~2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output) // gain = 0.5 -- relative weighting applied to each successive octave @@ -2200,7 +2200,7 @@ Image GenImagePerlinNoise(int width, int height, int offsetX, int offsetY, float // NOTE: We need to translate the data from [-1..1] to [0..1] float p = (stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6, 0, 0, 0) + 1.0f)/2.0f; - + int intensity = (int)(p*255.0f); pixels[y*width + x] = (Color){intensity, intensity, intensity, 255}; } @@ -2233,7 +2233,7 @@ Image GenImageCellular(int width, int height, int tileSize) for (int y = 0; y < height; y++) { int tileY = y/tileSize; - + for (int x = 0; x < width; x++) { int tileX = x/tileSize; @@ -2263,7 +2263,7 @@ Image GenImageCellular(int width, int height, int tileSize) pixels[y*width + x] = (Color){ intensity, intensity, intensity, 255 }; } } - + free(seeds); Image image = LoadImageEx(pixels, width, height); @@ -2411,7 +2411,7 @@ void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, V { float width = (float)texture.width; float height = (float)texture.height; - + if (sourceRec.width < 0) sourceRec.x -= sourceRec.width; if (sourceRec.height < 0) sourceRec.y -= sourceRec.height; @@ -2729,7 +2729,7 @@ static Image LoadDDS(const char *fileName) image.width = ddsHeader.width; image.height = ddsHeader.height; - + if (ddsHeader.mipmapCount == 0) image.mipmaps = 1; // Parameter not used else image.mipmaps = ddsHeader.mipmapCount; @@ -2946,9 +2946,9 @@ static Image LoadKTX(const char *fileName) // KTX file Header (64 bytes) // v1.1 - https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ // v2.0 - http://github.khronos.org/KTX-Specification/ - + // TODO: Support KTX 2.2 specs! - + typedef struct { char id[12]; // Identifier: "«KTX 11»\r\n\x1A\n" unsigned int endianness; // Little endian: 0x01 0x02 0x03 0x04 @@ -3028,11 +3028,11 @@ static Image LoadKTX(const char *fileName) static int SaveKTX(Image image, const char *fileName) { int success = 0; - + // KTX file Header (64 bytes) // v1.1 - https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/ // v2.0 - http://github.khronos.org/KTX-Specification/ - still on draft, not ready for implementation - + typedef struct { char id[12]; // Identifier: "«KTX 11»\r\n\x1A\n" // KTX 2.0: "«KTX 22»\r\n\x1A\n" unsigned int endianness; // Little endian: 0x01 0x02 0x03 0x04 @@ -3060,11 +3060,11 @@ static int SaveKTX(Image image, const char *fileName) else { KTXHeader ktxHeader; - + // KTX identifier (v2.2) //unsigned char id[12] = { '«', 'K', 'T', 'X', ' ', '1', '1', '»', '\r', '\n', '\x1A', '\n' }; //unsigned char id[12] = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A }; - + // Get the image header strcpy(ktxHeader.id, "«KTX 11»\r\n\x1A\n"); // KTX 1.1 signature ktxHeader.endianness = 0; @@ -3080,28 +3080,28 @@ static int SaveKTX(Image image, const char *fileName) ktxHeader.faces = 1; ktxHeader.mipmapLevels = image.mipmaps; // If it was 0, it means mipmaps should be generated on loading (not for compressed formats) ktxHeader.keyValueDataSize = 0; // No extra data after the header - + rlGetGlTextureFormats(image.format, &ktxHeader.glInternalFormat, &ktxHeader.glFormat, &ktxHeader.glType); // rlgl module function ktxHeader.glBaseInternalFormat = ktxHeader.glFormat; // KTX 1.1 only - + // NOTE: We can save into a .ktx all PixelFormats supported by raylib, including compressed formats like DXT, ETC or ASTC - + if (ktxHeader.glFormat == -1) TraceLog(LOG_WARNING, "Image format not supported for KTX export."); else { success = fwrite(&ktxHeader, sizeof(KTXHeader), 1, ktxFile); - + int width = image.width; int height = image.height; int dataOffset = 0; - + // Save all mipmaps data for (int i = 0; i < image.mipmaps; i++) { unsigned int dataSize = GetPixelDataSize(width, height, image.format); success = fwrite(&dataSize, sizeof(unsigned int), 1, ktxFile); success = fwrite((unsigned char *)image.data + dataOffset, dataSize, 1, ktxFile); - + width /= 2; height /= 2; dataOffset += dataSize; @@ -3110,7 +3110,7 @@ static int SaveKTX(Image image, const char *fileName) fclose(ktxFile); // Close file pointer } - + // If all data has been written correctly to file, success = 1 return success; } @@ -3321,7 +3321,7 @@ static Image LoadASTC(const char *fileName) TraceLog(LOG_DEBUG, "ASTC image width: %i", image.width); TraceLog(LOG_DEBUG, "ASTC image height: %i", image.height); TraceLog(LOG_DEBUG, "ASTC image blocks: %ix%i", astcHeader.blockX, astcHeader.blockY); - + image.mipmaps = 1; // NOTE: ASTC format only contains one mipmap level // NOTE: Each block is always stored in 128bit so we can calculate the bpp |
