// int rbd_get_overlap(rbd_image_t image, uint64_t *overlap);
482598
func (image *Image) GetOverlap() (overlap uint64, err error) {
	if image.image == nil {
		return 0, RbdErrorImageNotOpen
	}

	ret := C.rbd_get_overlap(image.image, (*C.uint64_t)(&overlap))
	if ret < 0 {
		return overlap, RBDError(int(ret))
	}

	return overlap, nil
}
174654
func (i *ImageObj) getRect() (*Rect, error) {

	i.rawImgReader.Seek(0, 0)
	m, _, err := image.Decode(i.rawImgReader)
	if err != nil {
		return nil, err
	}

	imageRect := m.Bounds()
	k := 1
	w := -128 //init
	h := -128 //init
	if w < 0 {
		w = -imageRect.Dx() * 72 / w / k
	}
	if h < 0 {
		h = -imageRect.Dy() * 72 / h / k
	}
	if w == 0 {
		w = h * imageRect.Dx() / imageRect.Dy()
	}
	if h == 0 {
		h = w * imageRect.Dy() / imageRect.Dx()
	}

	var rect = new(Rect)
	rect.H = float64(h)
	rect.W = float64(w)

	return rect, nil
}
1226431
func (r Rule) overlaps(o Rule) bool {
	if !(r.Layer == o.Layer || o.Layer == "") {
		return false
	}
	if !(r.Attachment == o.Attachment || o.Attachment == "") {
		return false
	}
	// if !(r.Class == o.Class || o.Class == "") {
	// 	return false
	// }
	if !(r.Zoom.combine(o.Zoom).Levels() > 0 || r.Zoom == o.Zoom) {
		return false
	}
	if !filterOverlap(o.Filters, r.Filters) {
		return false
	}
	return true
}
19242
func GetRegistryImage(conn client.Connection, id string) (*registry.Image, error) {
	rimagepath := path.Join(zkregistrytags, id)
	var node RegistryImageNode
	if err := conn.Get(rimagepath, &node); err != nil {
		return nil, err
	}
	return &node.Image, nil
}
947959
func (term Range) Overlaps(other Range) bool {
	return !term.Intersection(other).IsEmpty()
}
769758
func (t *TSMReader) OverlapsKeyPrefixRange(min, max []byte) bool {
	return t.index.OverlapsKeyPrefixRange(min, max)
}
955900
func GetDisplayUsableBounds(displayIndex int) (rect Rect, err error) {
	err = errorFromInt(int(
		C.SDL_GetDisplayUsableBounds(C.int(displayIndex), rect.cptr())))
	return
}
1176419
func (r *Session) GetRemoteImageLayer(imgID, registry string, imgSize int64) (io.ReadCloser, error) {
	var (
		statusCode = 0
		res        *http.Response
		err        error
		imageURL   = fmt.Sprintf("%simages/%s/layer", registry, imgID)
	)

	req, err := http.NewRequest("GET", imageURL, nil)
	if err != nil {
		return nil, fmt.Errorf("Error while getting from the server: %v", err)
	}

	res, err = r.client.Do(req)
	if err != nil {
		logrus.Debugf("Error contacting registry %s: %v", registry, err)
		// the only case err != nil && res != nil is https://golang.org/src/net/http/client.go#L515
		if res != nil {
			if res.Body != nil {
				res.Body.Close()
			}
			statusCode = res.StatusCode
		}
		return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
			statusCode, imgID)
	}

	if res.StatusCode != 200 {
		res.Body.Close()
		return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
			res.StatusCode, imgID)
	}

	if res.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 {
		logrus.Debug("server supports resume")
		return resumable.NewRequestReaderWithInitialResponse(r.client, req, 5, imgSize, res), nil
	}
	logrus.Debug("server doesn't support resume")
	return res.Body, nil
}
540115
func (f *Facade) CheckRemoveRegistryImage(ctx datastore.Context, imageID string) error {
	if len(imageID) > 0 {
		logger := log.WithField("imageid", imageID)
		if count, err := f.serviceStore.GetServiceCountByImage(ctx, imageID); err != nil {
			logger.WithError(err).Error("Error getting service count by imageID")
			return err
		} else if count == 0 {
			logger.Info("Removing image from the image registry")
			if err := f.registryStore.Delete(ctx, imageID); err != nil {
				logger.WithError(err).Error("Unable to remove unused image from the image registry")
				return err
			}
		}
	}
	return nil
}
125035
func getRawShot(c *gin.Context) {
	data, err := snapPicture()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
	}
	if http.DetectContentType(data) == "image/jpeg" { //Check that we have some actual image data
		c.Data(200, "image/jpeg", data)
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": "Unkown File type",
		})
	}
}
1336111
func GetImage(imageName string, includeLayers bool, cacheDir string) (Image, error) {
	logrus.Infof("retrieving image: %s", imageName)
	var img v1.Image
	var err error
	if IsTar(imageName) {
		start := time.Now()
		img, err = tarball.ImageFromPath(imageName, nil)
		if err != nil {
			return Image{}, errors.Wrap(err, "retrieving tar from path")
		}
		elapsed := time.Now().Sub(start)
		logrus.Infof("retrieving image ref from tar took %f seconds", elapsed.Seconds())
	} else if strings.HasPrefix(imageName, daemonPrefix) {
		// remove the daemon prefix
		imageName = strings.Replace(imageName, daemonPrefix, "", -1)

		ref, err := name.ParseReference(imageName, name.WeakValidation)
		if err != nil {
			return Image{}, errors.Wrap(err, "parsing image reference")
		}

		start := time.Now()
		// TODO(nkubala): specify gzip.NoCompression here when functional options are supported
		img, err = daemon.Image(ref, daemon.WithBufferedOpener())
		if err != nil {
			return Image{}, errors.Wrap(err, "retrieving image from daemon")
		}
		elapsed := time.Now().Sub(start)
		logrus.Infof("retrieving local image ref took %f seconds", elapsed.Seconds())
	} else {
		// either has remote prefix or has no prefix, in which case we force remote
		imageName = strings.Replace(imageName, remotePrefix, "", -1)
		ref, err := name.ParseReference(imageName, name.WeakValidation)
		if err != nil {
			return Image{}, errors.Wrap(err, "parsing image reference")
		}
		auth, err := authn.DefaultKeychain.Resolve(ref.Context().Registry)
		if err != nil {
			return Image{}, errors.Wrap(err, "resolving auth")
		}
		start := time.Now()
		img, err = remote.Image(ref, remote.WithAuth(auth), remote.WithTransport(http.DefaultTransport))
		if err != nil {
			return Image{}, errors.Wrap(err, "retrieving remote image")
		}
		elapsed := time.Now().Sub(start)
		logrus.Infof("retrieving remote image ref took %f seconds", elapsed.Seconds())
	}

	// create tempdir and extract fs into it
	var layers []Layer
	if includeLayers {
		start := time.Now()
		imgLayers, err := img.Layers()
		if err != nil {
			return Image{}, errors.Wrap(err, "getting image layers")
		}
		for _, layer := range imgLayers {
			layerStart := time.Now()
			digest, err := layer.Digest()
			path, err := getExtractPathForName(digest.String(), cacheDir)
			if err != nil {
				return Image{
					Layers: layers,
				}, errors.Wrap(err, "getting extract path for layer")
			}
			if err := GetFileSystemForLayer(layer, path, nil); err != nil {
				return Image{
					Layers: layers,
				}, errors.Wrap(err, "getting filesystem for layer")
			}
			layers = append(layers, Layer{
				FSPath: path,
				Digest: digest,
			})
			elapsed := time.Now().Sub(layerStart)
			logrus.Infof("time elapsed retrieving layer: %fs", elapsed.Seconds())
		}
		elapsed := time.Now().Sub(start)
		logrus.Infof("time elapsed retrieving image layers: %fs", elapsed.Seconds())
	}

	imageDigest, err := getImageDigest(img)
	if err != nil {
		return Image{}, err
	}
	path, err := getExtractPathForName(RemoveTag(imageName)+"@"+imageDigest.String(), cacheDir)
	if err != nil {
		return Image{}, err
	}
	// extract fs into provided dir
	if err := GetFileSystemForImage(img, path, nil); err != nil {
		return Image{
			FSPath: path,
			Layers: layers,
		}, errors.Wrap(err, "getting filesystem for image")
	}
	return Image{
		Image:  img,
		Source: imageName,
		FSPath: path,
		Digest: imageDigest,
		Layers: layers,
	}, nil
}
449378
func (i *ImageObj) GetRect() *Rect {

	rect, err := i.getRect()
	if err != nil {
		log.Fatalf("%+v", err)
	}
	return rect
}
1301913
func (image *Image) GetSize() (size uint64, err error) {
	if image.image == nil {
		return 0, RbdErrorImageNotOpen
	}

	if ret := C.rbd_get_size(image.image, (*C.uint64_t)(&size)); ret < 0 {
		return 0, RBDError(ret)
	}

	return size, nil
}
851737
func (t *tFile) overlaps(icmp *iComparer, umin, umax []byte) bool {
	return !t.after(icmp, umin) && !t.before(icmp, umax)
}
1132107
func blend(bg image.Image, fg image.Image, fn func(fcolor.RGBAF64, fcolor.RGBAF64) fcolor.RGBAF64) *image.RGBA {
	bgBounds := bg.Bounds()
	fgBounds := fg.Bounds()

	var w, h int
	if bgBounds.Dx() < fgBounds.Dx() {
		w = bgBounds.Dx()
	} else {
		w = fgBounds.Dx()
	}
	if bgBounds.Dy() < fgBounds.Dy() {
		h = bgBounds.Dy()
	} else {
		h = fgBounds.Dy()
	}

	bgSrc := clone.AsRGBA(bg)
	fgSrc := clone.AsRGBA(fg)
	dst := image.NewRGBA(image.Rect(0, 0, w, h))

	parallel.Line(h, func(start, end int) {
		for y := start; y < end; y++ {
			for x := 0; x < w; x++ {
				bgPos := y*bgSrc.Stride + x*4
				fgPos := y*fgSrc.Stride + x*4
				result := fn(
					fcolor.NewRGBAF64(bgSrc.Pix[bgPos+0], bgSrc.Pix[bgPos+1], bgSrc.Pix[bgPos+2], bgSrc.Pix[bgPos+3]),
					fcolor.NewRGBAF64(fgSrc.Pix[fgPos+0], fgSrc.Pix[fgPos+1], fgSrc.Pix[fgPos+2], fgSrc.Pix[fgPos+3]))

				result.Clamp()
				dstPos := y*dst.Stride + x*4
				dst.Pix[dstPos+0] = uint8(result.R * 255)
				dst.Pix[dstPos+1] = uint8(result.G * 255)
				dst.Pix[dstPos+2] = uint8(result.B * 255)
				dst.Pix[dstPos+3] = uint8(result.A * 255)
			}

		}
	})

	return dst
}
437102
func intersectBitmapRun(a, b *Container) *Container {
	statsHit("intersect/BitmapRun")
	var output *Container
	runs := b.runs()
	if b.n <= ArrayMaxSize || a.n <= ArrayMaxSize {
		// output is array container
		array := make([]uint16, 0, b.n)
		for _, iv := range runs {
			for i := iv.start; i <= iv.last; i++ {
				if a.bitmapContains(i) {
					array = append(array, i)
				}
				// If the run ends the container, break to avoid an infinite loop.
				if i == 65535 {
					break
				}
			}
		}

		output = NewContainerArray(array)
	} else {
		// right now this iterates through the runs and sets integers in the
		// bitmap that are in the runs. alternately, we could zero out ranges in
		// the bitmap which are between runs.
		output = NewContainerBitmap(0, nil)
		bitmap := output.bitmap()
		aBitmap := a.bitmap()
		for j := 0; j < len(runs); j++ {
			vb := runs[j]
			i := vb.start >> 6 // index into a
			vastart := i << 6
			valast := vastart + 63
			for valast >= vb.start && vastart <= vb.last && i < bitmapN {
				if vastart >= vb.start && valast <= vb.last { // a within b
					bitmap[i] = aBitmap[i]
					output.n += int32(popcount(aBitmap[i]))
				} else if vb.start >= vastart && vb.last <= valast { // b within a
					var mask uint64 = ((1 << (vb.last - vb.start + 1)) - 1) << (vb.start - vastart)
					bits := aBitmap[i] & mask
					bitmap[i] |= bits
					output.n += int32(popcount(bits))
				} else if vastart < vb.start { // a overlaps front of b
					offset := 64 - (1 + valast - vb.start)
					bits := (aBitmap[i] >> offset) << offset
					bitmap[i] |= bits
					output.n += int32(popcount(bits))
				} else if vb.start < vastart { // b overlaps front of a
					offset := 64 - (1 + vb.last - vastart)
					bits := (aBitmap[i] << offset) >> offset
					bitmap[i] |= bits
					output.n += int32(popcount(bits))
				}
				// update loop vars
				i++
				vastart = i << 6
				valast = vastart + 63
			}
		}
	}
	return output
}
559107
func bitmapFrom(input allocSet, minSize uint) structs.Bitmap {
	var max uint
	for _, a := range input {
		if num := a.Index(); num > max {
			max = num
		}
	}

	if l := uint(len(input)); minSize < l {
		minSize = l
	}

	if max < minSize {
		max = minSize
	} else if max%8 == 0 {
		// This may be possible if the job was scaled down. We want to make sure
		// that the max index is not byte-aligned otherwise we will overflow
		// the bitmap.
		max++
	}

	if max == 0 {
		max = 8
	}

	// byteAlign the count
	if remainder := max % 8; remainder != 0 {
		max = max + 8 - remainder
	}

	bitmap, err := structs.NewBitmap(max)
	if err != nil {
		panic(err)
	}

	for _, a := range input {
		bitmap.Set(a.Index())
	}

	return bitmap
}