static struct SourceDomainPrivate *newSourceDomainPrivate() { struct SourceDomainPrivate *result = sgMalloc(sizeof(struct SourceDomainPrivate)); result->db = sgDbInit(SGDBTYPE_DOMAINLIST, NULL); return result; }
void Pixmap::create ( u32 width, u32 height, const Color* data ) { cleanupData (); m_width = width; m_height = height; m_pixels = (Color *)sgMalloc ( sizeof(Color) * m_width * m_height ); if ( data != 0 ) memcpy ( m_pixels, data, sizeof(Color)*m_width*m_height ); }
Pixmap& Pixmap::operator= ( const Pixmap& other ) { cleanupData (); m_width = other.m_width; m_height = other.m_height; memcpy ( m_error, other.m_error, sizeof(m_error) ); m_pixels = (Color *)sgMalloc ( sizeof(Color) * m_width * m_height ); memcpy ( m_pixels, other.m_pixels, sizeof(Color)*m_width*m_height ); return *this; }
bool sgGLShader::prepareShader(void) { std::string errInfo = std::string(" ") + std::string(this->getFilename().getStr()) + std::string(" sgGLShader::prepareShader"); // create sgAssert(glCreateShader , "no function glCreateShader"); mShaderId = glCreateShader(mShaderType); if(mShaderId == 0) { sgLogSystem::instance()->error(SG_CHECK_GL_ERROR() + errInfo); return false; } // compile const GLchar *s[1]; s[0] = mShaderSource.c_str(); glShaderSource(mShaderId, 1, s , 0); std::string err; err = SG_CHECK_GL_ERROR(); if(!err.empty()) { sgLogSystem::instance()->error(err + errInfo); return false; } glCompileShader(mShaderId); err = SG_CHECK_GL_ERROR(); if(!err.empty()) { sgLogSystem::instance()->error(err + errInfo); return false; } //check error GLsizei temp; glGetShaderiv(mShaderId, GL_COMPILE_STATUS, &temp); if(temp == GL_FALSE) { glGetShaderiv(mShaderId, GL_INFO_LOG_LENGTH, &temp); char *log = (char*)sgMalloc(temp); glGetShaderInfoLog(mShaderId, temp, &temp, log); sgLogSystem::instance()->error(std::string(log) + errInfo); sgFree(log); return false; } return true; }
static struct node * listadd(struct node *lh, const char *name) { struct node *lp, *lq = NULL; lp = sgMalloc(sizeof *lp); lp->n_name = sgStrdup(name); if (lh != NULL) { for (lq = lh; lq->n_next != NULL; lq = lq->n_next) ; lq->n_next = lp; } else { lh = lp; } return lh; }
bool sgGLGpuProgram::getParameters(void) { int count; int maxChar; GLint size; GLenum type; glGetProgramiv(mProgramId, GL_ACTIVE_UNIFORMS, &count); glGetProgramiv(mProgramId, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxChar); mParameterList.clear(); char *name = (char*)sgMalloc(maxChar); for(int i=0; i<count; ++i) { glGetActiveUniform(mProgramId, i, maxChar, NULL, &size, &type, name); // check is predefined uniform variant std::string strName = name; if(sgStringUtil::start_with(strName, "gl_")) { continue; } if(sgStringUtil::start_with(strName, "[0]")) { size_t index = strName.find_first_of('['); strName = strName.substr(0, index); } sgGpuParameter param; param.name = strName; param.type = sgGetRendererDataType(type); param.location = glGetUniformLocation(mProgramId, name); mParameterList.insert(std::make_pair(sgStrHandle(strName.c_str()), param)); } sgFree(name); std::string err = SG_CHECK_GL_ERROR(); if(!err.empty()) return false; else return true; }
static struct Setting *newSetting(const char *key, const char *value) { struct Setting *result = sgMalloc(sizeof(struct Setting)); result->name = sgStrdup(key); result->value = value ? sgStrdup(value) : NULL; result->defaultValue = NULL; result->cb = NULL; result->next = NULL; if (lastSetting == NULL) { firstSetting = result; lastSetting = result; } else { lastSetting->next = result; lastSetting = result; } return result; }
bool sgGLGpuProgram::getAttributes(void) { int count; int maxChar; GLint size; GLenum type; glGetProgramiv(mProgramId, GL_ACTIVE_ATTRIBUTES, &count); glGetProgramiv(mProgramId, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &maxChar); mAttributeList.clear(); char *name = (char*)sgMalloc(maxChar); for(int i=0; i<count; ++i) { glGetActiveAttrib(mProgramId, i, maxChar, NULL, &size, &type, name); // check is predefined attribute variant std::string strName = name; if(sgStringUtil::start_with(strName, "gl_")) { continue; } sgGpuAttribute attr; attr.name = strName; attr.type = sgGetRendererDataType(type); attr.location = glGetAttribLocation(mProgramId, name); mAttributeList.insert(std::make_pair(sgStrHandle(strName.c_str()), attr)); } sgFree(name); std::string err = SG_CHECK_GL_ERROR(); if(!err.empty()) return false; else return true; }
char *substRedirect(const struct SquidInfo *req, const char *redirect, const char *srcClass, const char *destClass) { char *result = NULL; const char *p = NULL; char *t; size_t rlen = strlen(redirect); //while ((p = strchr(p, '%')) != NULL) { for (p = strchr(redirect, '%'); p; p = strchr(p, '%')) { const char *np = p + 1; switch (*np) { case 'a': // Source Address rlen += strlen(req->src); break; case 'i': // Source User Ident rlen += strlen(req->ident); break; case 'n': // Source Domain Name rlen += strlen(req->srcDomain); break; case 'p': // The url path (??) break; case 'f': // The url file (??) break; case 's': // Target Class Matched rlen += (srcClass ? strlen(srcClass) : 1); break; case 't': // Target Class Matched rlen += (destClass ? strlen(destClass) : 1); break; case 'u': // Target URL rlen += strlen(req->orig); break; default: // %% and unknown c rlen++; break; } p++; } result = sgMalloc(rlen + 1); *result = 0; t = result; for (p = redirect; *p; p++) { const char *np = p + 1; if (*p == '%') { switch (*np) { case 'a': // Source Address t += sprintf(t, "%s", req->src); break; case 'i': // Source User Ident t += sprintf(t, "%s", req->ident); break; case 'n': // Source Domain Name t += sprintf(t, "%s", req->srcDomain); break; case 'p': // The url path ?? break; case 'f': // The url file (??) break; case 's': // Target Class Matched t += sprintf(t, "%s", (srcClass ? srcClass : "-")); break; case 't': // Target Class Matched t += sprintf(t, "%s", (destClass ? destClass : "-")); break; case 'u': // Target URL t += sprintf(t, "%s", req->orig); break; default: *t = *np; t++; break; } p++; } else { *t = *p; t++; } } *t = 0; return result; }
bool sgGLGpuProgram::prepareProgram(void) { if(mProgramId == 0) return false; std::string errInfo = " sgGLGpuProgram::prepareProgram"; sgShader *vs = this->getVertexShader(); if(!vs->isActive() || vs->getShaderType() != GL_VERTEX_SHADER) { sgLogSystem::instance()->error(std::string("No vertex shader") + errInfo); return false; } sgShader *fs = this->getFragmentShader(); if(!fs->isActive() || fs->getShaderType() != GL_FRAGMENT_SHADER) { sgLogSystem::instance()->error(std::string("No fragment shader") + errInfo); return false; } glAttachShader(mProgramId, vs->getShaderId()); std::string err = SG_CHECK_GL_ERROR(); if(!err.empty()) { sgLogSystem::instance()->error(err + errInfo); return false; } glAttachShader(mProgramId, fs->getShaderId()); err = SG_CHECK_GL_ERROR(); if(!err.empty()) { sgLogSystem::instance()->error(err + errInfo); return false; } glLinkProgram(mProgramId); err = SG_CHECK_GL_ERROR(); if(!err.empty()) { sgLogSystem::instance()->error(err); return false; } //check error GLsizei temp; glGetProgramiv(mProgramId, GL_LINK_STATUS, &temp); if(temp == GL_FALSE) { glGetProgramiv(mProgramId, GL_INFO_LOG_LENGTH, &temp); char *log = (char*)sgMalloc(temp); glGetProgramInfoLog(mProgramId, temp, &temp, log); sgFree(log); sgLogSystem::instance()->error(std::string(log) + errInfo); return false; } if(!getParameters() || !getAttributes()) return false; return true; }
void *sgMemObject::operator new(size_t sz) { void *ptr = sgMalloc(sz); if(!ptr) throw std::bad_alloc(); return ptr; }
void Pixmap::resample_Y ( u32 newHeight ) { Color* newPixels = (Color *)sgMalloc ( sizeof(Color) * m_width * newHeight ); if ( newHeight < m_height ) { float ratio = m_height / (float)newHeight; for ( u32 w = 0; w < m_width; ++w ) { u32 h = 0; for ( float y = 0.0f; h < newHeight; y += ratio, ++h ) { float from = y; float to = min((float)m_height, y + ratio); u32 absoluteValue = (u32)floor(from); float fractionaryPart = from-absoluteValue; float pixelAccum [ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f }; if ( fractionaryPart > 0.000001f ) { float factor = 1.0f - fractionaryPart; Color& color = m_pixels [ m_width*absoluteValue + w ]; pixelAccum[0] += ( color.r() / 255.0f ) * factor; pixelAccum[1] += ( color.g() / 255.0f ) * factor; pixelAccum[2] += ( color.b() / 255.0f ) * factor; pixelAccum[3] += ( color.a() / 255.0f ) * factor; from = absoluteValue + 1; } from = floor(from); while ( to-from > 1.0f ) { Color& color = m_pixels [ m_width*(u32)from + w ]; pixelAccum[0] += color.r() / 255.0f; pixelAccum[1] += color.g() / 255.0f; pixelAccum[2] += color.b() / 255.0f; pixelAccum[3] += color.a() / 255.0f; ++from; } if ( fabs(to-from) > 0.000001f ) { float factor = fabs(to-from); Color& color = m_pixels [ m_width*(u32)from + w ]; pixelAccum[0] += (color.r() / 255.0f) * factor; pixelAccum[1] += (color.g() / 255.0f) * factor; pixelAccum[2] += (color.b() / 255.0f) * factor; pixelAccum[3] += (color.a() / 255.0f) * factor; } newPixels [ m_width*h + w ] = Color ( pixelAccum[0] / ratio * 255.0f, pixelAccum[1] / ratio * 255.0f, pixelAccum[2] / ratio * 255.0f, pixelAccum[3] / ratio * 255.0f ); } } } else if ( newHeight > m_height ) { float ratio = m_height / (float)newHeight; for ( u32 w = 0; w < m_width; ++w ) { for ( u32 h = 0; h < newHeight; ++h ) { u32 up = (u32)floor(h * ratio); u32 down = min(m_height-1, up+1); u32 upPrev = up > 0 ? up-1 : 0; u32 downNext = min(m_height-1, down+1); Color& u = m_pixels [ up*m_width + w ]; Color& d = m_pixels [ down*m_width + w ]; Color& u_ = m_pixels [ upPrev*m_width + w ]; Color& dn = m_pixels [ downNext*m_width + w ]; float colorValues [ 4 ][ 4 ] = { { u.r() / 255.0f, u.g() / 255.0f, u.b() / 255.0f, u.a() / 255.0f }, { d.r() / 255.0f, d.g() / 255.0f, d.b() / 255.0f, d.a() / 255.0f }, { u_.r() / 255.0f, u_.g() / 255.0f, u_.b() / 255.0f, u_.a() / 255.0f }, { dn.r() / 255.0f, dn.g() / 255.0f, dn.b() / 255.0f, dn.a() / 255.0f } }; float tangents [ 2 ][ 4 ] = { { colorValues[1][0]-colorValues[2][0], colorValues[1][1]-colorValues[2][1], colorValues[1][2]-colorValues[2][2], colorValues[1][3]-colorValues[2][3] }, { colorValues[3][0]-colorValues[0][0], colorValues[3][1]-colorValues[0][1], colorValues[3][2]-colorValues[0][2], colorValues[3][3]-colorValues[0][3] } }; float interpolatedValues [ 4 ]; float alpha = h*ratio; alpha = alpha - floor(alpha); interpolatedValues[0] = clamp(0.0f, cubic_interpolate(colorValues[0][0], tangents[0][0], alpha, colorValues[1][0], tangents[1][0]), 1.0f); interpolatedValues[1] = clamp(0.0f, cubic_interpolate(colorValues[0][1], tangents[0][1], alpha, colorValues[1][1], tangents[1][1]), 1.0f); interpolatedValues[2] = clamp(0.0f, cubic_interpolate(colorValues[0][2], tangents[0][2], alpha, colorValues[1][2], tangents[1][2]), 1.0f); interpolatedValues[3] = clamp(0.0f, cubic_interpolate(colorValues[0][3], tangents[0][3], alpha, colorValues[1][3], tangents[1][3]), 1.0f); newPixels [ h*m_width + w ] = Color ( interpolatedValues[0] * 255.0f, interpolatedValues[1] * 255.0f, interpolatedValues[2] * 255.0f, interpolatedValues[3] * 255.0f ); } } } sgFree ( m_pixels ); m_pixels = newPixels; m_height = newHeight; }
void Pixmap::resample_X ( u32 newWidth ) { Color* newPixels = (Color *)sgMalloc ( sizeof(Color) * newWidth * m_height ); if ( newWidth < m_width ) { float ratio = m_width / (float)newWidth; for ( u32 h = 0; h < m_height; ++h ) { u32 w = 0; for ( float x = 0.0f; w < newWidth; x += ratio, ++w ) { float from = x; float to = min((float)m_width, x + ratio); u32 absoluteValue = (u32)floor(from); float fractionaryPart = from-absoluteValue; float pixelAccum [ 4 ] = { 0.0f, 0.0f, 0.0f, 0.0f }; if ( fractionaryPart > 0.000001f ) { float factor = 1.0f - fractionaryPart; Color& color = m_pixels [ m_width*h + absoluteValue ]; pixelAccum[0] += ( color.r() / 255.0f ) * factor; pixelAccum[1] += ( color.g() / 255.0f ) * factor; pixelAccum[2] += ( color.b() / 255.0f ) * factor; pixelAccum[3] += ( color.a() / 255.0f ) * factor; from = absoluteValue + 1; } from = floor(from); while ( to-from > 1.0f ) { Color& color = m_pixels [ m_width*h + (u32)from ]; pixelAccum[0] += color.r() / 255.0f; pixelAccum[1] += color.g() / 255.0f; pixelAccum[2] += color.b() / 255.0f; pixelAccum[3] += color.a() / 255.0f; ++from; } if ( fabs(to-from) > 0.000001f ) { float factor = fabs(to-from); Color& color = m_pixels [ m_width*h + (u32)from ]; pixelAccum[0] += (color.r() / 255.0f) * factor; pixelAccum[1] += (color.g() / 255.0f) * factor; pixelAccum[2] += (color.b() / 255.0f) * factor; pixelAccum[3] += (color.a() / 255.0f) * factor; } newPixels [ newWidth*h + w ] = Color ( pixelAccum[0] / ratio * 255.0f, pixelAccum[1] / ratio * 255.0f, pixelAccum[2] / ratio * 255.0f, pixelAccum[3] / ratio * 255.0f ); } } } else if ( newWidth > m_width ) { float ratio = m_width / (float)newWidth; for ( u32 h = 0; h < m_height; ++h ) { for ( u32 w = 0; w < newWidth; ++w ) { u32 left = (u32)floor(w * ratio); u32 right = min(m_width-1, left+1); u32 leftPrev = left > 0 ? left-1 : 0; u32 rightNext = min(m_width-1, right+1); Color& l = m_pixels [ h*m_width + left ]; Color& r = m_pixels [ h*m_width + right ]; Color& lp = m_pixels [ h*m_width + leftPrev ]; Color& rn = m_pixels [ h*m_width + rightNext ]; float colorValues [ 4 ][ 4 ] = { { l.r() / 255.0f, l.g() / 255.0f, l.b() / 255.0f, l.a() / 255.0f }, { r.r() / 255.0f, r.g() / 255.0f, r.b() / 255.0f, r.a() / 255.0f }, { lp.r() / 255.0f, lp.g() / 255.0f, lp.b() / 255.0f, lp.a() / 255.0f }, { rn.r() / 255.0f, rn.g() / 255.0f, rn.b() / 255.0f, rn.a() / 255.0f } }; float tangents [ 2 ][ 4 ] = { { colorValues[1][0]-colorValues[2][0], colorValues[1][1]-colorValues[2][1], colorValues[1][2]-colorValues[2][2], colorValues[1][3]-colorValues[2][3] }, { colorValues[3][0]-colorValues[0][0], colorValues[3][1]-colorValues[0][1], colorValues[3][2]-colorValues[0][2], colorValues[3][3]-colorValues[0][3] } }; float interpolatedValues [ 4 ]; float alpha = w*ratio; alpha = alpha - floor(alpha); interpolatedValues[0] = clamp(0.0f, cubic_interpolate(colorValues[0][0], tangents[0][0], alpha, colorValues[1][0], tangents[1][0]), 1.0f); interpolatedValues[1] = clamp(0.0f, cubic_interpolate(colorValues[0][1], tangents[0][1], alpha, colorValues[1][1], tangents[1][1]), 1.0f); interpolatedValues[2] = clamp(0.0f, cubic_interpolate(colorValues[0][2], tangents[0][2], alpha, colorValues[1][2], tangents[1][2]), 1.0f); interpolatedValues[3] = clamp(0.0f, cubic_interpolate(colorValues[0][3], tangents[0][3], alpha, colorValues[1][3], tangents[1][3]), 1.0f); newPixels [ h*newWidth + w ] = Color ( interpolatedValues[0] * 255.0f, interpolatedValues[1] * 255.0f, interpolatedValues[2] * 255.0f, interpolatedValues[3] * 255.0f ); } } } sgFree ( m_pixels ); m_pixels = newPixels; m_width = newWidth; }
bool Pixmap::loadPNG ( std::istream& stream ) { png_structp png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0 ); if ( !png_ptr ) return SetError ( "Unable to create the PNG context" ); png_infop info_ptr = png_create_info_struct(png_ptr); if ( !info_ptr ) { png_destroy_read_struct(&png_ptr, 0, 0); return SetError ( "Unable to create the PNG info struct" ); } png_infop end_info = png_create_info_struct(png_ptr); if ( !end_info ) { png_destroy_read_struct(&png_ptr, &info_ptr, 0 ); return SetError ( "Unable to create the PNG end info struct" ); } if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); return SetError ( "An error occured while reading the PNG file" ); } png_set_sig_bytes ( png_ptr, 8 ); png_set_read_fn ( png_ptr, &stream, read_from_png ); png_read_png ( png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_GRAY_TO_RGB, 0 ); //m_width = (u32)info_ptr->width; //m_height = (u32)info_ptr->height; int bit_depth; int color_type; png_get_IHDR(png_ptr, info_ptr, (png_uint_32*)&m_width, (png_uint_32*)&m_height, &bit_depth, &color_type, 0, 0, 0); m_pixels = (Color *)sgMalloc ( sizeof(Color) * m_width * m_height ); u32* pixels = (u32 *)m_pixels; png_bytep* row_pointers = png_get_rows( png_ptr, info_ptr ); if ( color_type == PNG_COLOR_TYPE_RGB ) { for ( u32 h = 0; h < m_height; ++h ) { png_bytep row = row_pointers[h]; for ( u32 w = 0; w < m_width; ++w ) { pixels [ w + h*m_width ] = *(u32 *)&row[0] | 0xFF000000; row += 3; } } } else if ( color_type == PNG_COLOR_TYPE_RGB_ALPHA ) { u32* pixels = (u32 *)&m_pixels[0]; for ( u32 h = 0; h < m_height; ++h ) { png_bytep row = row_pointers[h]; memcpy ( &pixels[h*m_width], &row[0], 4*m_width ); } } png_destroy_read_struct(&png_ptr, &info_ptr, &end_info ); return true; }