static void handle_proc(const GBitmap& bitmap, void (*proc)(const GBitmap&), const std::string& root, const char name[]) { std::string path(root); path += name; path += ".png"; // fill the bitmap with transparent black memset(bitmap.pixels(), 0, bitmap.rowBytes() * bitmap.height()); proc(bitmap); if (!bitmap.writeToFile(path.c_str())) { fprintf(stderr, "failed to write %s\n", path.c_str()); } }
static void draw_ramp(const GBitmap& bitmap) { const float g0 = 0; const float g1 = 255; const float dg = (g1 - g0) / bitmap.width(); const float b0 = 0; const float b1 = 255; const float db = (b1 - b0) / bitmap.height(); GPixel* dst = bitmap.fPixels; float g = g0 + dg/2; for (int y = 0; y < bitmap.height(); ++y) { float b = b0 + db/2; for (int x = 0; x < bitmap.width(); ++x) { dst[x] = GPixel_PackARGB(0xFF, 0, (int)g, (int)b); b += db; } g += dg; dst = (GPixel*)((char*)dst + bitmap.rowBytes()); } }
static void draw_circle(const GBitmap& bitmap) { const GPixel px = GPixel_PackARGB(0xFF, 0xFF, 0, 0); const float cx = (float)bitmap.width() / 2; const float cy = (float)bitmap.height() / 2; const float radius = cx * 5 / 6; const float radius2 = radius * radius; GPixel* dst = bitmap.pixels(); for (int y = 0; y < bitmap.height(); ++y) { const float dy = y - cy; for (int x = 0; x < bitmap.width(); ++x) { const float dx = x - cx; const float dist2 = dx*dx + dy*dy; if (dist2 <= radius2) { dst[x] = px; } else { dst[x] = 0; // transparent } } dst = (GPixel*)((char*)dst + bitmap.rowBytes()); } }
int main(int argc, char** argv) { int N = 256; std::string root; for (int i = 1; i < argc; ++i) { if (0 == strcmp(argv[i], "--size") && i+1 < argc) { N = atoi(argv[++i]); } if (0 == strcmp(argv[i], "--write") && i+1 < argc) { root = argv[++i]; } } if (N < 1 || N > 1024) { fprintf(stderr, "specify a reasonable size for the image (e.g. 256)\n"); return -1; } if (root.size() > 0 && root[root.size() - 1] != '/') { root += "/"; if (!mk_dir(root.c_str())) { return -1; } } GBitmap bitmap; bitmap.fWidth = N; bitmap.fHeight = N; bitmap.fRowBytes = N * sizeof(GPixel); bitmap.fPixels = (GPixel*)malloc(bitmap.rowBytes() * bitmap.height()); for (int i = 0; i < GARRAY_COUNT(gRec); ++i) { handle_proc(bitmap, gRec[i].fProc, root, gRec[i].fName); } free(bitmap.fPixels); return 0; }
static void make_circle(const GBitmap& bitmap, const GColor& color) { const GPixel px = pin_and_premul_to_pixel(color); const float cx = (float)bitmap.width() / 2; const float cy = (float)bitmap.height() / 2; const float radius = cx - 1; const float radius2 = radius * radius; GPixel* dst = bitmap.pixels(); for (int y = 0; y < bitmap.height(); ++y) { const float dy = y - cy; for (int x = 0; x < bitmap.width(); ++x) { const float dx = x - cx; const float dist2 = dx*dx + dy*dy; if (dist2 <= radius2) { dst[x] = px; } else { dst[x] = 0; // transparent } } dst = (GPixel*)((char*)dst + bitmap.rowBytes()); } }
/* * The bitmap has already been sized and allocated. This function's job is to fill in the * pixels to create the custom image. See src/image.cpp for examples. */ void cs575_draw_into_bitmap(const GBitmap& bitmap) { // // Your code goes here // const float rx = 200; const float ry = 50; const float rz = (rx - ry)/bitmap.width(); const float bx = 50; const float by = 200; const float bz = (by - bx)/bitmap.width(); GPixel* dst = bitmap.fPixels; float r = rx + rz/4; for (int y = 0; y < bitmap.height(); ++y) { float b = bx + bz/4; for (int x = 0; x < bitmap.width(); ++x) { dst[x] = GPixel_PackARGB(0xFF, (int)r, 0, (int)b); b += bz; } r += rz; dst = (GPixel*)((char*)dst + bitmap.rowBytes()); } }