Ejemplo n.º 1
0
// random proportion sizes between 1 and 10
glm::vec2 Rectangle::getRandomProportion()
{
	int x = static_cast<int>(round((std::rand() * 1.0 / RAND_MAX) * 10 + 1));
	int y = static_cast<int>(round((std::rand() * 1.0 / RAND_MAX) * 10 + 1));
	// TODO: change this lines
	glm::vec2 proportion (x, y);
	// glm::vec2 proportion (1, 1);
	// std::cout << "proportion: " << proportion.x << " " << proportion.y << std::endl;
	return proportion;
}
Ejemplo n.º 2
0
/**
 * @brief convert convert image function
 *
 * @param im the image
 * @param req the zimg request
 *
 * @return 1 for OK and -1 for fail
 */
int convert(MagickWand *im, zimg_req_t *req)
{
    int result = 1, ret = -1;

    MagickResetIterator(im);
    MagickSetImageOrientation(im, TopLeftOrientation);

    int x = req->x, y = req->y, cols = req->width, rows = req->height;
    if (!(cols == 0 && rows == 0)) {
        /* crop and scale */
        if (x == -1 && y == -1) {
            LOG_PRINT(LOG_DEBUG, "proportion(im, %d, %d, %d)", req->proportion, cols, rows);
            ret = proportion(im, req->proportion, cols, rows);
            if (ret != MagickTrue) return -1;
        } else {
            LOG_PRINT(LOG_DEBUG, "crop(im, %d, %d, %d, %d)", x, y, cols, rows);
            ret = crop(im, x, y, cols, rows);
            if (ret != MagickTrue) return -1;
        }
    }

    /* rotate image */
    if (req->rotate != 0) {
        LOG_PRINT(LOG_DEBUG, "wi_rotate(im, %d)", req->rotate);
        PixelWand *background = NewPixelWand();
        if (background == NULL) return -1;
        ret = PixelSetColor(background, "white");
        if (ret != MagickTrue) {
            DestroyPixelWand(background);
            return -1;
        }
        ret = MagickRotateImage(im, background, req->rotate);
        LOG_PRINT(LOG_DEBUG, "rotate() ret = %d", ret);
        DestroyPixelWand(background);
        if (ret != MagickTrue) return -1;
    }

    /* set gray */
    if (req->gray == 1) {
        LOG_PRINT(LOG_DEBUG, "wi_gray(im)");
        //several ways to grayscale an image:
        //ret = MagickSetImageColorspace(im, GRAYColorspace);
        //ret = MagickQuantizeImage(im, 256, GRAYColorspace, 0, MagickFalse, MagickFalse);
        //ret = MagickSeparateImageChannel(im, GrayChannel);
        ret = MagickSetImageType(im, GrayscaleType);
        LOG_PRINT(LOG_DEBUG, "gray() ret = %d", ret);
        if (ret != MagickTrue) return -1;
    }

	/* set quality */
    /*
    int quality = 100;
    int im_quality = MagickGetImageCompressionQuality(im);
    im_quality = (im_quality == 0 ? 100 : im_quality);
    LOG_PRINT(LOG_DEBUG, "wi_quality = %d", im_quality);
    quality = req->quality < im_quality ? req->quality : im_quality;
    */
    LOG_PRINT(LOG_DEBUG, "wi_set_quality(im, %d)", req->quality);
    ret = MagickSetImageCompressionQuality(im, req->quality);
    if (ret != MagickTrue) return -1;

	/* set format */
    if (strncmp(req->fmt, "none", 4) != 0) {
        LOG_PRINT(LOG_DEBUG, "wi_set_format(im, %s)", req->fmt);
        ret = MagickSetImageFormat(im, req->fmt);
        if (ret != MagickTrue) return -1;
    }

    LOG_PRINT(LOG_DEBUG, "convert(im, req) %d", result);
	return result;
}
Ejemplo n.º 3
0
STDMETHODIMP C_Branches::put_YAverage(long newVal)
{
	C_STLException::install();

	try {

		if (!m_lOffsets) {

			unsigned int cnt = size();

			if (cnt > 0) {

				m_lOffsets = new long[cnt];
				::memset(m_lOffsets, 0, sizeof(long) * cnt);
			}
		}

		BOOL change = (m_lAverage != newVal);

		//if (m_lAverage != newVal) {

			int offset = newVal - m_lAverage;
			m_lAverage = newVal;

			iterator it = begin();
			int l = 0;

			while (it != end()) {

				C_BranchPtr branch((*it++).second);

				if (branch) {

					C_PositionPtr position(branch);
					C_ProportionalPtr proportion(branch);
					C_LinkPtr link(branch);
					C_ConstrainsParentPtr constrains;

					if (link) {

						I_MapBlock *block = NULL;

						link->get_To(&block);
						
						//constrains = block;

						if (block) {
							block->Release();
						}
					}

					if (!constrains) {

						BOOL switchsign = FALSE;

						if (change) {

							if (proportion) {
								proportion->LogProportions();	
							}

							if (position) {

								static C_Area area;
									
								area = position.GetPoints();

								POINT3D reference;
								position->get_ReferencePoint(&reference);

								area.Offset(-reference.x, -reference.y);

								area.GetRawData()[area.GetLength() - 1].y = m_lAverage + m_lOffsets[l];

								position->put_Points(&area.GetData());
							}

							if (proportion) {
								proportion->ApplyYProportions();	
							}
						}

						if (link) {

							I_MapBlock *block = NULL;
							link->get_To(&block);

							C_NodePtr node(block);

							if (block) {
								block->Release();
							}

							if (node) {

								I_Branches *branches = NULL;
								node->get_Branches(&branches);

								if (branches) {

									long l;

									//Call recursively so that all sub-branches log their proportions
									branches->get_YAverage(&l);
									branches->put_YAverage(l);
									branches->Release();
								}
							}
						}
					}
				}

				l++;
			}
		//}
	}
	catch (C_STLNonStackException const &exception) {
		exception.Log(_T("Exception in C_Branches::put_YAverage"));
	}

	return S_OK;
}
Ejemplo n.º 4
0
STDMETHODIMP C_Branches::get_YAverage(long *pVal)
{
	if (pVal == NULL)
		return E_POINTER;
		
	C_STLException::install();

	try {

		unsigned int cnt = size();

		long total = 0;

		if (m_lOffsets) {
		
			delete [] m_lOffsets;
			m_lOffsets = NULL;
		}

		if (m_lChildAverages) {
		
			delete [] m_lChildAverages;
			m_lChildAverages = NULL;
		}

		if (cnt > 0) {

			m_lOffsets = new long[cnt];
			::memset(m_lOffsets, 0, sizeof(long) * cnt);

			m_lChildAverages = new long[cnt];
			::memset(m_lChildAverages, 0, sizeof(long) * cnt);
		}

		if (cnt > 0) {

			iterator it = begin();
			long l = 0;

			while (it != end()) {

				C_BranchPtr branch((*it++).second);

				if (branch) {

					C_PositionPtr position(branch);
					C_ProportionalPtr proportion(branch);
					C_LinkPtr link(branch);

					if (position) {

						static C_Area area;
							
						area = position.GetPoints();

						POINT3D reference = { 0, 0, 0 };

						C_DrawingStrategyPtr strategy(branch);

						if (strategy) {
							
							I_MapItem *parent = NULL;
							strategy->get_Parent(&parent);

							C_PositionPtr pp(parent);

							if (parent) {
								parent->Release();
							}

							if (pp) {
								pp->get_ReferencePoint(&reference);
							}
						}

						if (area.GetLength() > 0) {
						
							m_lOffsets[l] = area.GetRawData()[area.GetLength() - 1].y - reference.y;
							total += m_lOffsets[l];
						}
					}

					if (link) {

						I_MapBlock *block = NULL;
						link->get_To(&block);

						C_NodePtr node(block);

						if (block) {
							block->Release();
						}

						if (node) {

							I_Branches *branches = NULL;
							node->get_Branches(&branches);

							if (branches) {

								//Call recursively so that all sub-branches log their proportions
								branches->get_YAverage(m_lChildAverages + l);
								branches->Release();
							}
						}
					}
				}

				l++;
			}

			m_lAverage = total / static_cast<int>(cnt);

			for (l = 0; l < cnt; l++) {
				m_lOffsets[l] -= m_lAverage;
			}
		}
		else {
			m_lAverage = 0;
		}

		*pVal = m_lAverage;
	}
	catch (C_STLNonStackException const &exception) {
		exception.Log(_T("Exception in C_Branches::get_YAverage"));
	}

	return S_OK;
}